The adapter mode for Java design mode

Source: Internet
Author: User

Defined:

The adapter pattern transforms the interface of a class into another interface that the customer expects. Those classes that would otherwise not work together because of incompatible interfaces can work together.

Categories of adapters:

      1. Combination: Adapter in a combined way becomes an object adapter feature: Combine the "ligand" as an object into the adapter class to modify the target interface wrapper by the appropriate person

2. Inheritance: The use of inheritance is called Class adapter features: Through multiple inheritance incompatible interface, to achieve the matching of the target interface, a single class to achieve a suitable

Role:

1. Transparent: Through the adapter, the client can invoke the same interface, and thus is transparent to the client. This is simpler, more straightforward, and more compact.

2. Reuse: Reusing existing classes solves the problem of inconsistent requirements for existing and reused environments.

3. Low coupling: Decoupling the target class from the adapter class and reusing existing adapters by introducing an adapter without modifying the original code (following the open and closed principle)

Example project: A laptop requires a three-phase socket charge, but there is a two-phase power supply, you need to create an adapter to do two three-phase charging

code example

1. Create a three-phase receptacle interface

1 /** 2 * Three-phase socket interface 3  */4publicinterface  threeplugif {5     / / use three-phase current to charge 6public     void  powerwiththree (); 7 8 }
View Code

2. Create a two-phase power supply class

1  Public class Gbtowplug {2     // power supply with two-phase current 3      Public void Powerwithtow () {4         SYSTEM.OUT.PRINTLN ("power supply using two-phase current"); 5     }6 }
View Code

3. Create a notebook class that uses three-phase sockets for charging, but the power supply is two-phase and requires an adapter for conversion

1  Public classNoteBook {2     Privatethreeplugif plug;3      PublicNoteBook (threeplugif plug) {4          This. Plug =plug; 5     }6     //charging with a socket7      Public voidcharge () {8 Plug.powerwiththree ();9         Ten     } One      A}
View Code

4-1. Create an adapter (combo)

1 /**2 * Two phase to three-phase socket adapter3  * */4  Public classTowplugadapterImplementsThreeplugif {5     PrivateGbtowplug plug;6      Publictowplugadapter (gbtowplug plug) {7          This. Plug =plug;8     } 9 @OverrideTen      Public voidPowerwiththree () { OneSystem.out.println ("Through conversion"); A Plug.powerwithtow (); -     } -  the}
View Code

4-2. Create an adapter (inherit)

1 /**2 * Adopt the Inheritance method to realize3  * */4  Public classTowplugadapterextends5     extendsGbtowplugImplementsThreeplugif {6 7 @Override8      Public voidPowerwiththree () {9System.out.println ("Inheritance mode implementation");Ten          This. Powerwithtow (); One     } A}
View Code

5. Test class

1  Public classTest {2      Public Static voidMain (string[] args) {3         /**Combination Mode*/4Gbtowplug tow =NewGbtowplug ();5Threeplugif three =NewTowplugadapter (tow);6NoteBook NB =NewNoteBook (three);7 Nb.charge ();8         /**How to inherit*/9three =Newtowplugadapterextends ();TenNB =NewNoteBook (three); One Nb.charge (); A     } -}
View Code

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.