Small test mode adapter

I. INTRODUCTION

We look at the definition of "grind design mode":

Converting the interface of a class clients expect another interface. Adapter pattern makes those classes otherwise because of incompatible interfaces can not work together to work together.

It is the essence of the adapter mode - conversion match, the multiplexing function.

How to understand it? That is, the new interface needs to implement functionality, the old interface have now, the question is, how the old interface implementation class take to achieve the new interface.

The method is very simple, and that is the class that implements the old interface into the adapter, can be used as member variables, local variables can be used as long as it can use all OK. Then implement new adapter interfaces, the adapter @Overide method, call the class that implements the old interface to achieve functionality.

 

Second, the structure

1, Client-- caller

2, Target-- target interface, an interface adapter required

3, Adapter-- adapter to achieve the target interface

4, Adaptee-- already existing interfaces, to meet the demand, but the interface is inconsistent and objectives. Adapters rely on it, the function of the target interface.

 

Third, I realized

1, first of all we define an interface PastInterface, as follows:

package adapter;

public interface PastInterface {

    public void past1();

    public void past2();
}

2, a simple implementation class is as follows:

package adapter;

public class PastImpl implements PastInterface{

    PastImpl(){
        System.out.println ( "I realize PastImpl interface" );
    }
public  void past1 () {
         // the TODO Auto-Generated Stub Method 
        System.out.println ( "Past method of operating a first interface" );
    }

    public  void past2 () {
         // the TODO Auto-Generated Stub Method 
        System.out.println ( "Past the second working interface" );
    }
}

3, when the client calls, is very simple:

package adapter;

public  class Customer {

    public static void main(String[] args) {
        PastInterface past = new PastImpl();
        past.past1();
        past.past2();
    }
}

4, as follows:

I realize PastImpl interface
Run Past The first method interface
The method of operation of the second interface Past

5, now needs to change, to use the new interface to those functions in the past, the new interface is as follows:

package adapter;

public  interface PresentInterface {

    public  void present1 ();

    public void present2();
}

6, adapter mode is to solve the problem.

package adapter;

public class Adapter implements PresentInterface {

    PastInterface past = new PastImpl();

    public void present1() {
        // TODO Auto-generated method stub
        past.past1();
    }

    public void present2() {
        // TODO Auto-generated method stub
        past.past2();
    }

}

Above, Adapter class implements the interface PresentInterface, the original interface methods to achieve its methods, described as "medication", like a simple converter, the package about the original function, into the present function .

7. Thus, when the client calls, as follows:

package adapter;

public  class Client2 {

    public  static  void main (String [] args) {
         // create an adapter, because the adapter implements the features of the new interface, the method of the adapter so called directly. 
        Adapter = Adapter new new Adapter ();
        adapter.present1();
        adapter.present2();
    }
}

The end result is no difference with the original results.

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/3617285.html

Guess you like

Origin blog.csdn.net/weixin_34405354/article/details/93248508