Adapter mode in software design mode

Source: Internet
Author: User

What is the adapter mode?

In computer programming, the adapter mode (sometimes called packaging style or packaging) adapts a class interface to what the user expects. The adapter can work together classes that cannot work together because of the incompatibility of interfaces, by encapsulating their own interfaces in an existing class.

 

How does one implement the adapter?

① Object Adapter mode-in this adapter mode, the adapter holds an instance of the class it wraps. In this case, the adapter calls the physical entity of the wrapped object. ② Class adapter mode-in this adapter mode, the adapter inherits its own implemented classes (generally multiple inheritance ). Theoretical things seem to be annoying. Let's take a small daily example to better understand the adapter. Figure 1: Three-phase connector Diagram 2: Two-hole socket diagram 3: Three-phase connector to two-phase converterAfter reading the above three figures, I think everyone should be clear at a glance. If you want to insert the three-phase plug into the two-hole socket, you can only add a converter in the middle, here we can simply understand it as the adapter we are going to talk about today. Let's look at this article. "The adapter can work together classes that cannot work together because the interfaces are incompatible. ", yes, you can associate them by adding an adapter between two components that cannot work together. There are three roles in the adapter mode: 1. Target role (target): that is, the interface desired by the customer, such as the three-phase plug, the jack that the customer wants is the three-hole socket (target role ). 2. Source (adatee): The existing interface, that is, the interface that does not meet the customer's needs, that is, the two-hole socket (source ). 3. Adapter: the adapter can be used to convert a two-hole socket into a "three-hole socket" component, that is, the three-phase plug to the two-phase plug converter (adapter ).For the above example, let's talk about the adapter mode in code first. Object Adapter Mode
Take the notebook power-on as an example. We all know that the normal notebook power supply is a three-phase interface threesocket. Java, which is a requirement interface that the customer expects, that is, the target interface (target) in the three roles)
1 package COM. LCW. adapter. test; 2 3 Public interface threesocket {4 5 // a three-hole socket is required, that is, the customer needs 6 Public void needthreesocket (); 7 8}

 

Twosocket. Java

This is an existing component, that is, the two-hole socket, that is, the source (adatee) among the three roles. Although it can communicate with electricity, the interface does not meet the customer's needs.

1 package COM. LCW. adapter. test; 2 3 Public class twosocket {4 // The existing socket has only two holes. The power supply function is available, but the three-hole interface 5 Public void servertwo () {6 system is missing. out. println ("I Am a two-hole socket, I have power supply"); 7} 8 9}

 

Threetotwoadapter. Java

This is an adapter class. It requires 2-hole sockets to provide electricity and implement 3-hole sockets. In the constructor method, the object of the existing component 2-hole socket (charged) is introduced)

1 package COM. LCW. adapter. test; 2 3 Public class threetotwoadapter implements threesocket {4 private twosocket socket; 5 Public threetotwoadapter (twosocket two) {6 this. socket = two; 7} 8 // now there are 2-hole socket objects (meaning power-on feature) 9 @ override10 public void needthreesocket () {11 socket. servertwo (); 12 system. out. println ("two-hole interface has been converted into three-hole interface and injected with electricity"); 13} 14 15}

In this way, the intermediate class of the adapter class has the effect of the source (two-hole socket), that is, "electricity"

Then the target interface method is implemented, that is, the three-phase interface is activated to inject "electricity", so that two classes that cannot work together can be combined, like the matchmaker, there is a link between them.

 

Computer. Java notebook class, which requires a three-hole Interface

1 package COM. LCW. adapter. test; 2 3 Public class computer {4 // The power plug requirement for the laptop is 3 holes 5 private threesocket three; 6 Public Computer (threesocket three) {7 This. three = three; 8} 9 Public void server () {10 system. out. println ("laptop powered! "); 11} 12 13}

 

To write a test class

1 package COM. LCW. adapter. test; 2 3 Public class test {4 public static void main (string [] ARGs) {5 // the existing two-hole socket has provided the power function, only 3-hole interface 6 twosocket two = new twosocket () is missing; 7 // The object with power-on function is passed in, the purpose is to activate the power-on function of the 3-hole interface 8 threesocket three = new threetotwoadapter (two ); 9 // obtain a function object that satisfies the notebook Power interface and has power 10 computer = new computer (three); 11 three. needthreesocket (); 12 computer. server (); 13} 14}

See:

Now, we can link two classes that cannot work together.

 

Next let's talk aboutClass adapter Mode

The difference between class adapters is that, since the 'class' is highlighted, we generally think of inheritance.

Well, that's right. The method of class adapter is to let the adapter inherit the existing function (two-hole socket) class, and then implement the expected interface (three-hole socket)

For more information, see the code. Except for the test class and adapter class, the other classes are consistent with the above ones, so we will not post them here.

Threetotwoextendsadapter. Java (adapter class)

1 package COM. LCW. adapter. test; 2 3 Public class threetotwoextendsadapter extends twosocket implements threesocket {4 Public void needthreesocket () {5 this. servertwo (); 6 system. out. println ("two-hole interface has been converted to three-hole interface"); 7} 8 9}

Test. Java (test class)

1 package COM. LCW. adapter. test; 2 3 Public class test {4 public static void main (string [] ARGs) {5 // because threetotwoextendsadapter inherits the source, it has the source function (electricity ), 3-hole interface 6 threesocket socket = new threetotwoextendsadapter (); 7 // insert interface 8 computer = new computer (socket); 9 socket. needthreesocket (); 10 computer. server (); 11 12} 13}

Effect:

 

 

Summary:

① Object Adaptation Mode:

The "adapter" is combined into the adapter as an object to modify the Target Interface and encapsulate the adapter.

(For example, in the above example, the two-hole socket is the adapter, which transfers the object to the adapter and implements interface method transformation)

 

② Adaptive Mode:

Through multi-inheritance and interface implementation, a single adaptation for a class is realized.

 

What is the role of the adapter?

1. Transparency: the adapter client can call a unified interface, which is transparent to the client. This can be more concise, direct, and compact.

2. Reuse: existing classes are reused to solve inconsistent environmental requirements.

3. Low coupling: decouples the target class from the source class, introduces an adapter to reuse the source class (the adapter class), and complies with the open-close principle.

Adapter mode in software design mode

Related Article

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.