Java Design Pattern Rookie series (eight) modeling and implementation of adapter mode

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39805069


Adapter Mode (Adapter): Converts the interface of a class to another interface that the client expects to represent, in order to eliminate compatibility issues with classes caused by an interface mismatch.
There are three main categories: The adapter mode of the class, the adapter mode of the object, and the adapter mode of the interface.

First, the class of adapter mode 1, UML modeling:


2. Code implementation

/** * Example (i): Adapter mode for class *  * The original class has a method to be adapted Originmethod */class Original {public void Originmethod () {System.out.println (" This is a original method ... ");} Interface Targetable {/** * is the same method as the original class */public void Originmethod ();/** * method of the target class */public void Targetmethod ();} /** * The purpose of the Adapter class: To adapt the Original class to the Targetable interface */class Adapter extends Original implements targetable {/** * You can see that the class only needs to implement TA Rgetmethod can be. *  * Because the Originmethod method in the Targetable interface has been implemented by original. *  This is the benefit of the adapter adapter class: The transfer of the method implementation (or graft)--and the transfer of adapter responsibility to original *  * This implements the class adapter mode-- Adapting the original class to the Targetable interface *  * If original adds a new method ORIGINMETHOD2, simply declare it in the Targetable interface. */@Overridepublic void Targetmethod () {System.out.println ("This is a target method ...");} /** * Client Test class *  * @author Leo */public class Test {public static void main (string[] args) {targetable target = new Adap ter (); Target.originmethod (); Target.targetmethod ();}}

Second, the adapter mode of the object

1. UML Modeling:



2, the Code implementation:

/** * Example (ii): The adapter mode of the object *  * The original class has a method to fit Originmethod */class Original {public void Originmethod () {System.out.println (" This is a original method ... ");} Interface Targetable {/** * is the same method as the original class */public void Originmethod ();/** * method of the target class */public void Targetmethod ();} /** * Holds instances of Original class */class Adapter implements targetable {private Original original;public Adapter (Original Original) {T His.original = original;} @Overridepublic void Targetmethod () {System.out.println ("This is a target method ..."); @Overridepublic void Originmethod () {Original.originmethod ();}} /** * Client Test class *  * @author Leo */public class Test {public static void main (string[] args) {Original Original = new Orig Inal (); targetable target = new Adapter (original); Target.originmethod (); Target.targetmethod ();}}

Third, the adapter mode of the interface

1. UML Modeling:



2. Code implementation

/** * Example (c): Interface Adapter Mode * * This time we'll just make an interface--the original interface */interface originable {public void originMethod1 ();p ublic void orig INMETHOD2 ();} /** * This abstract class implements the original interface and implements all the methods. * * Empty implementation can, the specific implementation of sub-class, sub-class only need to implement their own methods. * * We won't have to deal with the original interface, just get in touch with the abstract class. */abstract class Adapter implements originable {public void originMethod1 () {}public void OriginMethod2 () {}}/** * subclass just select you The required method can be implemented */class OriginSub1 extends Adapter {@Overridepublic void OriginMethod1 () {System.out.println ("This is Originable interface ' s first sub1 ... "); /** * At this time: OriginMethod2 method default NULL implementation */}class ORIGINSUB2 extends Adapter {/** * at this time: OriginMethod1 method default NULL implementation */@Overridepublic void or IginMethod2 () {System.out.println ("This is Originable interface ' s second sub2 ...");}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {originable origin1 = new Origin Sub1 (); originable origin2 = new OriginSub2 (); Origin1.originmethod1 (); Origin1.originmethod2 (); origin2.originmethod1 (); Origin2.originmethod2 ();}}

Iv. Summary

1. Class Adapter mode: When you want to convert a class to a class that satisfies another new interface, you can use the class's adapter pattern, create a new class, inherit the original class, and implement the new interface.

2. Object Adapter mode: When you want to convert an object to an object that satisfies another new interface, you can create a adapter class that holds an instance of the original class, and in the method of the adapter class, the method that invokes the instance is the line.

3, Interface Adapter mode: When you do not want to implement all the methods of an interface, you can create an abstract class adapter implement all methods, when we write other classes, inherit the abstract class.



Java Design Pattern Rookie series (eight) modeling and implementation of 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.