Original address: http://leihuang.org/2014/12/06/adapter/
Structural mode how to design the static structure between objects, how to complete the inheritance, implementation and dependency between objects, which is related to the system design is robust (robust): such as easy to understand, easy to maintain, easy to modify, low coupling and other issues. As the name of the Structural model, the pattern under its classification gives a variety of object relationship structures that can be applied in different situations.
- Default Adapter Mode
- Adapter mode
- Bridge mode
- Composite mode
- Decorator mode
- Facade mode
- Flyweight mode
- Proxy mode
What is an adapter?
For example, for example, programmers like to use Linux programming, but, we sometimes need to use QQ, and Linux under no QQ, how to do? So there is wine this thing. The wine here is an adapter (Wineadapter), and Linux is the adaptation source ( Linuxadaptee). Here we define the Windows platform as an interface (iwindows).
However, there are two implementations of the adapter pattern, one is the class adapter (adapter inheritance adaptation source), and the second is the object adapter (the adapter contains an object with an adaptation source)
Class Adapter
Iwindows interface
Public interface Iwindows {public void coding (); public void QQ ();
Linuxadaptee class
public class Linuxadaptee {public void coding () { System.out.println ("Print ' Hello World '");} }
Wineadapter class
public class Wineadapter extends Linuxadaptee implements iwindows {public void coding () { super.coding (); You can also define the encoding method in wine } @Override public void qq () { System.out.println ("QQ"); }
Object Adapter
The class structure diagram of the object adapter below
If implemented, only the Wineadapter adapter is different
public class Wineadapter implements Iwindows { private linuxadaptee Linux = null; Public Wineadapter (Linuxadaptee Linux) { this.linux = Linux; } public void Coding () { linux.coding (); } @Override public void qq () { System.out.println ("QQ");} }
2014-12-06 22:11:17
Brave,happy,thanksgiving!
Design mode: Adapter mode