This essay mainly introduces the Java implementation of the adapter design pattern.
First look at the problem to be solved:
( photo from http://blog.csdn.net/jason0539)
From the situation, the European wall socket has only a three-legged socket, and if we want to use a two-foot socket, we need an adaptor to convert it. (The adapter side is a three-legged jack, one side is a two-legged jack)
There are two common implementations of adapter mode: 1, Class Adapter 2, object adapter.
First look at the UML diagram of the class adapter:
The client can only manipulate the request method of the Target interface to perform the required operation (this is, of course, the equivalent of calling the request method to charge with a two-foot socket). There are two types of interface-specific implementation classes, one is to call the Concretetarget Request method to charge (in the case of the equivalent of directly inserting two foot sockets into the two-foot socket, unfortunately we are now in Europe, and Europe is not a two-legged socket, only three-foot socket, so We cannot use the request method of Concretetarget . Another option is to call the Specificrequest method of the Adaptee class for charging (which is equivalent to charging with a three-foot socket, but we now have only two-legged sockets that cannot be plugged into a three-foot socket for charging, in other words, the Spe of the Adaptee class Cificrequest method We cannot call directly), we need to use the adapter class Adapter to convert the Specificrequest method of Adaptee class into the Request method we can invoke.
Next look at the specific code implementation:
1 InterfaceTarget {2 voidRequest ();//requires two-foot plug charging standard3 }4 5 classAdaptee {6 Public voidspecificrequest () {7SYSTEM.OUT.PRINTLN ("plug the three-foot plug on me to charge."));8 }9 }Ten One classAdapterextendsAdapteeImplementsTarget { A - @Override - Public voidRequest () { the //TODO auto-generated Method Stub - - //call the Specificrequest method of the parent class first, which is equivalent to inserting the end of the converter into a three-foot socket in Europe, - //You can charge the two-foot plug on the other end. + Super. Specificrequest (); - +SYSTEM.OUT.PRINTLN ("plug the two foot plugs on me to charge.")); A } at - } - Public classClient { - - Public Static voidcharge (target target) { - //customer calls the Request method to charge in Target. Request (); - } to + Public Static voidMain (string[] args) { -Adapter AD =NewAdapter ();//buy a converter first in Europe theCharge (AD);//charging with a converter * } $ Panax Notoginseng -}
Operation Result:
Two-foot plugs are also available for charging.
2. Object Adapter Mode implementation
UML diagram:
The implementation is approximately the same as the class adapter implementation, except that Adapter from the Adaptee class to Adapter holds the Adaptee class object.
1 InterfaceTarget {2 voidRequest ();//requires two-foot plug charging standard3 }4 5 classAdaptee {6 Public voidspecificrequest () {7SYSTEM.OUT.PRINTLN ("plug the three-foot plug on me to charge."));8 }9 }Ten One classAdapterImplementsTarget { A - Privateadaptee adaptee; - the PublicAdapter (adaptee adaptee) { - This. Adaptee =adaptee; - } - + @Override - Public voidRequest () { + //TODO auto-generated Method Stub A Adaptee. Specificrequest (); at -SYSTEM.OUT.PRINTLN ("plug the two foot plugs on me to charge.")); - } - - } - Public classClient { in - Public Static voidcharge (target target) { to //customer calls the Request method to charge + Target. Request (); - } the * Public Static voidMain (string[] args) { $Adapter AD =NewAdapter (NewAdaptee ());//buy a converter in Europe and insert one end into a three-foot socketPanax NotoginsengCharge (AD);//charging with a converter - } the + A}
The results of the operation are as above.
Adapter design patterns are typically used for late system expansion and modification.
Reference article: http://blog.csdn.net/ymeng_bupt/article/details/6833480
http://blog.csdn.net/jason0539/article/details/22468457
Probe into adapter design pattern (Java implementation)