因此,對於繼承,我始終會發現自己處理最特定類中的極長構造函數。我正在尋找一種可以避免這個問題的範式或方法。我想過使用工廠,但如果我這樣做了,我仍然必須在工廠中使用構造函數。不管那個想法,有什麼我可以做的,以保持構造參數長度下來?使用OOP繼承我如何在「子」類中保持較低的參數
例如:
abstract class DrawableComponent : Component, ITransformable, IScalable, IDrawable, IRotatable
是兒童類構造函數像
DrawableComponent(string Name, int SizeX, int SizeY, int X, int Y, float Rotation) : base(Name) { ... }
現在,讓我們說,我想做出一個孩子。事情變得更加複雜和醜陋。
class TextBoxComponent : DrawableComponent, IRenderable
構造函數現在變得非常長:
public TextBoxComponent(
IDrawableUnit Background,
IDrawableUnit Text,
string Name,
int X,
int Y,
int SX,
int SY)
: base(Name, SX,SY,X,Y,0.0f)
{
this.Background = Background;
this.Foreground = Foreground;
}
在這個總結,我看了就討厭這樣的事情的:
class Blah {Blah(blah)}
class childBlah : Blah {childBlah(blah,blahs,blaha) : base (blah)}
class grandChildBlah : childBlah {grandChildBlah(blah, blahs, blaha, blaht, blauh) : base(blah,blahs,blaha) }
極其長的構造函數正在做什麼? –
使用您最喜愛的IoC容器/ DI框架。這是依賴注入解決的許多問題之一。 – Kane
@Kane - 雖然構造函數DI解決了需要「服務」的類的問題,但它並沒有解決任何帶有值的構造函數,例如示例中的值。 – Lucero