我目前正在從一個服務器發送數據到下一個服務的服務結構解決方案。當處理web請求時使用SOLID方法
我正在發送具有多個屬性的類。這個類是一種數據傳輸對象。我想用一種可靠的方法來防止使用導致切換邏輯的整數。
即。我不想這樣:
public void ExecuteFunctionForType(IntegerTypedClass myClass, IInterface myinterface)
{
switch(myClass.typeInt)
{
case 1:
// Do shizzle for type 1
myinterface.execute1();
break;
case 2:
// do shizzle for type 2
myinterface.execute2();
break;
}
}
而是我想實現一個SOLID方法。浮現在腦海的一個如下圖所示:
public abstract class AbstractTypedClass
{
public abstract void ExecuteInheritedFunction(IInterface myinterface);
}
public class FirstTypedClass : AbstractTypedClass
{
public override void ExecuteInheritedFunction(IInterface myinterface)
{
// do shizzle for the first typed class
myinterface.execute1();
}
}
public class SecondTypedClass : AbstractTypedClass
{
public override void ExecuteInheritedFunction(IInterface myinterface)
{
// do shizzle for the second typed class
myinterface.execute2();
}
}
這種方法的問題是,AbstractTypedClass
,它的所有的孩子都會對IInterface
,我不想依賴。這是由於它是一個服務結構接口,它帶來了很多其他的依賴關係。 我希望有一個解決方案,你可以使用固體原則,而不是建立在不同的庫的依賴,這將弄亂我的解決方案
事情我不能使用:
部分類,因爲他們需要要在同一assemvly
擴展方法,因爲默認的方法被調用:
public static class TypedClassExtensions
{
public static void executeExtension(this AbstractTypedClass request, IInterface myinterface)
{
throw new NotImplementedException();
}
public static void executeExtension(this FirstTypedClass request, IInterface myinterface)
{
myinterface.execute1();
}
}
// when calling the code like so, the not implemented exception is thrown:
public void execute(AbstractTypedClass myclass, IInterface myinterface)
{
myclass.executeExtension(myinterface);
}
嘗試戰略模式http://blogs.microsoft.co.il/gilf/2009/11/22/applying-strategy-pattern-instead-of-using-switch-statements/ – Nkosi
是不是什麼策略我剛剛描述過?如果我使用策略,我仍然堅持依賴於接口。我正在尋找一種方法來消除依賴關係 – martijn
向我們展示您所需的設計輸出。它不必工作..只是爲了得到想法。因爲你說過你不需要'interface'依賴。 – niksofteng