小编Jos*_*osh的帖子

策略模式还是界面?

我正在寻找抽象一个辅助方法。该方法需要能够接收一个对象,根据对象的类型对其执行操作,并返回一个值。这样做会更好吗:

interface ICanDo
{
 string DoSomething();
}

string DoThings(ICanDo mything)
{
 return mything.DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

或者最好做这样的事情:

interface IStrategy
{
 string DoSomething(object o);
}

string DoThings(object mything, IStrategy strategy)
{
 return strategy.DoSomething(mything);
}
Run Code Online (Sandbox Code Playgroud)

后者是否甚至使用策略模式,因为该策略没有内置到类中?

有没有更好的方法来做到这一点我没有想到?将策略构建到类中,为任何需要运行 DoThings 的类使用包装器会更好吗?

抱歉——我对这种模式很陌生,并试图弄清楚在哪里以及如何最好地使用它。

这就是我最终整理出来的。我不确定这是否遵循良好的开发原则。

class IndexWrapper
{
    public interface IDocumentable
    {
        Document BuildDocument();
    }

    public interface IDocumentBuilder
    {
        Type SupportedType { get; }

        Document BuildDocument(object o);
    }

    public class StringDocumentBuilder : IDocumentBuilder
    {
        public Type SupportedType { get { return typeof(string); } }

        public Document BuildDocument(object o) …
Run Code Online (Sandbox Code Playgroud)

design-patterns strategy-pattern c#-4.0

7
推荐指数
1
解决办法
3225
查看次数

一个类知道它在数组中的位置是不好的做法吗?

类是否将其位置存储在数组中是不好的做法?

同样,集合维护每个对象的内部位置是不好的做法?

如果这些是不好的做法,有什么更好的方法可以快速访问数组中对象的位置或维持该位置?

c# arrays class

1
推荐指数
1
解决办法
166
查看次数

标签 统计

arrays ×1

c# ×1

c#-4.0 ×1

class ×1

design-patterns ×1

strategy-pattern ×1