2014-01-21 39 views
0

我有一個程序,我使用的管理器維護一個泛型類型的字典。我希望能夠註冊新類型(使用多態)並將其存儲在字典中(使用基於整數的鍵),然後能夠基於該存儲類型創建新對象。以下是我有:從字典列表中返回一個新對象

Dictionary<uint,GenericClass> mGenericLibrary = new Dictionary<uint,GenericClass>(); 

public GenericClass GetNewGenericType(uint id) 
{ 
    return mGenericLibrary[id]; 
} 

字典將舉行的GenericClass類型,即GenericClassSub1,GenericClassSub2,GenericClassSub3等子類....

在這一點上,我想在另一個類中能夠調用GetNewGenericType並根據註冊的整數ID獲取一個新類型的子類型。

我該怎麼做?

+0

你可以從其他類調用'GetNewGenericType'嗎?它是'公共'。 – rae1

+0

它會不會返回集合中的實際對象而不是新的對象? –

+0

所以你想要返回存儲在給定'id'處的對象類型的新實例嗎?如果是這樣,不管在哪裏調用,GetNewGenericType都不會這樣做。你需要更新這個問題,更清楚地說明你需要什麼。 – rae1

回答

2

你要做的是叫做「Factory Pattern」。而不是保存你想要創建的類型的字典有字典保持類來生成你想創建的類型(「工廠」)。

public abstract GenericFactory 
{ 
    public abstract GenericClass CreateInstance(); 
} 

public GenericClassSub1Factory : GenericFactory 
{ 
    public override GenericClass CreateInstance() 
    { 
     return new GenericClassSub1(); 
    } 
} 

public GenericClassSub2Factory : GenericFactory 
{ 
    public override BaseClass CreateInstance() 
    { 
     return new GenericClassSub2(); 
    } 
} 

然後你宣佈你的字典,並使用它,像這樣:

class MyClassFactory 
{ 
    Dictionary<uint,GenericFactory> mGenericLibrary = new Dictionary<uint,GenericFactory>(); 

    public void RegisterFactory(uint id, GenericFactory factory) 
    { 
     mGenericLibrary[id] = factory; 
    } 

    public GenericClass GetNewGenericType(uint id) 
    { 
     return mGenericLibrary[id].CreateInstance(); 
    } 
} 


void Example() 
{ 
    var factory = new MyClassFactory(); 

    factory.RegisterFactory(1, new GenericClassSub1Factory()); 
    factory.RegisterFactory(2, new GenericClassSub2Factory()); 
    factory.RegisterFactory(3, new GenericClassSub3Factory()); 

    var item1 = factory.GetNewGenericType(1); //Contains a new GenericClassSub1; 
    var item2 = factory.GetNewGenericType(2); //Contains a new GenericClassSub2; 
    var item3 = factory.GetNewGenericType(3); //Contains a new GenericClassSub3; 
} 

UPDATE:

如果你不想讓人們作出工廠類,你仍然可以做工廠模式,但通過委託來完成,那麼最終用戶需要的代碼少得多才能添加到工廠中。

class MyClassFactory 
{ 
    Dictionary<uint,Func<GenericClass>> mGenericLibrary = new Dictionary<uint,Func<GenericClass>>(); 

    public void RegisterFactory(uint id, Func<GenericClass> factory) 
    { 
     mGenericLibrary[id] = factory; 
    } 

    public GenericClass GetNewGenericType(uint id) 
    { 
     //This could be one line, but i think mGenericLibrary[id]() looks too weird. 
     Func<GenericClass> factory = mGenericLibrary[id]; 
     return factory(); 
    } 
} 

    void Example() 
{ 
    var factory = new MyClassFactory(); 

    factory.RegisterFactory(1,() => new GenericClassSub1()); 
    factory.RegisterFactory(2,() => new GenericClassSub2()); 
    factory.RegisterFactory(3,() => new GenericClassSub3()); 

    var item1 = factory.GetNewGenericType(1); //Contains a new GenericClassSub1; 
    var item2 = factory.GetNewGenericType(2); //Contains a new GenericClassSub2; 
    var item3 = factory.GetNewGenericType(3); //Contains a new GenericClassSub3; 
} 
+0

我會使用'interface',否則,不壞。 – rae1

+0

我也會,但他在他原來的代碼中使用了'GenericClass',所以我試圖匹配他的模式。但是,是的,如果你可以用接口替換基類,它將在後面打開更多的選項。 –

+0

是否有可能在基類中創建一個「CreateInstance()」方法並使用它? 我真的想要做的是讓特定的子對象不需要知道,直到運行時,誰想要添加新的子類,當他們想擴展時,不需要沿着子類創建工廠,他們只是註冊新的類型 –

相關問題