Run the following code in Linqpad:
void Main()
{
IBowler bowler = new Bowler();
IBatter allRounder = new AllRounderAdapter(bowler);
allRounder.Bat();
}
// IBowler and IBatter are incompatible interfaces
internal interface IBowler
{
void Bowl();
}
internal interface IBatter
{
void Bat();
}
// Adapter
internal class AllRounderAdapter : IBatter
{
private IBowler bowler;
public AllRounderAdapter(IBowler bowler)
{
this.bowler = bowler;
}
public void Bat()
{
Console.WriteLine("Batting...");
}
}
// Adaptee
internal class Bowler : IBowler
{
public void Bowl()
{
Console.WriteLine("Bowling...");
}
}
Output:Batting...
Comments
Post a Comment