Structural mode:
---core role: to achieve loose coupling from the structure of the program, so that the whole class structure can be enlarged to solve the larger problem
---categories:
Adapter mode, proxy mode, bridge mode, decoration mode, combo mode, appearance mode, enjoy meta mode
Adapter Mode: Principle:
--Convert a class interface into another interface that the customer wants.
Advantages:
Adapter mode makes it possible for classes to work together because they are incompatible with the interface
Roles in the pattern:
- Destination interface (target): the interface that the customer expects. The target can be a specific or abstract class, or it can be an interface
- Classes that need to be adapted (Adaptee): Classes or adapter classes that need to be adapted
- Adapter (Adapter): By wrapping an object that needs to be adapted. Convert the original interface into a target interface
Implementation mode: 1: Class Adapter:
Package Com.lp.adpater; Public class extends Implements Target { @Override publicvoid handlereq () { super. Request (); }}
Class Adapter
2: Object Adapter:
Package Com.lp.adpater; Public class adaptee { publicvoid request () { System.out.println ("ability to complete customer needs" ); }}
classes that need to be adapted
Package Com.lp.adpater; Public Interface Target { void handlereq ();}
Target Interface
Package Com.lp.adpater; Public class Implements Target { private adaptee adaptee; @Override publicvoid handlereq () { adaptee.request (); } Public Adapter (adaptee adaptee) { Super(); this. adaptee = adaptee; } }
Adapter
PackageCom.lp.adpater;//Client Class//equivalent to a laptop computer with only USB interface Public classClient { Public voidtest1 (Target t) {t.handlereq (); } Public Static voidMain (string[] arg) {Client C=NewClient (); Adaptee a=Newadaptee (); Target T=NewAdapter (a); C.test1 (t); } }Client
Work in the scene:
-often used to retrofit and upgrade old systems
-If we don't need to maintain the system after development, many models are unnecessary, but unfortunately, the cost of maintaining a system is often several times that of developing a system.
The learning scenario is used in adapter mode:
--Java.io.InputStremReader (InputStream)
--Java.io.OutputStremReader (OutputStream)
Design pattern Learning (V) Adapter mode