Design Patterns - adapter mode (Object Adapter)

Public concern number JavaStorm get more exciting.

The interface of a class into a client interface to expect another, so that the two classes had to work together by an interface mismatch can not work together.

Scene analysis

For example, our mobile interface 6 millet phone only type c interface, while listening to music and for charging. Standard interfaces are mobile phones with a 3.5mm headphone jack, charging port type c. Now if we only 3.5mm headphones to listen to music on 6 millet phone, you need an adapter that will fit into our target interface 3.5mm headphone millet type c 6 in order to achieve songs.

Adapter pattern in life is still very common, such as the power adapter on your laptop, you can use varies between 110 ~ 220V power supply, while the notebook still work, and this is a manifestation of good adapter mode, simply put, adapter pattern is to convert an interface or class into another interface or class, on the other hand, the adapter mode is a mode of packaging, why? It Adaptee packaged into a Target class interface, add a layer of clothing, packed into the other liangniu. Decorator details see article history.

UML 类图

Here Insert Picture Description

  • target target role: This role is defined to be converted to other classes what the interface, the interface is what we expect, the example is our millet 6 type c interface.
  • Adapter that role: the central role of the Adapter pattern, two other roles are already present role, and the role of the adapter is a need to establish new, its role is very simple: to source role into target role, how the conversion? Reference related through inheritance or hold class. In the example is our type c interface converter.
  • Adaptee source role: Who you want to convert into a target role, the "who" is the source role, which is already existing, well-functioning class or object, the role of packaging through the adapter, it will become a new, beautiful role. In the example that our 3.5mm headphone jack interface.

Code combat

  1. First we defined the target target role. That is our millet 6 type c interface. And the phone default implementation.
/**
 * Project: com-zero-design-stu
 * File created at 2019/6/24 19:45
 */
package com.zero.headfirst.adapter.objects;

/**
 * 通过小米6手机的 type-c 接口听歌,也可以充电(Target 角色)
 * @date 2019/6/24 19:45
 */
public interface XiaoMi6Interface {
    /**
     * 听歌
     */
    void listenMusic();

}

And millet 6 type c default functions to achieve

/**
 * Project: com-zero-design-stu
 * File created at 2019/6/24 20:02
 */
package com.zero.headfirst.adapter.objects;

/**
 * 小米六 type c 接口默认实现功能
 * @date 2019/6/24 20:02
 */
public class XiaoMi6InterfaceImpl implements XiaoMi6Interface {
    @Override
    public void listenMusic() {
        System.out.println("通过 type c 接口的耳机听歌");
    }
}

  1. Adaptee define our role, which is our 3.5mm headphone interface.
package com.zero.headfirst.adapter.objects;

/**
 * 普通手机通用 接口 ,对应的3.5mm 耳机孔
 * @date 2019/6/25 16:28
 */
public interface CommonPhoneInterface {
    /**
     * 听歌
     */
    void listenMusic();

}

public class CommonPhoneImpl implements CommonPhoneInterface {
    @Override
    public void listenMusic() {
        System.out.println("标准手机 3.5 mm 耳机接口听歌");
    }

}
  1. Then we define an interface converter, which is the role adapters Adapter. Here we use the object adapter mode, which is adapted to hold a reference to the adapter. While achieving the target interface.
public class TypeC2HeadphoneAdapter implements XiaoMi6Interface {

    private CommonPhoneInterface commonPhoneInterface;

    public TypeC2HeadphoneAdapter(CommonPhoneInterface commonPhoneInterface) {
        this.commonPhoneInterface = commonPhoneInterface;
    }

    @Override
    public void listenMusic() {
        System.out.println("通过 type c 转 3.5mm适配器将目标 type c 接口适配上3.5mm接口。");
        commonPhoneInterface.listenMusic();
    }

}
  1. test
public class Client {
    public static void main(String[] args) {
        //被适配者 3.5毫米耳机
        CommonPhoneInterface headPhone = new CommonPhoneImpl();
        //适配器
        TypeC2HeadphoneAdapter typeC2HeadphoneAdapter = new TypeC2HeadphoneAdapter(headPhone);
        //通过适配器实现了听歌
        typeC2HeadphoneAdapter.listenMusic();
    }
}

Print results

通过 type c 转 3.5mm适配器将目标 type c 接口适配上3.5mm接口。
标准手机 3.5 mm 耳机接口听歌

to sum up

When we can not modify the interface of the target role, we use an interface adapted to convert you can use the adapter mode.

Decorator pattern is focused on the same interface enhancements and new features multiplexing.

Learn more concerned about the public number.
Here Insert Picture Description

Published 28 original articles · won praise 2 · Views 1469

Guess you like

Origin blog.csdn.net/qq_14855971/article/details/93631545