简体   繁体   English

如何通过实现适配器模式来隔离胖接口?

[英]How can I segregate fat interface through implementing adaptern pattern?

Suppose I have a some fat interface, which cannot be changed.假设我有一个不能改变的胖接口。 And also I have some client class which want to use only few methods from that fat interface.而且我还有一些客户端类只想使用该胖接口中的少数方法。 How can be implemented adapter pattern for this situation, to achieve Interface Segregation Principle?对于这种情况,如何实现适配器模式,以实现接口隔离原则?

You can do the following:您可以执行以下操作:

// 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();
    }
}

The adapter isn't really the right tool here.适配器在这里并不是真正的正确工具。 Its designed to make two incompatible interfaces be able to talk by adapting one to the other.它旨在通过使两个不兼容的接口相互适应来进行通话。 In this case you want to expose some subset of functionality differently base on the end user.在这种情况下,您希望根据最终用户以不同方式公开某些功能子集。 In this case you want to use a facade .在这种情况下,您要使用外观

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.

相关问题 如何隔离DateTime的属性? - How can I segregate properties of DateTime? 如何重构胖接口? - How to Refactor a fat interface? c# - 如何提取FAT磁盘映像? - c# - How can I extract a FAT Disk Image? 如何遍历字符串,替换与模式匹配的部分? - How can I loop through a string, replacing sections that match a pattern? 如何检查拖到检查器上的可编写脚本的 object 是否正在实现接口? - How can I check if a scriptable object dragged onto inspector is implementing an interface or not? 我如何从运行时已知的类派生一个类,实现编译时已知的接口 - How can I have a class derived from a class known at runtime implementing an interface known at compilation 我可以将对象视为通过反射实现接口吗? - Can I treat an object as implementing an interface from reflection? 是还是不是? 我可以在不同的程序集中划分接口和实现类吗? - Yes or No? Can I divide interface and implementing classes in different assemblies? 实施复选框产品过滤器 ASP.NET MVC - 如何通过 URL 传递集合中的数据? - Implementing Checkbox Product Filters ASP.NET MVC - How Can I Pass Data in Collection through URL? 使用事件通过表单类实现接口 - Implementing an interface through a form class using an event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM