Design Mode-adapter Mode)

Source: Internet
Author: User
Basic Concepts

The adapter mode converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together.

 

In the gof design mode, there are two types of adapter modes: Class adapter mode and Object Adapter mode. There is also a special case of the adapter mode, that is, the default adapter, also known as the interface adaptation mode.

1. Class adapter Mode

Converts an adaptation class API to an API of the target class. Because the class adapter mode matches one interface with another through multi-inheritance, although C #, Java and other languages do not support multiple inheritance, you can also simply use the class adapter mode.

2. Object Adapter Mode

Like the adapter mode of the class, the difference is that the adapter mode of the object is not to connect to the adaptee class by using an inheritance relationship, but to connect to the adaptee class by using an association relationship.

3. Default adapter mode, also known as interface adapter Mode

The default adaptation mode provides a default implementation for an interface, so that the type can be extended from this default implementation without the need to extend from the original interface. As a special case of the adapter mode, the default adaptation mode has special applications in the Java language.


Problems

So that classes that cannot be combined because of interface incompatibility can work together.


Class adapter Mode

We use an example to learn the adapter mode: in daily life, the plug of the laptop is generally three-phase, that is, in addition to the anode, cathode, there is a ground pole. In some places, the power outlet is only two poles without a ground pole. The power plug of the power outlet does not match the power plug of the laptop so that the laptop cannot be used. At this time, a three-phase to two-phase converter (adapter) can solve this problem, and this is like what this mode does.


Class diagram:



Source code

Core Idea: There is an adaptee class that has a method getreceptacle1 (), to be adapted, the target interface target, through the adapter class, extends the adaptee function to the target, read the code:


Adaptee class, the class to be adapted

Public class adaptee {/*** get socket */Public void getreceptacle1 () {system. Out. println ("this is a two-hole outlet! ");}}

Target class, Target Interface

Public interface target {/*** get two-hole socket */Public void getreceptacle1 ();/*** get three-hole socket */Public void getreceptacle2 ();}

Adapter class

/*** This type of goal is to enable the computer to use 2 empty sockets, solve the interface mismatch problem, and expand the function. * @ Author zsl */public class adapter extends adaptee implements target {@ overridepublic void getreceptacle2 () {system. Out. println ("extension plug-in, this is a three-hole plug! ");}}

Test class:

/*** Test class ** @ author zsl */public class computer {public static void main (string [] ARGs) {Target target = new adapter (); target. getreceptacle1 (); target. getreceptacle2 (); // This function is the extended feature of the adapter }}



Object Adapter Mode

Like the adapter mode of the class, the difference is that the adapter mode of the object does not connect to the adaptee class by using an inheritance relationship, but connects to the adaptee class by using an association relationship. The class diagram is as follows:


As you can see, the adaptee class does not have the getreceptacle2 () method, and the client expects this method. To enable the client to use the adaptee class, a wrapper adapter is required. This packaging class encapsulates an adaptee instance, so that this packaging class can link the adaptee API with the target class API. The adapter and adaptee are dependent, which determines that the adapter mode is object.


Source code

Compared with the class adapter mode, you need to modify the adapter class.

Adapter class

/*** Object Adapter mode, * @ author administrator **/public class adapter2 implements target {private adaptee; Public adapter2 (adaptee) {This. adaptee = adaptee;}/*** there is a method for the class to be adapted. You can directly use the adapter */@ overridepublic void getreceptacle1 () {This. adaptee. getreceptacle1 ();}/*** this method is not available for the adaptive class. The adapter supplements */@ overridepublic void getreceptacle2 () {system. out. println ("Expansion plug-in, this is a three-hole plug-in! ");}}

Test class:

/*** Test class ** @ author zsl */public class computer {public static void main (string [] ARGs) {adaptee = new adaptee (); target target = new adapter2 (adaptee); target. getreceptacle1 (); target. getreceptacle2 (); // This function is the extended feature of the adapter }}

Interface adapter Mode

The default adapter mode provides a default implementation for an interface, so that the type can be extended from this default implementation instead of the original interface. As a special case of the adapter mode, the default adaptation mode has special applications in the Java language.

Sometimes an interface we write has multiple abstract methods. When we write the implementation class of this interface, all methods of this interface must be implemented, which is obviously a waste of time, because not all methods are needed, sometimes we only need some. To solve this problem, we introduce the adapter mode of the interface, with the help of an abstract class, this abstract class implements this interface and implements all the methods. Instead of dealing with the original interface, we only get in touch with this abstract class. Therefore, we write a class that inherits this abstract class, rewrite the method we need.

 

(Source: http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html)

What do monks do? Eat fast, read Sutra, sit, hit clock, Xi Wu, etc. If you design a monk interface and provide all methods that need to be implemented, the interface should be as follows:

Public interface monk {public void chuzhai (); Public void chanting (); Public void meditation (); Public void hitting clock (); Public void Xi Wu (); public String getname ();}

Obviously, all Monk classes should implement all the methods defined by interfaces, otherwise they will not be able to use the Java editor. Similar to the following Lu zhishen class.

Public class Lu Zhi-Shen implements monk {public void Xi Wu () {boxing town Kansai; Wutai Mountains; Maha peach blossom village; burning waguan temple; pulling down willow;} Public String getname () {return "Lu zhishen ";}}

Lu zhishen only implements the getname () and XI Wu () methods, but does not implement any other methods. Therefore, it does not pass through the Java compiler. Lu zhishen class can only use Java compiler to implement all methods of monk interface, but Lu zhishen is no longer Lu zhishen. Learn from history and know the world. Studying how Lu zhishen shaved a Monk several hundred years ago will inspire Java programming. Good, when Ruda shaves, the monk said: "This person describes ugliness, looks stubborn, do not shave him", but the elders said: "This person is straight to the stars and heart. Although the current fierce, hit complex, but after a long time but clean. The evidence is remarkable, and you are inferior to him ."

So it turns out! It seems that the problem will be solved as long as a star is used here! If you use an object-oriented language, you can implement it as well. If you are a "Star", you can also use an abstract class.

Public abstract class tianxing implements monk {public void eat Zhai () {} public void read Sutra () {} public void SIT () {} public void hit clock () {} public void Xi Wu () {} Public String getname () {return NULL ;}}

Lu zhishen inherits the abstract class "tianxing"

Public class Lu zhishen extends tianxing {public void Xi Wu () {boxing town Kansai; Wutai Mountain; huanao peach blossom village; burning waguan temple; pulling down Yangliu;} Public String getname () {return "Lu zhishen ";}}

The abstract star category is an adapter class, and Lu zhishen actually achieved the purpose of shaving by means of the adapter mode. This adapter class implements all the methods required by the monk interface. However, unlike the usual adapter mode, the implementation of all the methods provided by this adapter class is "mediocre. This "mediocre" adapter mode is called the default adaptation mode.

In many cases, a specific class must implement an interface, but this class does not use all the methods specified by the interface. The common solution is to implement all methods in this specific class, implement useful methods, and implement idle and mediocre methods.

These empty methods are a waste and sometimes a mess. Unless you have read the code for these empty methods, the programmer may think that these methods are not empty. Even if he knows that some of the methods are empty, he does not necessarily know which methods are empty and which are not empty unless he has read the source code or documentation of these methods.

The default adaptation mode can handle this situation well. You can design an abstract adapter class to implement the interface. This abstract class provides an empty method for each method required by the interface. Just like Lu zhishen's "Shang Ying tianxing", this abstract class can save its specific subclass from being forced to implement null methods.


Comparison between class adapters and object adapters

Class Adapter Object Adapter
Object Inheritance is a static definition method. Object combination is a dynamic combination.
The adapter can redefine some behavior of adaptee, which is equivalent to partial implementation of sub-classes covering the parent class. It is difficult to redefine the behavior of adaptee. In this case, you need to define a subclass of adaptee to implement redefinition, and then let the adapter combine subclasses. Although it is difficult to redefine adaptee behavior, it is convenient to add some new behavior, and the newly added behavior can be applied to all sources at the same time.
Only one object is introduced, and no additional reference is required to indirectly obtain adaptee. An additional reference is required to indirectly obtain adaptee.

Adapter advantages

1. Better reusability

The system needs to use existing classes, and such interfaces do not meet the requirements of the system. The adapter mode enables better reuse of these functions.

2. better scalability

When implementing the adapter function, you can call your own developed functions to naturally expand the system functions.


Adapter disadvantages

Using too many adapters will make the system messy and difficult to grasp as a whole. For example, we can see that the called a interface is actually adapted to the implementation of B interface internally. If there are too many systems, this would be a disaster. Therefore, if not necessary, you can directly refactor the system without using the adapter.


(Original address: http://blog.csdn.net/zhshulin)

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.