[英]How can I segregate fat interface through implementing adaptern pattern?
假設我有一個不能改變的胖接口。 而且我還有一些客戶端類只想使用該胖接口中的少數方法。 對於這種情況,如何實現適配器模式,以實現接口隔離原則?
您可以執行以下操作:
// Assuming this is your fat interface
interface IAmFat
{
void Method1();
void Method2();
...
void MethodN();
}
// You create a new interface that copies part of the fat interface.
interface IAmSegregated
{
void Method1();
}
// You create an adapter that implements IAmSegregated and forward
// calls to the wrapped IAmFat.
class FatAdapter : IAmSegregated
{
private readonly IAmFat fat;
public FatAdapter(IAmFat fat)
{
this.fat = fat;
}
void IAmSegregated.Method1()
{
this.fat.Method1();
}
}
適配器在這里並不是真正的正確工具。 它旨在通過使兩個不兼容的接口相互適應來進行通話。 在這種情況下,您希望根據最終用戶以不同方式公開某些功能子集。 在這種情況下,您要使用外觀。
class Fat{
public string A();
public int B();
.
public void EatMeat()
.
public void Z();
}
class JennyCraig{
private Fat f = Fat();
public string A(){
return f.A();
}
public void Z(){
return f.Z();
}
class Atkins{
public Fat f = Fat();
public void EatMeat(){
return f.EatMeat();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.