[Design Pattern] Adapter Pattern

Repeat using a different interface and you expect the existing class !!


Official Description

One class interface, is converted into another interface for customer use. Interface adapter so that the original collaboration between incompatible class.

The primary efficacy

Repeat using a different interface and you expect the existing class

Through the adapter (Adapter) interface different classes (the Adaptee) converted to a user (Client) desired target interface (ITarget)

image

Applicable

1. Conversion interface requirements

2. Interface functionally similar (with corresponding function)

Key Heart

1. Establish adapter (Adapter) [inherited desired target interface (ITarget)]

2. Object synthetically through packaging (wrap) is redirected by (the Adaptee)

3. The method of transfer will be those (the Adaptee) The method corresponds to the target interface (ITarget) required in practice

4. The customer is not required to modify the program, conversion Adaptee Adapter directly, to the object through the operation Adaptee Itarget original interface.

Lenovo applications

There are a remote control (RemoteController) only supports control with mechanical switch (ISwitchable) lamps, along with the advancement of technology, with a number of manufacturers to develop with touch switches (ITouchable) lamps, of course, the boss wants new remote control can also touch the line of lamps; analyze demand, remote control product already on the market is not allowed to be modifiers, developed touch table lamp manufacturers can not be in accordance with our practice of unreasonable demands ISwitchable interface to meet our needs, so we can only to find ways to convert ITouchable ISwitchable interface to meet the remote control. Below we will through practical exercises on how to assist Adapter mode, the conversion to a different interface objects we look forward interface.

First, check whether it is suitable for scenarios:

1. Conversion interface requirements

    Conversion ITouchable to ISwitchable interface to meet the goals of the remote control interface requirements

2. Interface functionally similar (with corresponding function)

    ITouchable and ISwitchable open interface Jie (On) and off (Off) function

Take a look at the system already exists in the class diagram

image

* Mechanical lamp (practical mechanical switch interface)


interface ISwitchable
{
    // 机械式开启
    void SwitchOn();

    // 机械式关闭
    void SwitchOff();
}

class SwitchableLamp : ISwitchable
{
    // 机械式开启
    public void SwitchOn()
    {
        Console.WriteLine("开灯(使用机械式开关)");
    }

    // 机械式关闭
    public void SwitchOff()
    {
        Console.WriteLine("关灯(使用机械式开关)");
    }
}

* remote control


class RemoteController
{
    // 只支持操控"具有机械式开关特性"对象
    ISwitchable _switchableLamp;

    // 注入"具有机械式开关特性"对象实例
    public RemoteController(ISwitchable switchableLamp)
    {
        _switchableLamp = switchableLamp;
    }

    // 开启
    public void TurnOn()
    {
        if (_switchableLamp != null)
        { _switchableLamp.SwitchOn(); }
    }

    // 关闭
    public void TurnOff()
    {
        if (_switchableLamp != null)
        { _switchableLamp.SwitchOff(); }
    }
}

* User Operation


static void Main(string[] args)
{
    // 遥控器
    RemoteController remoteController;

    Console.WriteLine("使用遥控器控制 机械式式台灯:");

    // 建立机械式开关台灯
    ISwitchable switchableLight = new SwitchableLamp();

    // 注入遥控器欲操控之台灯实例 (符合ISwitchable界面)
    remoteController = new RemoteController(switchableLight);

    // 开启
    remoteController.TurnOn();

    // 关闭
    remoteController.TurnOff();


    Console.ReadKey();
}

Output

image

Then came the new touch table lamp.


interface ITouchable
{
	// 触碰式开启
    void TouchOn();

    // 触碰式关闭
    void TouchOff();
}

class TouchableLamp : ITouchable
{
    // 触碰式开启
    public void TouchOn()
    {
        Console.WriteLine("开灯(使用触碰式开关)");
    }

    // 触碰式关闭
    public void TouchOff()
    {
        Console.WriteLine("关灯(使用触碰式开关)");
    }
}

First problem is faced, RemoteController unable to practice non-injection table lamp ISwitchable interface , so we are unable to reach the owner needs to use the same remote to control a new type of touch table lamp. So I Heart will be implemented with the key interface converter (Adapter), so that the new touch table lamp is also included in the scope of remote support.

image

1. Establish adapter (SwitchableLampAdapter) [inherited desired target interface (ISwitchable)]


class SwitchableLampAdapter : ISwitchable
{
	// 机械式开启
    public void SwitchOn()
    {
        throw new NotImplementedException();
    }

    // 机械式关闭
    public void SwitchOff()
    {
        throw new NotImplementedException();
    }
}

2. Synthesis of the object through the wrapper way (wrap) is redirected by (ITouchable / TouchableLamp)


class SwitchableLampAdapter : ISwitchable
{
    // 支持转换"具有触碰式开关特性"对象
    ITouchable _touchableLamp;
    
    // 注入"具有触碰式开关特性"对象实例
    public SwitchableLampAdapter(ITouchable touchableLamp)
    {
        _touchableLamp = touchableLamp;
    }

    // 机械式开启
    public void SwitchOn()
    {
        throw new NotImplementedException();
    }

    // 机械式关闭
    public void SwitchOff()
    {
        throw new NotImplementedException();
    }
}

3. The method of transfer will be by (ITouchable / TouchableLamp) The method corresponds to the target interface (ISwitchable) required in practice


class SwitchableLampAdapter : ISwitchable
{
    // 支持转换"具有触碰式开关特性"对象
    ITouchable _touchableLamp;

    // 注入"具有触碰式开关特性"对象实例
    public SwitchableLampAdapter(ITouchable touchableLamp)
    {
        _touchableLamp = touchableLamp;
    }

    // 机械式开启
    public void SwitchOn()
    {
        // 触碰式开启
        _touchableLamp.TouchOn();
    }

    // 机械式关闭
    public void SwitchOff()
    {
        // 触碰式关闭
        _touchableLamp.TouchOff();
    }
}

4. The customer is not required to modify the program, conversion Adaptee Adapter directly, to the object through the operation Adaptee Itarget original interface.


static void Main(string[] args)
{
    // 遥控器
    RemoteController remoteController;
    
    Console.WriteLine("使用同一支遥控器控制触碰式台灯:");

    // 建立触碰式开关台灯
    ITouchable touchableLight = new TouchableLamp();

    // 透过转接器将触碰式开关台灯,转换为"假"的机械式开关台灯
    ISwitchable fackSwitchableLight = new SwitchableLampAdapter(touchableLight);

    // 注入遥控器欲操控之台灯实例 (符合ISwitchable界面)
    remoteController = new RemoteController(fackSwitchableLight);
    
    // 开启
    remoteController.TurnOn();

    // 关闭
    remoteController.TurnOff();


    Console.ReadKey();
}

Output

image

other apps

Teddy predecessors mentioned in the article, you can take advantage of the characteristics Adapter Pattern to do as a team to develop the border connector. Imagine a situation, the need API_B A team B team developed to process some data, then the B team has not yet completed API_B development, but the project is always to keep going, so the A team can put forward their own to continue the development of API_A activity. Wait for the B team API_B development is completed and then through the Adapter Pattern to referral API_A to API_B, this case is also an application of Pluggable Adapter.

Reference data

http://teddy-chen-tw.blogspot.tw/2013/05/clean-codeadapter.html


I hope this article can help to people in need

If the content is incorrect or there are other suggestions, please feel free to leave a message to the author Oh!

Original: Large column  [Design Pattern] Adapter Pattern


Guess you like

Origin www.cnblogs.com/chinatrump/p/11514347.html