The adapter pattern is defined as the interface of one class to another interface that the customer expects, and the adapter makes the classes that are incompatible with the original interface work seamlessly. Look at the following two pictures to make it clearer.
Adapter can realize the decoupling of the interface, if after a period of time, the manufacturers want to change the interface, the adapter can change the part of the package, the user does not need to follow the interface changes, as long as the adapter is changed.
There is a requirement: there is a duck and a turkey, I want to use the turkey object to impersonate the duck, but the turkey and the Duck method is not exactly the same, then need a converter
Duck.java Duck Interface
Package Com.dzt.adapter;public interface Duck {public void quack ();p ublic void Fly ();
Turkey.java Turkey Interface
Package Com.dzt.adapter;public interface Turkey {public void gobble ();p ublic void Fly ();
Implement a Duck class Mallardduck.java
Package Com.dzt.adapter;public class Mallardduck implements Duck {@Overridepublic void Quack () {//TODO auto-generated met Hod stubSystem.out.println ("Mallardduck------------------>quack");} @Overridepublic void Fly () {//TODO auto-generated method StubSystem.out.println ("Mallardduck------------------> Fly ");}}
Implement a turkey class Wildturkey.java
Package Com.dzt.adapter;public class Wildturkey implements Turkey {@Overridepublic void gobble () {//TODO auto-generated m Ethod stubSystem.out.println ("Wildturkey------------------>gobble");} @Overridepublic void Fly () {//TODO auto-generated method StubSystem.out.println ("Wildturkey------------------>fly ");}}
To turn a turkey into a duck, create an adapter Turkeyadapter.java
Package com.dzt.adapter;/** * Can convert turkeys into ducks * * @author Administrator * */public class Turkeyadapter implements Duck {Turkey turkey;public turkeyadapter (Turkey Turkey) {this.turkey = Turkey;} @Overridepublic void Quack () {//TODO auto-generated method Stubturkey.gobble ();} @Overridepublic void Fly () {//TODO auto-generated method stubfor (int i = 0; i < 5; i++) Turkey.fly ();}}
Since it is necessary to turn the turkey into a duck, the duck interface needs to be implemented, and the duck implementation class has the same method of passing in the object that needs to be adapted (Turkey)
Test code
Package com.dzt.adapter;/** * Using adapter mode requirements: Because the right duck can not be found, only temporarily to the Turkey to replace the duck, but the duck and Turkey method is not the same, you need to use a "middleman" to convert. * * @author Administrator * @data 2014.08.19 */public class AdapterModel1 {public static void main (string[] args) {Wil Dturkey Wildturkey = new Wildturkey (); Mallardduck Mallardduck = new Mallardduck ();D UCK adapter = new Turkeyadapter (Wildturkey); System.out.println ("Turkey say ..."); wildturkey.gobble (); Wildturkey.fly ();;; ";" System.out.println ("Duck say ..."); Mallardduck.quack (); Mallardduck.fly ();;; ";" SYSTEM.OUT.PRINTLN ("adapter say ..."); Adapter.quack (); Adapter.fly ();;; ";" System.out.println ("-------------------------------------------------");}}
This way the adapter can use the Duck class method, this mode can reduce the coupling between the customer and the system, more conducive to program maintainability
Related code: http://download.csdn.net/detail/deng0zhaotai/7835473
Reference book: "Head First design mode"
Adapter mode for Android design mode