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?
Asked
Active
Viewed 984 times
2 Answers
10
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();
}
}

Steven
- 166,672
- 24
- 332
- 435
-
2+1 Nobody want's to be fat (or segregated for that matter) :( – jgauffin Feb 23 '12 at 13:26
-
So I need also a class which implements the IAmFat, and which instance will be injected to FatAdapter constructor? – Nick Bratym Feb 23 '12 at 13:57
-
No, if your fat class doesn't have an interface, you just inject that class. And if the class has static methods, you don't need to inject it at all, you can simply call one of them directly from within the `FatAdapter`. – Steven Feb 23 '12 at 14:06
-
Suppose IAmFat is the IApplicationHost from System.Web.Hosting, therefore I need create a class that's implementing IApplicationHost for injecting into FatAdapter constructor? – Nick Bratym Feb 23 '12 at 14:30
-
If the consumer is already talking to the `IApplicationHost` it means there already is an implementation of `IApplicationHost`, and you don't need to create your own implementation. Just inject the existing instance into the `FatAdapter`. – Steven Feb 23 '12 at 14:51
0
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();
}
}

Nix
- 57,072
- 29
- 149
- 198