簡體   English   中英

工廠設計模式是否適用於這種情況

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM