Adapter mode and appearance mode for design mode

Source: Internet
Author: User

First, the adapter mode:

  Adapter Mode , simply say " to what mountain, sing what song ", that it solves is incompatible , mismatch problem. Let's start with a small example: what should we do when we want to plug a three-phase plug into a two-phase socket? Of course, to find a three-phase two-phase adapter socket, in this example, the role of the adapter socket is to change the interface of the socket to meet the needs of three-phase plug; again, in the OO adapter mode we're going to introduce, the adapter is to convert an interface into another interface to meet the needs of the customer .

Let's use the code to illustrate this example. We first put out the Plugoftwo and plugofthree two interfaces, indicating the two-phase socket and three-phase socket macro abstraction, the specific entity class are to achieve the corresponding interface (two-phase sockets and three-phase sockets have Plug method Plug and Start power supply method offer, but its working principle is different, So the name is also different). The code is as follows:

1  Public Interface plugoftwo {2     3      Public void plugwithtwo (); 4 5      Public void offerwithtwo (); 6 }
two-phase socket macro Interface
1  Public Interface Plugofthree {2     3      Public void Plugwiththree (); 4 5      Public void Offerwiththree (); 6 }
three-phase socket macro Interface

The following two classes are implementation classes of two interfaces, the code is as follows:

1  Public classTwoplugentityImplementsPlugoftwo {2 3      Public voidPlugwithtwo () {4System.out.println ("Two-phase receptacle solid is plugged in ...");5     }6 7      Public voidOfferwithtwo () {8System.out.println ("Two-phase receptacle body starts to power ...");9     }Ten}
Two-phase socket implementation class
1  Public classThreeplugentityImplementsPlugofthree {2 3      Public voidPlugwiththree () {4System.out.println ("three-phase receptacle solid is plugged in ...");5     }6 7      Public voidOfferwiththree () {8System.out.println ("three-phase receptacle entity starts to power ...");9     }Ten}
three-phase socket implementation class

Let's test the code below:

1  Public classMainClass {2     3      Public Static voidMain (string[] args) {4         //Test the operation of three-phase sockets5Threeplugentity three =Newthreeplugentity ();6 Three.plugwiththree ();7 Three.offerwiththree ();8         //Test the operation of two-phase sockets9TwoplugentityNewtwoplugentity ();Ten two.plugwithtwo (); One two.offerwithtwo (); A     } -}
Test Code

Test results

In the above example, our problem is that a three-phase plug should be plugged into a two-phase socket, so we need to add an adapter. The code for the adapter is as follows:

1  Public classTwotothreeadapterImplementsPlugofthree {2     PrivatePlugoftwo;3 4      PublicTwotothreeadapter (plugoftwo) {5          This. both =both ;6System.out.println ("The three-phase socket has been fitted to the two ...");7     }8 9      Public voidPlugwiththree () {Ten two.plugwithtwo (); One     } A  -      Public voidOfferwiththree () { - two.offerwithtwo (); the     } -}
the code of the adapter

The code for the second Test:

1  Public classMainClass {2     3      Public Static voidMain (string[] args) {4         //Test the operation of three-phase sockets5Threeplugentity three =Newthreeplugentity ();6 Three.plugwiththree ();7 Three.offerwiththree ();8         //Test the operation of two-phase sockets9TwoplugentityNewtwoplugentity ();Ten two.plugwithtwo (); One two.offerwithtwo (); A         //test the socket after adding the adapter work condition -Twotothreeadapter adapter =NewTwotothreeadapter (both); - Adapter.plugwiththree (); the Adapter.offerwiththree (); -     } -}
the code for the second Test

Test results

To be Continued ...

Adapter mode and appearance mode for design 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.