Java design mode (iii) adapter mode

Source: Internet
Author: User
Tags what interface

The source code is JDK1.8 as reference

1. Definition:
The interface of one class is transformed 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.
2. Analysis:
Adapter pattern, also known as wrapper pattern, refers to Adapter pattern, the first thing you might think of is the power adapter, for example, a 12V computer needs to change home with 220V AC power to use, you need to use AC adapter, Convert the power supply.
In the Java language, it is the ability to wrap or adapt an existing class to complete existing functionality.
General class Diagram:

class Diagram parsing:
2.1.Client characters
The application-specific role is the user after the adapter has been packaged.
2.2.Target Target Role
This role defines what interface to convert other classes to, that is, the interface we expect.
2.3.Adaptee Source Role
Usually an already existing, well-run, rigorously tested class that, after being packaged in the adapter role, becomes a new class with some new features attached.
2.4.Adapter Adapter Role
The core role of the adapter mode, the other two roles are already existing roles, and the adapter role needs to be new, its responsibility is very simple: convert the source role to the target role.
how the adapter mode is implemented:
1) class-oriented adapter mode
Adapter roles increase the ability of the source role to have target role conventions in an inherited manner
2) Object-oriented adapter mode
Adapter to hold object references so that the target role has a source role with additional functionality
Both of these methods can complete the encapsulation of the source role, but the implementation of the class-oriented approach has some limitations, because Java is not allowed to inherit more, so this adapter role can only complete the wrapper for a source role.
But object-oriented implementations, because the adapter role holds a source role reference, the adapter role can wrap multiple source roles to achieve the purpose of the target role requirements.
3. Specific application:
3.1.Client characters

publicclass Client {    publicstaticvoidmain(String[] args) {        // .. 原有的业务逻辑        new ConcreteTarget();        target.request();        // .. 现在增加了适配器角色后的业务逻辑        new Adapter();        target2.request();    }}

3.2.Target Target Role

publicinterface Target {    // .. 目标角色有自己的方法    publicvoidrequest();}publicclass ConcreteTarget implements Target {    publicvoidrequest() {        System.out.println("if you need any help,pls call me!");     }}

3.3.Adaptee Source Role
The source role here can already be in the running state, so it is unwise to modify the source role to achieve the purpose of the target role.

publicclass Adaptee {    // .. 原有的业务逻辑    publicvoiddoSomething(){        System.out.println("I‘m kind of busy,leave me alone,pls!");    }}

4.Adapter Adapter Role
Class-oriented adaptation mode

publicclass Adapter extends Adaptee implements Target {    publicvoidrequest() {        super.doSomething();    }}

Object-oriented adaptation mode

public   class  adapter  implements  target  { //: You can define references to multiple source roles at the same time to adapter wrap the purpose of multiple source roles   adaptee adaptee; public  adapter  (Adaptee adaptee)    {this . adaptee = adaptee; } public  void      Request  () {adaptee.dosomething (); }}

You can see that the conversion from Adaptee to target can be achieved through the adapter class, and you can add a specific implementation in adapter if target requires a new implementation. The class-oriented and object-oriented adaptation modes make the adapter pattern more flexible to use, and if complex adaptation is required, the adaptation of multiple source targets can be done just like an object-oriented implementation.
4. Application Scenario:
Adapter mode, in general, is not considered at the beginning of the design, the purpose of its existence is to be in the system, due to the design defects or the need to update the problem caused by the adapter to achieve compatibility before the version, and can make the current version of the normal operation of the means.
The adapter pattern allows two classes that have no relationship to run together, and because the original implementation is not changed, the reuse of the class is improved at design time, and the class is loosely coupled to the class, reducing the dependency between the class and the class. When the system does not require new features, it simply removes the adapter and does not have any effect on the original design.

Note: I am referring to the "design mode of Zen" and "design mode" two books learned, which added their own understanding of iterator design patterns, as well as the JDK in the source code understanding.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java design mode (iii) adapter mode

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.