Design mode--Adapter mode
1, definition: The interface of a class is converted into another interface that the customer wants;
2, Category:
Object Adapter mode: Not through inheritance, but by the combination of objects to deal with;
Class Adapter pattern: Implemented by an inherited method, encapsulating the methods of the old system. When an object adapter is transitioning between adapters, it is undoubtedly possible to use a class adapter, but the dependencies become larger and, depending on the flexibility of the adaptation requirements, can be inflated to uncontrollable by the inheritance system;
3, the application of:
1): When you want to use a class that already exists, but the interface of the class does not meet the existing requirements;
2): When it is necessary to create a subclass that can be reused, the class can work in conjunction with other unrelated classes or even unforeseen classes;
3): When it is necessary to use some subclasses that already exist, but it is not possible to subclass all subclasses to match their my interface, the object adapter can adapt its parent class interface;
4, Code:
1), the basic implementation code:
Main class: Main.class:
public class Main {
Public static void Main (string[] args) {
target target = new target ();
Target. Request ();
target = new Adapter ();
Target. Request ();
}
}
the interface class that the customer wants: Target.class:
public class Target {
Public void Request () {
System.out.println ("Customer initiated general basic needs");
}
}
class to be reused: Adaptee.class:
public class Adaptee {
Public void Specificrequest () {
//TODO auto-generated method stub
System.out.println ("Customer initiated special requirements");
}
}
Adapter class: Adapter.class:
public class Adapter extends target{
private Adaptee adaptee = new Adaptee ();
@Override
Public void Request () {
//TODO auto-generated method stub
Adaptee. Specificrequest ();
}
}
Operation Result:
2), Instance code:
Main class: Main.class:
public class Main {
Public static void Main (string[] args) {
System.out.println ("a facet before going abroad to use a notebook:");
Adapter Adapter = new adapter220v ();
adapter. Powersupply ();
System.out.println ("Using notebooks after a facet goes abroad");
adapter = new adapter110v ();
adapter. Powersupply ();
}
}
class to be reused--110v Power Interface class: Powerport110v.class:
/*110v Power connector */
public class powerport110v {
Public void Powersupply () {//adapter provides power
//TODO auto-generated method stub
System.out.println ("110V power supply interface outputs 110V voltage");
}
}
class to be reused--220v Power Interface class:powerport220v.class:
/*220v Power connector */
public class powerport220v {
Public void Powersupply () {//adapter provides power
System.out.println ("110V power supply Interface output 220V voltage");
}
}
Adapter Interface class:adapter.class:
/* Adapter Interface */
Public interface Adapter {
void Powersupply ();
}
Adapter class: Adapter class for 110V interface : Adapter110v.class:
Adapter class for/*110v interface */
public class adapter110v extends powerport110v implements adapter{
NoteBook NoteBook = new NoteBook ();
@Override
Public void Powersupply () {//adapter provides power
//TODO auto-generated method stub
Super. Powersupply ();
System.out.println ("adapter converts the voltage into a notebook");
notebook.work ();
}
}
Adapter class: Adapter class for 220V interface: Adapter220v.class:
Adapter class for/*220v interface */
public class adapter220v extends powerport220v implements adapter{
NoteBook NoteBook = new NoteBook ();
@Override
Public void Powersupply () {//adapter provides power
//TODO auto-generated method stub
super.powersupply ();
System.out.println ("adapter converts the voltage into a notebook");
notebook.work ();
}
}
class to use the multiplexed code: Notebook.class:
/* Notebook class */
public class NoteBook {
Public void Work () {
System.out.println ("The notebook has begun to work");
}
}
?
Operation Result:
Adapter mode of design mode