lex*_*eme 2 c# design-patterns strategy-pattern
使用默认策略是否正常,如下面的代码所示:
public abstract class ClContext
{
protected sealed class InitialAlgorithm : IClAlgorithm
{
public void Initialize()
{
return;
}
public void Execute()
{
return;
}
public Byte[] Result
{
get { return new Byte[1]{0}; }
}
}
protected IClAlgorithm algorithm;
protected ClContext(IClAlgorithm algorithm = null)
{
this.algorithm = algorithm ?? new ClContext.InitialAlgorithm();
}
public void Execute()
{
this.algorithm.Execute();
}
}
Run Code Online (Sandbox Code Playgroud)
提供自动实现的属性也是正常的,例如:
public IClAlgorithm Algorithm
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
从设计的角度来看,我只是好奇,这是可以接受的.
谢谢!
我无法想象这种设计实际上有用的场景 - 您的类依赖于通过其构造函数传递的策略,以后可能通过属性设置器进行更改.不传递依赖项不应允许调用者创建类的实例.
你应该只提供一个"默认"策略,如果它实际上做了一些有用的事情,即使这样我也不会将它们绑在一起,但是有一个工厂方法用策略创建你的类.
| 归档时间: |
|
| 查看次数: |
561 次 |
| 最近记录: |