The adapter mode for Java design mode

Source: Internet
Author: User

Speaking of adapters, we may think of the computer adapter, yes, the function is the same, the computer adapter can be used in China, can be used in the United States, its main role is to adapt between the new interface and the old interface. This is an adaptation process, with the class diagram for the adapter pattern as follows:

This class diagram is I cut in other places, I used to think the class diagram is particularly unattractive, and after a slow contact to find that the class diagram and flowchart are particularly meaningful, a look is particularly clear. The whole process is to convert the adaptee toa subclass of target by Adapter . Now that we know the principle, let's start the code below. Let me give an example of a scene, often need to use the projector, but some people's computer does not support the projector's big mouth, only use the adapter. That is, you can turn a small mouth into a big mouth.

 Package Com.roc.adapter; /**  @author*/Publicinterface  bigport        {  publicvoid userbigport (); // use the big Mouth }

 Package Com.roc.adapter; /**  @author*/Publicinterface  smallport    {  publicvoid usersmallport (); // use small Mouth }
 PackageCom.roc.adapter;/*** Adapter Mode *@authorLIAOWP **/ Public classSmalltobigImplementsbigport{PrivateSmallport Smallport;//Small Mouth         PublicSmalltobig (Smallport smallport) {//Get Small Mouth         This. smallport=Smallport; }     Public voidUserbigport () { This. Smallport.usersmallport ();//Use small mouth    }            }
 PackageCom.roc.adapter;/*** Client *@authorLIAOWP **/ Public classClient { Public Static voidMain (string[] args) {Smallport smallport=NewSmallport () {//The computer comes with a small mouth             Public voidUsersmallport () {System.out.println ("The use of a small computer mouth");        }        }; //need a big eloquence can be projected, small mouth converted to large mouthBigport bigport=NewSmalltobig (Smallport); Bigport.userbigport ();//in the computer small mouth work realizes the suitable match    }    }

Here are the advantages and disadvantages of the adapter:

Advantages:

1. Decoupling the target class from the appropriate ligand class

2, increase the transparency and reusability of the class, the specific implementation encapsulated in the adapter class, for the client class is transparent, and improve the reusability of the appropriate ligand

3, flexibility and extensibility are very good, in line with the opening and shutting principle

Class adapters have some advantages:

1, because the adapter class is a subclass of the adaptive class, it is possible to replace some adapters in the class of the method, making the adapter more flexible.

Disadvantages of class Adapters:

1, for Java, C # and other languages do not support multiple inheritance, a maximum of one adapter class at a time, and the target abstract class can only be an interface, can not be a class, its use has certain limitations, can not be an adapter class and his sub-class at the same time to adapt to the target interface.

The object adapter has some advantages:

1, to adapt a number of different adapters to the same target, that is, the same adapter can be the appropriate ligand class and his sub-class to adapt to the target interface.

Disadvantages of the object adapter:

1, compared with the class adapter mode, it is not easy to replace the method of the matching class.

The difference between adapter mode and proxy mode:

Adapter mode

is to convert 2 different things to normal use, the adapter is to cater to the target object with the source object.

Proxy mode

is 2 objects have the same function, 2 interface is the same.

The adapter mode for Java 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.