Java design mode 6 -- Adapter mode and design mode 6 --
URL: http://www.cnblogs.com/archimedes/p/java-adapter-pattern.html.
Adapter mode (alias: Package)
Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Overview
The adapter mode is a mature mode that converts an interface (the adapter) of a class to another interface (target) that the customer wants, this mode involves targets, adaptors, and adapters. The key to the adapter mode is to create an adapter that implements the Target Interface and contains references from the adapter.
Applicability
1. You want to use an existing class, and its interface does not meet your needs.
2. You want to create a reusable class that can work collaboratively with other unrelated classes or unforeseen classes (that is, classes that may not be compatible with certain interfaces.
3. (applies only to object adapters) You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface.
Participants
1. Target defines the interfaces used by the Client in specific fields.
2. Collaboration between the Client and objects that comply with the Target interface.
3. Adaptee defines an existing interface, which must be adapted.
4. the Adapter adapts the Adaptee interface to the Target interface.
Structure and usage of the adapter Mode
The schema structure includes three roles:
Target)
Adaptee)
Adapter)
UML class diagram of the Mode:
Practice
The user already has a two-phase outlet, but recently the user has a new three-phase outlet. The user now has a washing machine and a TV. the washing machine is equipped with three-phase plugs according to the standard of Three-phase sockets, while the TV is equipped with two-phase plugs according to the standard of Two-Phase sockets. Now users want to use a new three-phase socket to use washing machines and televisions.
1. Target: ThreeElectricOutlet. java
public interface ThreeElectricOutlet{ public abstract void connectElectricCurrent(); }
2. Adaptee: TwoElectricOutlet. java
public interface TwoElectricOutlet{ public abstract void connectElectricCurrent(); }
3. Adapter: treepolicicadapter. java
public class TreeElectricAdapter implements ThreeElectricOutlet{ TwoElectricOutlet outlet; TreeElectricAdapter(TwoElectricOutlet outlet){ this.outlet=outlet; } public void connectElectricCurrent(){ outlet.connectElectricCurrent(); }}
4. Application. java_1
Public class Application {public static void main (String args []) {ThreeElectricOutlet outlet; Wash wash = new Wash (); outlet = wash; System. out. println ("using three-phase socket for current:"); outlet. connectElectricCurrent (); TV TV = new TV (); treepolicicadapter adapter = new treepolicicadapter (TV); outlet = adapter; System. out. println ("using three-phase socket for current:"); outlet. connectElectricCurrent ();}}
4. Application. java_2
Class Wash implements ThreeElectricOutlet {String name; Wash () {name = "Yellow River Washing Machine" ;}wash (String s) {name = s;} public void connectelectrelectriccurrent () {turnOn ();} public void turnOn () {System. out. println (name + "Start laundry. ");}}
4. Application. java_3
Class TV implements TwoElectricOutlet {String name; TV () {name = "Changjiang TV";} TV (String s) {name = s;} public void connectelectrelectriccurrent () {turnOn ();} public void turnOn () {System. out. println (name + "starts to play the program. ");}}
Advantages of the adapter Mode
• The Target and Adaptee are completely decoupled.
• The adapter mode meets the "On-Off" principle ". When you add a new class that implements the Adaptee interface, the Adapter can adapt to the instance of the new class without modifying the Adapter.
You may also be interested in:
Java Design Pattern series:
Java Design Pattern 5-Prototype)
Java Design Pattern 4 -- Builder pattern (Builder)
Java Design Mode 3 -- Singleton)
Java Design Pattern 2 -- Abstract Factory pattern (Abstract Factory)
Java design mode 1 -- Factory Method)
Java design mode 0-Introduction to design mode