« Adapter pattern » : différence entre les versions
Apparence
Page créée avec « Category:CSharp Category:Design Patterns = Definition = Convert the interface of a class into another interface that clients expect.<br> The Adapter pattern lets c… » |
Aucun résumé des modifications |
||
Ligne 7 : | Ligne 7 : | ||
= Exemple = | = Exemple = | ||
<filebox fn='Rectangle.cs'> | |||
public interface IRectangle | |||
{ | |||
int GetArea(); | |||
} | |||
public class Rectangle : IRectangle | |||
{ | |||
private readonly int width; | |||
private readonly int lenght; | |||
public Rectangle(int width, int lenght) | |||
{ | |||
this.lenght = lenght; | |||
this.width = width; | |||
} | |||
public int GetArea() => width * lenght; | |||
} | |||
</filebox> | |||
<filebox fn='Disk'> | |||
public interface IDisk | |||
{ | |||
double ComputeArea(); | |||
} | |||
public class Disk : IDisk | |||
{ | |||
private readonly int radius; | |||
public Disk(int radius) | |||
{ | |||
this.radius = radius; | |||
} | |||
public double ComputeArea() => 2 * Math.PI * radius; | |||
} | |||
</filebox> | |||
<filebox fn='DiskToRectangleAdapter.cs'> | |||
public class DiskToRectangleAdapter : IRectangle | |||
{ | |||
private readonly IDisk disk; | |||
public DiskToRectangleAdapter(IDisk disk) | |||
{ | |||
this.disk = disk; | |||
} | |||
public int GetArea() => (int)disk.ComputeArea(); | |||
} | |||
</filebox> | |||
<kode lang='cs'> | |||
var rectangles = new List<IRectangle>(); | |||
IRectangle rectangle = new Rectangle(40, 30); | |||
rectangles.Add(rectangle); | |||
IDisk disk = new Disk(100); | |||
IRectangle adaptedDisk = new DiskToRectangleAdapter(disk); | |||
rectangles.Add(adaptedDisk); | |||
DisplayArea(rectangles); | |||
void DisplayArea(IEnumerable<IRectangle> rectangles) | |||
{ | |||
foreach (var rectangle in rectangles) | |||
{ | |||
Console.WriteLine(rectangle.GetArea()); | |||
} | |||
} | |||
</kode> |
Dernière version du 7 juin 2020 à 17:07
Definition
Convert the interface of a class into another interface that clients expect.
The Adapter pattern lets classes work together that could not otherwise because of incompatible interfaces.
- Match otherwise incompatible interfaces
Exemple
Rectangle.cs |
public interface IRectangle
{
int GetArea();
}
public class Rectangle : IRectangle
{
private readonly int width;
private readonly int lenght;
public Rectangle(int width, int lenght)
{
this.lenght = lenght;
this.width = width;
}
public int GetArea() => width * lenght;
}
|
Disk |
public interface IDisk
{
double ComputeArea();
}
public class Disk : IDisk
{
private readonly int radius;
public Disk(int radius)
{
this.radius = radius;
}
public double ComputeArea() => 2 * Math.PI * radius;
}
|
DiskToRectangleAdapter.cs |
public class DiskToRectangleAdapter : IRectangle
{
private readonly IDisk disk;
public DiskToRectangleAdapter(IDisk disk)
{
this.disk = disk;
}
public int GetArea() => (int)disk.ComputeArea();
}
|
var rectangles = new List<IRectangle>();
IRectangle rectangle = new Rectangle(40, 30);
rectangles.Add(rectangle);
IDisk disk = new Disk(100);
IRectangle adaptedDisk = new DiskToRectangleAdapter(disk);
rectangles.Add(adaptedDisk);
DisplayArea(rectangles);
void DisplayArea(IEnumerable<IRectangle> rectangles)
{
foreach (var rectangle in rectangles)
{
Console.WriteLine(rectangle.GetArea());
}
}
|