Five min Understanding design mode (3)---adapter mode

Source: Internet
Author: User
Some examples of life

In our daily life, for the adapter, can be said to be everywhere. For example, we usually use the notebook computer, may be the operating voltage of the laptop is 20V, but our general household electricity is 220V, this time when the computer is charged with a power adapter to the 220V of the computer to convert to 20V voltage. This power adapter is also not called a charger or transformer.

Another example is when we want to charge Android's phone with an apple charging line. Because the interfaces of the two are different, they can cause the charging port not to match. At this point, we need the adapter to convert the Android charging interface into an Apple interface so that it can be recharged.

The two examples above are the adapter patterns that we're going to talk about today.

Adapter module definition

The adapter mode transforms the interface of one class into another interface that the client expects, so that two classes that would otherwise not work together because of an interface mismatch can work together.

In adapter mode, there are two different schema structures: The adapter mode of the class and the adapter mode of the object

The adapter mode of the object

For example, we use the above Apple charging line to charge the Android phone, if you are to write code, they are compatible with, how would you write?

For this problem, let's first look at the three roles involved in the adapter pattern:

1. Target abstract class: The target abstract class defines the interface required by the client , which can be an abstract class or interface, or it can be a concrete class. For example, for the second example above, the goal of the client is to charge an Android phone that only accepts a charging port, so our target abstract class is the interface of the Android charging cable .

2, Adaptee (Adaptive): The adaptation is the role of the adaptation, it defines an existing interface, the interface needs to be adapted, the adapter class is generally a specific class , including the customer wants to use the business method. For example, the above Apple charging line is the adapter class .

3, Adapter (Adapter Class): By wrapping a need to adapt the object, the original interface into the target interface. For example, in order to be able to recharge, we need an adapter that can connect an Android charging interface while connecting an apple charging cable.

UML diagram

Here we use code to do an example:

(1). Target class

public class Android {    public void isAndroid(){        System.out.println("这是一个只接受安卓充电线的插口");    }}

(2). Adaptee class

public class Iphone {    public void isIphone(){        System.out.println("这是一个适配苹果充电线的插口");    }}

(3). Adapter class: Adapting them to fit

/** * 适配器,作为中间件,把他们进行适配 */public class Adapter extends Android{    private Iphone iphone;    public Adapter(Iphone iphone){        this.iphone = iphone;    }    @Override    public void isAndroid() {        iphone.isIphone();    }}

(4). Test class

public class Demo {    public static void main(String[] args){        Android android = new Adapter(new Iphone());        //调用的是安卓的接口,但实际上        //确实一个可以接受苹果充电器的接口        android.isAndroid();    }}

(5). Print Results

这是一个适配苹果充电线的插口

With the help of the adaptor, we can get the Apple charging cable to charge the Android phone.

the adapter pattern for this object is actually a combination of the target class and the class that needs to be adapted through an adapter class. Therefore, the adapter class adapter generally need to inherit or implement Targert, and also have to hold adaptee instance references.

class's Adapter mode

In addition to the object's adapter mode, there is another class of adapter mode . In this mode, adapter does not hold the Adaptee instance reference, but inherits the Adaptee class directly, and then implements the target interface. Or directly inherit the Adaptee class and the target class, but because multiple inheritance is not supported in Java, it is only possible to implement the target.

This also leads to the adapter pattern in the Jaca class, where target must be an interface rather than an abstract class and a concrete class (because Java does not support multiple inheritance).

Rewrite The example above and use the class's adapter pattern to achieve:

(1). Target Interface class

interface Android {    void isAndroid();}

(2). Adaptee class

public class Iphone {    public void isIphone(){        System.out.println("这是一个适配苹果充电线的接口");    }}

(3). Adapter class: Inherit adaptee, achieve target

/** * 适配器,把安卓手机的插口转化为可以用苹果充电线充电的接口 */public class Adapter extends Iphone implements Android{    @Override    public void isAndroid() {        //直接调用        isIphone();    }}

(4). Test class

public class Demo {    public static void main(String[] args){        Android android = new Adapter();        android.isAndroid();    }}

(5). Print Results

这是一个适配苹果充电线的接口

For this class of adapter mode, there is less use in Java.

The most important differences between the two approaches are:

The object adapter pattern implements the adapter functionality through a combination, while the adapter mode of the class implements the adapter functionality through multiple inheritance or implementation .

Some advantages and disadvantages of adapter mode

Feel, pros and cons nothing to say, but actually to use to understand. Anyway, the most important advantage is that incompatible interfaces through an intermediate class, they are adapted, and this adaptation of the process for the client is transparent, the client does not know what happened, only know that it through an adapter, you can get to the target interface.

As for the disadvantage, if you use the adapter, then I think it will make the whole class system a bit fragmented, and later to refactor will be more complex ...

Finish

Pay attention to the public my number: hard yards of farmers , get more original articles, backstage reply gift pack to send you a list of popular resources in the present package. And thanks for introducing the article to more people who need it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.