简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM