Structural Design Mode - Adapter Mode

A small case study

1, functional requirements:

To the power adapter, for example.
  A person to foreign travel, living abroad in a hotel, just run out of battery (cell phone is a two-hole plug), but only three-hole socket foreign hotels, hotels generally impossible for passengers, will replace the two-hole three-hole socket outlet, this when the adapter comes in handy, the plug will turn into a three-pin plug with two holes, to meet the requirements.

2. What is the adapter mode:

  Adapter Mode is a structural design pattern. Adapter pattern of thought is: to transform a class interface as a client expect another interfaces so that two classes can work together originally because of mismatched interfaces can not work together.

3, the adapter pattern classification

(1) in the adapter mode
(2) Object Adapter mode
(3) Interface adapter mode

4, the Adapter pattern roles:

(1) Source (Adaptee): the type of object or needs to be adapted, with two holes corresponding to the plug.
(2) an adapter (Adapter): and an intermediate connection target object sources, corresponding to plug adapter.
(3) target (Target): looking forward to get the goal, the equivalent of three-pin plug.

 

Second, the adapter mode

1, class Adapter pattern implementation case:

(1) adapter object inherits the original class (such as three-hole plug).
(2) a combination of a target object for the new adapter interface (such as two-hole plug) implementation.
The original method (3) adapter calls instance is entrusted to the new interface methods. That method calls the method in the original class of the new interface. (The two-hole plug can call the three-pin plug)
(4) code to achieve:

Package adapter.pattern.demo1; 

/ ** 
 * test class, a test class adapter mode 
 * / 
public  class ClassAdapterDemo {
     public  static  void main (String [] args) { 
        Adapter Adapter = new new Adapter (); 
        adapter.show2 (); 
    } 
} 

/ ** 
 * three-pin plug (original class) 
 * 
 * / 
class ThreeHolePlug {
     public  void show1 () { 
        System.out.println ( "three-pin plug" ); 
    } 
} 

/ ** 
 * two-hole plug (new requirements ) 
 * / 
interface DoubleHolePlug {
    public  void Show2 (); 
} 

/ ** 
 * original class inheritance, and implement a new interface, the interface method for rewriting, and call the methods of the original class. 
 * 
 * / 
Class Adapter the extends ThreeHolePlug the implements DoubleHolePlug { 

    @Override 
    public  void Show2 () { 
        show1 (); 
    } 
}

(5) Analysis Code:
  use inheritance to achieve, so that the class has some limitations. But you can get the original class or a method of rewriting on demand.
(6) UML diagram:

 

 

2, object adapter pattern implementation case

(1) has an internal adapter object instance of the object class of the original. (I.e. three holes socket object)
(2) a combination of a target object for the new adapter interface (such as two-hole plug) implementation.
The original method (3) adapter calls instance is entrusted to the new interface methods. That method calls the method in the original class of the new interface. (The two-hole plug can call the three-pin plug)
(4) code to achieve:

Package adapter.pattern.demo2; 

/ ** 
 * test class object adapter test mode 
 * / 
public  class ObjectAdapterDemo {
     public  static  void main (String [] args) { 
        Adapter Adapter = new new Adapter ( new new ThreeHolePlug ()); 
        adapter.show2 (); 
    } 
} 

/ ** 
 * three-pin plug (original class) 
 * 
 * / 
class ThreeHolePlug {
     public  void show1 () { 
        System.out.println ( "three-pin plug" ); 
    } 
} 

/ ** 
 * two holes plug (new requirements) 
 * /
interface DoubleHolePlug {
     public  void Show2 (); 
} 

/ ** 
 * implement a new interface, the interface method in rewriting of the original class instance object into the adapter by instantiating the object class to invoke the original method of . 
 * 
 * / 
Class Adapter the implements DoubleHolePlug { 

    ThreeHolePlug threeHolePlug; 

    / ** 
     * constructor, the instance of the object class of the original incoming adapter 
     * 
     * @param threeHolePlug 
     instance of the object class of the original * 
     * / 
    public Adapter (ThreeHolePlug threeHolePlug) {
         the this = .threeHolePlug threeHolePlug; 
    } 

    @Override 
    public  void Show2 () {
        threeHolePlug.show1();
    }
}

(5) Analysis of code:
  this code, instead of multiplexing synthesized according to the inheritance principle, polymerization, reduces the limitations of inheritance, and no longer requires the interface must be new requirements.
(6) UML diagram:

 

 

3, interface adapter mode (default adapter mode):

(1) When a too abstract interface methods to achieve this class will need to implement all of the methods, unfriendly. An abstract class may be used to achieve the interface, rewriting all methods (be rewritten as null method). At this time, the abstract class inheritance, and overridden method may be selected as desired.
(2) applies to the use of an interface, but do not want to rewrite the scenario interface all methods.
(3) code implementation:

Package adapter.pattern.demo3; 

/ ** 
 * test class interface adapter test mode 
 * / 
public  class InterfaceAdapterDemo {
     public  static  void main (String [] args) {
         // according to their needs, rewrite related methods need not all rewriting 
        new new MoveApapter () { 

            @Override 
            public  void up () { 
                System.out.println ( "upward movement" ); 
            } 
        } .UP (); 

        new new MoveApapter () { 

            @Override 
            public  void Down () { 
                the System.out .println ( "downward movement"  );
            }
        .down} (); 
    } 
} 

/ ** 
 * interface comprising a plurality of abstract methods 
 * 
 * / 
interface the Move {
     void up (); 

    void Down (); 

    void left (); 

    void right (); 
} 

/ ** 
 * abstract class that implements the interface, and a method of rewriting all the empty method 
 * 
 * / 
abstract  class MoveApapter the implements the Move { 

    @Override 
    public  void up () { 
    } 

    @Override 
    public  void Down () { 
    } 

    @Override 
    public  void left () { 
    }

    @Override
    public void right() {
    }

}

(4) UML diagram:

 

Guess you like

Origin www.cnblogs.com/l-y-h/p/11431535.html