Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/25833393
Hold on, Hold on ~
Continue to design patterns, the time to stop a few days do not want to write, must insist ~ Today brings adapter mode
In the old way, definition: The interface of a class is transformed into another interface that the customer expects, and the adapter makes the classes that are incompatible with the original interface cooperate with each other. This definition is good, saying that the function of the adapter is to turn an interface into another interface.
Two images are found to explain the adapter mode well:
These two graphs are a good indication of the role of the adapter ha, say I bought a European version of HTC G17, also equipped with a plug converter, this plug converter is the role of the adapter. Down to a bit of code to explain Kazakhstan, such as the topic, mobile phone charger is generally about 5V bar, our celestial household AC voltage 220V, so the mobile phone charging needs an adapter (buck), what the use of physical nouns wrong, forgive me.
First Mobile: Mobile.java
Package Com.zhy.pattern.adapter;public Class mobile{/** * charging * @param power */public void Inputpower (V5power power) {i NT Providev5power = Power.providev5power (); System.out.println ("Mobile Phone (client): I need 5V voltage charge, now is--" + Providev5power + "V");}}
As you can see, the phone relies on an interface that provides a 5 V voltage:
Package com.zhy.pattern.adapter;/** * Provides an interface for 5 v voltage * @author Zhy * */public interface v5power{public int providev5power ();}
Then we have the 220V household AC power:
Package com.zhy.pattern.adapter;/** * Household 220V AC * @author zhy * */public class v220power{/** * provides 220V voltage * @return */public int Providev220power () {System.out.println ("I offer 220V ac voltage. "); return 220;}}
Below we need an adapter to complete the function of 220V to 5V:
Package com.zhy.pattern.adapter;/** * Adapter, turn 220V voltage into 5V * @author Zhy * */public class V5poweradapter implements v5power{/** * Combination of ways */private v220power v220power;p ublic v5poweradapter (v220power v220power) {this.v220power = V220Power;} @Overridepublic int providev5power () {int power = V220power.providev220power ();//power through various operations-->5 SYSTEM.OUT.PRINTLN ("Adapter: I quietly adapted the voltage. "); return 5;}}
Last Test, we give the phone a power:
Package Com.zhy.pattern.adapter;public class Test{public static void Main (string[] args) {Mobile mobile = new mobile (); V5power v5power = new V5poweradapter (new V220power ()); Mobile.inputpower (V5power);}}
Output:
Existing class: I supply 220V AC voltage. Adapter: I have quietly adapted the voltage. Mobile (client): I need 5V voltage charge, now it's-->5v
As can be seen, we use an adapter to complete the conversion of 220V 5V and then provide to the mobile phone use, and we use the combination (OO design principles), the original phone, and 200V voltage class are not required to change, and the phone (client) and 220V (the adapter) completely decoupled.
Finally, we draw a UML class diagram, which is easy to understand:
The above is the class diagram of the adapter, here is our example of the class diagram, how, well. Nothing to draw a picture is also good, otherwise the software is white installed ....
Finally, congratulations, you learned a design mode, adapter mode. Continue to praise, ask for messages ~