[英]Is the factory design pattern applicable in this situation
taskA,taskB,taskC从我的类Task继承
ContextA,ContextA2,ContextB,ContextC从我的类Context继承
上下文具有对应的Task作为类属性:
public abstract class Context
{
public String CommonProperty { get; set; }
public abstract void MethodToOverride();
}
public class ContextA : Context
{
public TaskA Task { get; set; }
public override void MethodToOverride()
{
//do stuff with this.Task and this.CommonProperty
}
}
public class ContextA2 : Context
{
public TaskA Task { get; set; }
public override void MethodToOverride()
{
//do stuff with this.Task and this.CommonProperty
}
}
public class ContextB : Context
{
public TaskB Task { get; set; }
public override void MethodToOverride()
{
//do stuff with this.Task and this.CommonProperty
}
}
等等...
遍历任务列表时,我想创建相应的Context:
foreach (Task t in tasks)
{
Context context;
if (t is TaskA)
{
if (condition)
{
context = new ContextA() { Task = t as TaskA};
}
else
{
context = new ContextA2() { Task = t as TaskA };
}
}
else if (t is TaskB)
{
context = new ContextB() { Task = t as TaskB };
}
else if (t is TaskC)
{
context = new ContextC(){ Task = t as TaskC };
}
else
{
throw new Exception("Unkown Task");
}
context.CommonProperty = "value";
context.MethodToOverride();//Do different things based on the context type
}
我觉得应该有一个更干净的方法来实现这一目标,但是我无法弄清楚如何管理上下文对象的创建,尤其是取决于条件的contextA和contextA2的情况。
foreach (Task t in tasks)
{
Context context = t.ConstructContext(condition);
context.CommonProperty = "value";
context.MethodToOverride();//Do different things based on the context type
}
public asbtract class Task
{
//whatever might be in here
public abstract Context ConstructContext();
}
public class TaskA : Task
{
//NOTE: In my opinion condition should be internal to TaskA and no one else should know about and you should remove the parameter from the method
// but I don't know where you get it from so I'm leaving it here
public override Context ConstructContext(bool condition)
{
if (condition)
{
return new ContextA() { Task = this};
}
else
{
return new ContextA2() { Task = thid };
}
}
}
public class TaskB : Task
{
public override Context ConstructContext(bool condition)
{
//ignore condition which implies it shouldn't really be passed
return new ContextB() {Task = this};
}
}
//etc...
我真的不喜欢传递这种条件,在我看来,它是一种代码气味,但是从您所说的来看,它必须是任务之外的东西。 无论如何这应该满足您的需求
是的,适合工厂模式。
class ContextFactory {
public create(TaskA t, bool condition) {
return condition ? new ContextA() { Task = t } : new ContextA2() { Task = t };
}
public create(TaskB t) {
return new ContextB() { Task = t };
}
public create(TaskC t) {
return new ContextC() { Task = t };
}
}
...
ContextFactory factory = //... new or passed from somewhere
foreach (Task t in tasks) {
Context context;
if (t is TaskA) {
context = factory.create(t as TaskA, condition);
} else if (t is TaskB) {
context = factory.create(t as TaskB);
} else if (t is TaskC)
context = factory.create(t as TaskC);
} else {
throw new Exception("Unkown Task");
}
context.CommonProperty = "value";
context.MethodToOverride();//Do different things based on the context type
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.