Intent to convert the interface of one class into another interface that the client wants. The Adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together. Mainly divided into three categories:The adapter mode of the class, the adapter mode of the object, the adapter mode of the interface。 1. The adapter mode of the class has a source class, has a method, is ready to be fitted, the target interface is targetable, through the adapter class, the function of the source extension to targetable.
public class Source {public void method1 () {System.out.println ("This is original method!");}}
Public interface Targetable {//and the original class method is the same as public void method1 ();//new public void Method2 ();}
public class Adapter extends Source implements targetable{@Overridepublic void Method2 () {System.out.println ("This is tar Getable method! ");}}
2. Adapter mode for objects
The basic idea is the same as the adapter pattern of the class, only the adapter class is modified, this time does not inherit the source class, but holds an instance of the source class to solve the compatibility problem.
public class Wrapper implements Targetable{private source Source;public Wrapper (source source) {super (); This.source = SOURCE;} @Overridepublic void Method1 () {source.method1 ();} @Overridepublic void Method2 () {System.out.println ("This is the targetable method!");}
3. Adapter mode for the interface
When you do not want to implement all the methods in an interface, you can create an abstract class wrapper, implement all the methods, we write the other class, inherit the abstract class.
Public abstract class Wrapper2 implements targetable{@Overridepublic void Method1 () {} @Overridepublic void Method2 () {}}
public class Sourcesub extends wrapper2{@Overridepublic void Method1 () {System.out.println (' This is first method! ');}}
Transferred from: http://blog.csdn.net/zhangerqing/article/details/8239539
Java Learning Note-design mode 7 (adapter mode)