MasterofProject

JAVA design patterns: adapter mode

81 people read comment(0) Collection Report
Classification:
Adapter model is a class of interface adapter into the user's expectations, so that the original interface is not compatible with some of the classes can not work together to work together to achieve the desired functionality of the user.
Adapter mode advantage:
  1. Through the adapter, the client can call the unified interface, easy to operate, and the code logic is compact, easy to use.
  2. Code reuse, the adapter pattern is solved because the environment requirements are not the same, by adapting to achieve code reuse.
  3. Decoupling the target class and the adapter class, by creating an adapter class to reuse the current class, do not have to repeat the original code, to achieve the principle of open and close.
    A total of two types of adapter mode

1 object adapter mode: in this adapter pattern, the adapter contains an instance of the class that it wraps. In this case, the adapter is invoked by the physical entity of the package object.
2 type of adapter mode: this adapter mode, the adapter inherits from its own implementation of the class (generally multiple inheritance).
Sample code:
/ * *
* @description:
* the original function of the interface, such as the electric car charging function
* /
Interface FirstBaseIf public {
Void (charging); / / charging

}

/ * *
* to achieve the operating function
* @description:
* /
Class NewFunction public {
Void powerWithTwo public () {
System.out.println ("new features: to the electric car can also be charged");
}

}

/ * *
* new adapter class, to achieve the original interface FirstBaseIf the operation of the use of
* @description:
* /
Class FunctionAdapter implements FirstBaseIf public {
NewFunction plug private;
FunctionAdapter public (plug NewFunction) {
This.plug=plug;
}
@Override
Public void (charging) method {// rewrite of the original interface, so as to realize the interface conversion
System.out.println ("through adapter conversion");
Plug.powerWithTwo ();
}
}
- - - - - - - - - - - - - -
Class Test public {
FirstBaseIf plug private;

Test public (plug FirstBaseIf) {
This.plug = plug;
}

Static void main public (args String[]) {
Two NewFunction = NewFunction new ();
FirstBaseIf three = new FunctionAdapter (two); / / here is through the adapter to convert
Note Test = Test new (three);
Note.charge ();
}

Void charge public () {
(plug.charging); / / use the original interface function
}

}
Operation result:
Write the picture here.
Applicable scenarios:
In the Java program, if an abstract class or interface, it is necessary to rewrite all the abstract methods.
So, if the definition of an interface in the abstract method is too much, and a lot of abstract methods in the subclass and do not need to use, you should design an adapter. Java language through the development of Android, it is often used in the adapter model.

top
Zero
step on
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit44905 times
    • Integral:Two thousand one hundred and fifty-three
    • Grade
    • Rank:10603rd name
    • Original176
    • Reproduced:37
    • Translation:1
    • Comments:11
    Latest comments