Java Design Patterns (learning grooming)---iterative patterns

Source: Internet
Author: User

Adapter of design mode (adapter)

1. Definition :
The use of two incompatible classes jiuge in a structured pattern requires two identities for adaptee (the adapter) and adaptor (adapters).

2. Why use ?
We often encounter the need to combine two classes that are not related to each other, the first solution is to modify the interfaces of the respective classes, but if we do not have the source code, or we do not want to modify the respective interfaces for an application. What to do?

Using Adapter, create a hybrid interface (half-breed) between the two interfaces.

3, how to use ?
Implementation of the adapter way, in fact, "think in Java," The "Class regeneration" section has been mentioned, there are two ways: combination (composition) and Inheritance (inheritance).

Suppose we want to pile, there are two kinds: square pile round pile.

1  Public classsquarepeg{2      Public voidInsert (String str) {3System.out.println ("Squarepeg insert ():" +str);4 }5 6 }7 8  Public classroundpeg{9      Public voidinsertintohole (String msg) {TenSystem.out.println ("Roundpeg insertintohole ():" +msg); One } A} 

Now there is an application that requires both square piles and round piles. Then we need to combine the two non-relational classes. Suppose Roundpeg we don't have source code, or source code we don't want to modify, then we use adapter to implement this application:

 1  public  class  Pegadapter extends   squarepeg{ 2  private   Roundpeg Roundpeg;  4   Public  Pegadapter (roundpeg peg) (peg;)  6   Public  void   insert (String str) { Roundpeg.insertintohole (str);}  8  9 } 

In the above code , Roundpeg belongs to Adaptee, which is the appropriate ligand. The Pegadapter is adapter, adapting Adaptee (the adapter roundpeg) and Target (squarepeg). This is actually the combination method (composition) and Inheritance (inheritance) Methods are used comprehensively.

Pegadapter first Inherits Squarepeg, then uses the combination of new to generate the object, generates Roundpeg object Roundpeg, and then overloads the parent class insert () method. From here, you also understand that using new to build objects differs from building objects using extends inheritance, which does not need to be modified for the original class, or even need to know its internal structure and source code.

If you have some experience with Java use, it has been found that this pattern is often used.

4. Further use
The above Pegadapter is inherited squarepeg, if we need to inherit from both sides, that is, inherit Squarepeg and inherit Roundpeg, because Java does not allow multiple inheritance, but we can implement (implements) two interfaces (interface )

 1  public  interface     iroundpeg{ 2  public  void   Insertintohole (String msg);  3  }  5  6  public  interface     isquarepeg{ 7  public  void   insert (String str);  8  9 } 

The following is the new roundpeg and Squarepeg, in addition to implementing the interface of the difference, and the above is no different.

1  Public classSquarepegImplementsisquarepeg{2      Public voidInsert (String str) {3System.out.println ("Squarepeg insert ():" +str);4 }5 6 }7 8  Public classRoundpegImplementsiroundpeg{9      Public voidinsertintohole (String msg) {TenSystem.out.println ("Roundpeg insertintohole ():" +msg); One } A}

Here is the new Pegadapter, called Two-way adapter:

1  Public classPegadapterImplementsiroundpeg,isquarepeg{2 3     Privateroundpeg roundpeg;4     Privatesquarepeg squarepeg;5 6     //Construction Method7      PublicPegadapter (Roundpeg peg) { This. roundpeg=peg;}8     //Construction Method9      PublicPegadapter (Squarepeg Peg) ( This. squarepeg=peg;)Ten  One      Public voidInsert (String str) {roundpeg.insertintohole (str);} A  -}

  

There is also a kind of called pluggable adapters, can dynamically get a few adapters in one. Using reflection technology, you can dynamically discover public methods in a class.

Java Design Patterns (learning grooming)---iterative patterns

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.