Java Mode-Adapter mode

Source: Internet
Author: User

Adapter mode (Adapter):

1. Concept: Convert the interface in one class into another interface that the customer wants, so that those classes that cannot work together because of incompatible interfaces can work together.

2. Role in the pattern

1 target interface: the interface that the customer expects. The target can be a specific or abstract class, or it can be an interface.

2 classes that need to be adapted: Classes or adapter classes that need to be fitted.

3 Adapter: The original interface is converted to the target interface by wrapping an object that needs to be adapted.

3. Realization method

Adapter pattern for Class 1 (implemented with inheritance)

2 Object Adapter (implemented by object combination)

4 Code

1. Class adapters, adapter classes that inherit both the adaptive class and the standard interface

1 //Existing classes that have special features but do not conform to our existing standard interfaces2 classAdaptee {3      Public voidspecificrequest () {4System.out.println ("Classes that need to be adapted"); 5     }  6 }  7   8 //Target Interface9 InterfaceTarget {Ten      Public voidrequest ();  One }   A    - //specific target class, only provide normal function - classConcretetargetImplementsTarget { the      Public voidrequest () { -System.out.println ("The original existence of the class");  -     }   - }   +     - //adapter class, inherited by the appropriate class, while implementing the standard interface + classAdapterextendsAdapteeImplementstarget{ A      Public voidrequest () { at         Super. Specificrequest ();  -     }   - }   -     - //Test class
- Public Static voidMain (string[] args) { in //using normal function classes -Target Concretetarget =NewConcretetarget (); to concretetarget.request (); + - //use special function classes, i.e. adaptation classes theTarget adapter =NewAdapter (); * adapter.request (); $ } Panax Notoginseng}

2 Object Adapter

1  Public classTest12 {2      Public Static voidMain (string[] args) {3         //how the original interface is called4Powera a=NewPoweraimpl ();5 input (a);6         7Powerb b=NewPowerbimpl ();8         //input (B); not available because the input method can only accept Powera interfaces9Poweradapter adapter=NewPoweradapter (b);Ten input (adapter); One     } A     //the system's methods are incompatible with the B interface, but need to continue to use -      Public Static voidinput (Powera a) { - A.connect (); the     } - } - //adapter, can be fitted with a power adapter - classPoweradapterImplementspowera{ +     //directly associated with the interface being adapted -    PrivatePowerb B; +     PublicPoweradapter (Powerb b) { A         This. b=b; at         -    } -    //method to invoke interface B in the adapter - @Override -      Public voidConnect () { - B.insert ();  in     } -      to } +  - //interface B that needs to be adapted the Interfacepowerb{ *      Public voidInsert (); $ }Panax Notoginseng classPowerbimplImplementspowerb{ -  the @Override +      Public voidInsert () { ASystem.out.println ("Power B interface starts working");  the     } +      - } $  $ //power supply A interface, System original interface - Interfacepowera{ -      Public voidConnect (); the } - classPoweraimplImplementspowera{Wuyi  the @Override -      Public voidConnect () { WuSystem.out.println ("Power A interface starts working");  -     } About      $}

5 Summary
(1) The way that class adapters use object inheritance is statically defined, and object adapters use object composition in a dynamic combination of ways.

(2) for class adapters , because the adapter inherits Adaptee directly, the adapter cannot work with adaptee subclasses, because inheritance is a static relationship, and when the adapter inherits Adaptee, it is impossible to process the subclass of Adaptee.

     for object adapters , an adapter can adapt many different sources to the same target. In other words, the same adapter can adapt the source class and its subclasses to the target interface. Because the object adapter is a combination of objects, it doesn't matter if the object type is correct.

(3) for class adapters , the adapter can redefine part of the behavior of Adaptee, which is equivalent to a partial implementation of the parent class that the subclass overrides.

for object adapters , it is difficult to redefine the behavior of Adaptee, in which case you need to define a subclass of Adaptee to implement the redefinition and then have the adapter group the subclasses. Although it is difficult to redefine the behavior of adaptee, it is convenient to add some new behaviors, and the newly added behavior can be applied to all sources at the same time.

(4) for class adapters , only one object is introduced, and no additional references are required to indirectly obtain the adaptee.

for object Adapters , additional references are required to get adaptee indirectly.

It is recommended to use the implementation of object adapters as much as possible, using compositing/aggregation and less inheritance. Of course, specific problems specific analysis, according to the need to choose the implementation, the most suitable is the best.

Benefits of Adapter mode

Better reusability

The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system. The adapter mode allows for better reuse of these features.

Better extensibility

When implementing the adapter functionality, you can invoke the features you have developed to naturally extend the functionality of the system.

Disadvantages of Adapter mode

Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. For example, clearly see the call is a interface, in fact, the interior is adapted to the implementation of the B interface, a system if too many occurrences of this situation, is tantamount to a disaster. So if it's not necessary, you can refactor the system without using the adapter.

Java Mode-Adapter 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.