[英]Simple factory design in C# calling factory from another factory?
我需要在C#中使用簡單工廠創建一個對象,該對象具有一個依賴項對象,該對象也使用另一個簡單工廠創建。
從另一個簡單工廠調用一個簡單工廠是一個好的設計原則嗎? 我想我可能在做些奇怪的事情。
我有以下幾點:
interface IObjecAtImplement {
void task();
}
interface IObjecBtImplement {
void taskFoo();
void anotherTaskFoo();
}
public static class ObjectAFactory {
public static IObjecAtImplement Get(string objecttype) {
IObjectBImplement objectB = ObjectBFactory.Get(objecttype);
switch (objecttype)
case "A";
return ObjectAFirstImplementacion(objectB);
case "B";
return ObjectASecondImplementacion(objectB);
}
}
public static class ObjectBFactory {
public static IObjecBtImplement Get(string objecttype) {
switch (objecttype)
case "A";
return new ObjectBFirstImplementacion();
case "B";
return new ObjectBSecondImplementacion();
}
}
}
這是個好主意嗎? 還有其他解決方法嗎? (代碼括號寫得不好,只是一個例子)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.