我有一个类库,它有一些代码来执行某些操作。例如,它将执行2个操作,如:
- Add
- Multiply
将来可能会有更多的操作,例如分区。
上面只是一个例子,因为我对每个Add,Multiply操作都有一些长时间运行的操作。
所以这个库的想法是接收输入,然后针对这些输入执行长时间运行的代码。
这就是我的想法:
public class Input
{
//input properties
}
public interface IOperations
{
public abstract void Process(Input obj);
}
public class Add : IOperations
{
Input obj;
public Add(Input obj)
{
this.obj = obj;
}
public override void Process(Input obj)
{
//Add method implementation
}
}
public class Multiply : IOperations
{
Input obj;
public Multiply(Input obj)
{
this.obj = obj;
}
public override void Process(Input obj)
{
//Multiply method implementation
}
}
现在假设我想执行添加操作或乘法操作,那么我将如何根据以下类型调用相应的方法:
string type;
if(type=="Add")
//perform add operation
else if(type=="Multiply")
//perform multiply operation
但是我没有按照上述要求设计代码结构的正确方法。
注意: IOperations
创建Interface
的原因适用于dependency injection
答案 0 :(得分:2)
好的,正如所讨论的那样
为输入和结果定义类。
public class Input
{
}
public class Result
{
}
定义您的界面
public interface IOperations
{
Result Process(Input obj);
}
定义工厂以返回所需的实现
public class MathFactory
{
public IOperations GetOperatorByType(string type)
{
switch (type)
{
case "Add":
return new Add(new Input());
case "Multiply":
return new Multiply(new Input());
}
throw new Exception("Unknown type.");
}
}
定义具体实现
public class Add : IOperations
{
Input obj;
public Add(Input obj)
{
this.obj = obj;
}
public Result Process(Input obj)
{
//Perform Add here
return new Result();
}
}
public class Multiply : IOperations
{
Input obj;
public Multiply(Input obj)
{
this.obj = obj;
}
public Result Process(Input obj)
{
//Perform multiply here
return new Result();
}
}
最后从代码中调用。
private void button1_Click(object sender, EventArgs e)
{
var mathFactory = new MathFactory();
var operation = mathFactory.GetOperatorByType("Add");
var result = operation.Process(new Input());
}
显然,您将不得不修改输入对象的使用方式(现在或可能在界面上使用构造函数)......以及如何构造结果并将答案公开为属性。
希望有所帮助。
答案 1 :(得分:2)
这是strategy design pattern的好候选人。
定义一系列算法,封装每个算法,并使它们可以互换。
public interface IOperation
{
Output Process(Input input);
bool AppliesTo(string operation);
}
public interface IOperationStrategy
{
Output Process(string operation, Input input);
}
public class Add : IOperation
{
public bool AppliesTo(string operation)
{
return nameof(Add).Equals(operation, StringComparison.OrdinalIgnoreCase);
}
public Output Process(Input input)
{
// Implementation
return new Output();
}
}
public class Multiply : IOperation
{
public bool AppliesTo(string operation)
{
return nameof(Multiply).Equals(operation, StringComparison.OrdinalIgnoreCase);
}
public Output Process(Input input)
{
// Implementation
return new Output();
}
}
public class OperationStrategy : IOperationStrategy
{
private readonly IOperation[] operations;
public OperationStrategy(params IOperation[] operations)
{
if (operations == null)
throw new ArgumentNullException(nameof(operations));
this.operations = operations;
}
public Output Process(string operation, Input input)
{
var op = operations.FirstOrDefault(o => o.AppliesTo(operation));
if (op == null)
throw new InvalidOperationException($"{operation} not registered.");
return op.Process(input);
}
}
// I am showing this in code, but you would normally
// do this with your DI container in your composition
// root, and the instance would be created by injecting
// it somewhere.
var strategy = new OperationStrategy(
new Add(), // Inject any dependencies for operation here
new Multiply()); // Inject any dependencies for operation here
// And then once it is injected, you would simply do this.
// Note that it would not be appropriate to use an Enum for
// the operation, because the compiled Enum would have to
// remain in sync with the runtime operation values (not possible).
// That said, the data type doesn't necessarily need to be string.
var input = new Input { Value1 = 2, Value2 = 3 };
var output = strategy.Process("add", input);
// output.Value is 5
var output = strategy.Process("multiply", input);
// output.Value is 6
在工厂设计中使用此模式的一个优点是,设计不需要更改以添加或删除操作。在工厂设计中,您有一个硬编码的switch case语句,每次添加操作时都需要更改。
当然,如果您为每个IOperation
使用相同的类型,则对输入和输出的设置方式没有任何限制。我只是以这种方式显示它,因为将输出作为Process
方法的返回值是明智的,但您使用的实现可能会有所不同。