2015-10-18 74 views
1

我有一個包含視圖依賴關係(所有接口)的類。基本上,類的行爲是通過實現這些接口來定義的。我希望能夠有一個「構建器」,它可以通過接口的不同實現(或其部分)來創建此類的實例。這樣的事情:尋找一種設計模式,可以用不同的接口實現創建一個類的不同實例

public class API 
{ 
private readonly ISomeInterface _someInterface; 
private readonly ISomeOtherInterface _someOtherInterface; 
private readonly ISomeAnotherInterface _someAnotherInterface; 

API(ISomeInterface someInterface,ISomeOtherInterface someOtherInterface,ISomeAnotherInterface someAnotherInterface) 
{*/implementation ommitted*/} 

//Example method 
public void DoSomethingWhichDependsOnOneOrMoreInterfaces() 
{ 
    //somecode 
    id(_someInterface != null) 
    _someInterface.SomeMethode(); 
} 


public class MyApiBuilder() 
{ 
    // implementation ommitted 
    API CreateAPI(someEnum type) 
    { 
    switch(type) 
    { 
     case SpecificAPI32: 
      var speficImplementationOfSomeInterface = new ImplementsISomeInterface(); 
      speficImplementationOfSomeInterface .Setup("someSetup"); 
      var specificImplementationOfOtherInterface = new ImplementsISomeOtherInterface(); 
      returns new API(speficImplementationOfSomeInterface,specificImplementationOfOtherInterface ,null); 

    } 
    } 
} 

什麼是最優雅的方式來實現它(如果這是有道理的)?我首先想到了Builder的設計模式,但據我所知,它略有不同。

[編輯] 正如我指出的那樣,我實施它的方式是一種工廠方法,但我並不完全滿意它。 API可以包含各種不同的接口,這些接口可以完全獨立,但有些可能依賴於其他接口(但不是強制性的)。我想給用戶(開發人員使用這個「API」)儘可能多的自由在創建他想要使用的API。讓我們試着解釋一下我基本上可以達到: 假設我正在開發一個遊戲引擎的插件,它可以將成就和其他內容發佈到各種社交媒體渠道。所以基本上可以有一個接口,它實現了對twitter,facebook,youtube,無論什麼或者一些定製服務器的訪問。這個自定義服務器可能需要某種認證過程。用戶應該能夠在開始時以良好的(hmm fluent is nice ..)方式構建API。所以基本上是這樣的:

var myTotallyForMyNeedsBuildAPI = API.CreateCustomApi().With(Api.Twitter).And(Api.Facebook).And(Api.Youtube).And(Api.CustomServer).With(Security.Authentification); 

我實際上不知道如何使流利,但這樣的事情會很好。

+0

你有沒有檢查工廠? –

+0

是的,我檢查了工廠的方法,這基本上是我的例子做對了嗎?但我想我不需要靜態的MyApiBuilder,但我想知道是否有更優雅的方法。 – zlZimon

+0

根據應用程序的規模,您可能需要考慮依賴注入。 –

回答

1

它使用依賴注入,你想給程序員的能力組成與所需的配置對象一個很好的做法。

檢查MEFUnity非常適合這項工作的框架。

例如在Unity,你可以這樣寫:

// Introducing an implementation for ISomeInterface 
container.Register<ISomeInterface, SomeImplementation>(); 
// Introducing an implementation for ISomeOtherInterface 
container.Register<ISomeOtherInterface, SomeOtherImplementation>(); 
// Introducing an implementation for ISomeAnotherInterface 
container.Register<ISomeAnotherInterface, SomeAnotherImplemenation>(); 
container.Register<API, API>(); 

// and finally unity will compose it for you with desired configurations: 
var api = container.Resolve<API>(); 

在這種情況下的api組成與所期望的實現。

1

你實施的是Factory method模式。

對於您正在嘗試做的事情來說,這是完全正確的,但您可以根據您的上下文以及您認爲自己的代碼將來會如何演變來查看其他工廠模式(即here)。

無論如何,我也會考慮不要把這三個接口綁在一個工廠裏。如果他們真的如此緊張在一起被消耗在一起並且一起建立起來,也許他們不應該是三個不同的接口,或者至少所有三個接口都是由同一個類實現的,所以你的工廠將用適當的方法構建適當的類執行這些。

你可能後面的是Decorator pattern。 在您的API類中,如果已將API接口實例提供給每個接口,那麼這是Decorator模式的行爲。 通過這種模式,您可以獲得一個模塊化的實現,允許您將多個行爲添加到您的API。

+0

不是我,但是如果給出這個標記的理由是有幫助的。也許不是答案......但有一些可信的信息....馬克已經消失...... – Seabizkit

+0

那些接口實際上並沒有收緊在一起,他們只是定義了API的實現 – zlZimon

+0

你是否期望提供只有某些特定API實例的接口或未來添加新接口的接口?我在答案中增加了一個新段落。 – AleFranz

相關問題