[英]What design pattern is this? Adapter?
[ 參見下面的更新 ]
我很難定義模式。 我的同事說這是適配器模式。 我不確定。 我們之所以陷入困境,主要是因為我們想正確命名我們的組件。
問題: 是適配器模式嗎? 如果不是,那是什么? 如果還有其他事情,這是否是實現該想法的最佳方法?
概括地說,它是一個主要組件( 這是適配器嗎? )與子組件( 這些提供者是嗎? )共享一個接口。 主要組件決定/協調哪個子組件被稱為 。 主要組件的行為類似於某種“包裝器”,以調用具有相同接口的其他組件。 其實例通過構造函數注入。
假設 :
我正在使用一個社交網絡方案,其中一個主要組件在hastag上獲取統計信息並實例化相應的Social Sub Component(有一個Social Component接口:
ISocialComponent
{
SomeStatsObject GetStats(string hashTag);
}
社交子組件實現ISocialComponent接口
Twitter子組件
public class TwitterSubComponent : ISocialComponent
{
public SomeStatsObject GetStats(string hashTag)
{
return SomeMethodThatReturnsStatsObject(hashTag);
}
private SomeMethodThatReturnsStatsObject(string hashTag)
{
//... Twitter-specific code goes here
}
}
Facebook子組件
public class FacebookSubComponent : ISocialComponent
{
public SomeStatsObject GetStats(string hashTag)
{
return SomeMethodThatReturnsStatsObject(hashTag);
}
private SomeMethodThatReturnsStatsObject(string hashTag)
{
//... Facebook-specific code goes here
}
}
Instagram子組件
public class InstagramSubComponent : ISocialComponent
{
public SomeStatsObject GetStats(string hashTag)
{
return SomeMethodThatReturnsStatsObject(hasTag);
}
private SomeMethodThatReturnsStatsObject(string hashTag)
{
//... Instagram-specific code goes here
}
}
主要成分
有一個主要的社交組件對象,該對象調用實現共享ISocialComponent接口的子組件(定義如下)中的任何一個
public class MainSocialComponent : ISocialComponent
{
//this is an enum
private RequestedNetwork _requestedNetwork{ get; set;}
//the SocialComponent instance is injected outside of this class
private readonly ISocialComponent _socialComponent;
public MainSocialComponent(ISocialComponent socialComponent)
{
_socialComponent = socialComponent;
}
public SomeStatsObject GetStats(string hashTag)
{
return _socialComponent.GetStats(hashTag)
/**** original code, kept for historical purposes****
switch(_requestedNetwork)
{
case RequestedNetwork.Twitter:
var twit = new TwitterSubComponent();
return twit.GetStats(hashTag)
break;
case RequestedNetwork.Facebook:
var fb = new FacebookSubComponent();
return fb.GetStats(hashTag)
break;
case RequestedNetwork.Instagram:
var in = new InstagramSubComponent();
return in.GetStats(hashTag)
break;
default:
throw new Exception("Undefined Social Network");
break;
}*/
}
}
更新:
我知道為什么有人說這是Factory模式,因為它正在創建對象。 我已經提到我們使用IoC容器和DR。 排除這一點是我的錯誤。 我已經重構了代碼
正如其他人所提到的,這是工廠/服務模式的一部分,在依賴注入和控制反轉中非常流行。
現在,盡管沒有理由將子組件聲明為非靜態,因為您沒有將實例保存到任何內容。
所以在我看來,除非您缺少將組件添加到列表中的代碼,否則您可以這樣做:
public static class InstagramSubComponent : ISocialComponent
{
public static SomeStatsObject GetStats(string hashTag)
{
return stuff;
}
}
public class MainSocialComponent : ISocialComponent
{
//this is an enum
private RequestedNetwork _requestedNetwork{ get; set;}
private static var Mappings = new Dictionary<string, Func<SomeStatsObject>> {
{ "Twitter", TwitterSubComponent.GetStats },
{ "Facebook", FacebookSubComponent.GetStats },
{ "Instagram", InstagramSubComponent.GetStats }
}
public SomeStatsObject GetStats(string hashTag)
{
return Mappings[hashTag].invoke();
}
}
}
現在,如果您正在做一些事情,例如將子組件實例實際保存到列表中以備后用或進行其他操作,那么一切都會改變。 但是我沒有看到,因此如果這些方法很簡單,就沒有理由不將其全部靜態化。
如果它們非常復雜,那么您將需要使用依賴注入,以便可以對所有內容進行單元測試。
我相信您可以將SubComponent
創建提取到Factory
然后將此Factory
傳遞給MainSocialComponent
。 在GetStats
方法內部,您將調用_factory.Create(hashTag);
然后在返回的對象上調用GetStats
。 這樣,您將擁有工廠模式。
這絕對不是適配器模式。
在大多數情況下,適配器模式執行以下操作:
•充當兩個不兼容接口之間的橋梁。
•允許具有不兼容接口的類一起工作
您的案例更像是Factory模式 。 您可以使用高級抽象,並在需要時返回接口/組件的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.