Convert
the interface of a class into another interface clients expect. Adapter lets
classes work together that couldn't otherwise because of incompatible
interfaces.
Participants
The
classes and/or objects participating in this pattern are:
- Target(Chemical Compound) : Defines the domain-specific interface that Client uses.
- Adapter(Compound) :Adapts the interface Adaptee to the Target interface.
- Adaptee(Chemical Data bank): Defines an existing interface that needs adapting.
- Client(Adapter App): Collaborates with objects conforming to the Target interface.
// Adapter pattern -- Structural
example
|
using System;
namespace Adapter.Structural
{
/// <summary>
/// MainApp
startup class for Structural
/// Adapter
Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry
point into console application.
/// </summary>
static void Main()
{
//
Create adapter and place a request
Target target
= new Adapter();
target.Request();
//
Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The
'Target' class
/// </summary>
class Target
{
public virtual void Request()
{
Console.WriteLine("Called
Target Request()");
}
}
/// <summary>
/// The
'Adapter' class
/// </summary>
class Adapter : Target
{
private Adaptee _adaptee
= new Adaptee();
public override void Request()
{
//
Possibly do some other work
//
and then call SpecificRequest
_adaptee.SpecificRequest();
}
}
/// <summary>
/// The
'Adaptee' class
/// </summary>
class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine("Called
SpecificRequest()");
}
}
}
|
Output
Called
SpecificRequest()
|
No comments:
Post a Comment