definition:(transforms the interface of one class into another interface that the client expects, so that two classes that would otherwise not work together because of an interface mismatch can work together
Type: structure type pattern
Class Diagram:
The adapter mode of the class (implemented with inheritance)
Object adapter pattern (implemented by object combination)
Code implementation:
class's Adapter mode
A class Adaptee {public void Specificrequest () { System.out.println ("the" adapter class has special functionality) that already exists and has a special function but does not conform to our existing standard interface ...");
Target interface, or standard interface interface target {
Target class, only provide normal function class Concretetarget implements target {public void request () { System.out.println ("Normal class has normal function ...");
The adapter class inherits the appropriate class, while implementing the standard interface class Adapter extends Adaptee implements target{public void request () { Super.specificrequest (); } }
Test class public class Client {public static void main (string[] args) { //Use normal function class Target concretetarget = new Concre Tetarget (); Concretetarget.request (); Using a special function class, the adaptation class Target adapter = new adapter (); Adapter.request ();
Operation Result:
Ordinary class has normal function ... The adapted class has special functions ...
Object Adapter Mode
The adapter class, which is directly associated with the appropriate class, implements the standard interface class Adapter implements target{ //directly associated with the appropriate class private adaptee adaptee; The Adapter (adaptee adaptee) { this.adaptee = adaptee ) can be passed through the constructor to the appropriate matching class object that needs to be adapted. public void request () { //Here is the way to accomplish special functions using a delegate this.adaptee.specificRequest ();} }
Test class public class Client {public static void Main (string[] args) { //Use normal function class Target concretetarget = NE W Concretetarget (); Concretetarget.request (); Using a special function class, the adaptation class, //requires first creating an object of the appropriate class as the parameter target adapter = new Adapter (new Adaptee ()); Adapter.request ();
the test results are consistent with the above.
Advantages of the adapter mode:
Through the adapter, the client can invoke the same interface and thus be transparent to the client. This is simpler, more straightforward, and more compact.
L Reuse existing classes to solve the problem of inconsistent requirements for existing and reused environments.
L DECOUPLE the target class from the adapter class by introducing an adapter class to reuse existing adapter classes without modifying the original code.
L An object adapter can adapt multiple different adapter classes to the same target, that is, the same adapter can match the class of the adaptor and its subclasses to the target interface.
Disadvantages of Adapter mode:
For object adapters, the implementation of the replacement adapter is more complex.
Applicable scenarios:
The system needs to use existing classes, and the interfaces of these classes do not conform to the interface of the system.
l want to create a reusable class that works with some classes that are not much related to each other, including some that might be introduced in the future.
L Two classes do things the same or similar, but with different interfaces.
The old system-developed class has implemented some functionality, but the client can only access it in the form of a different interface, but we don't want to change the original class manually.
Using a third-party component, the component interface definition differs from its own definition and does not want to modify its own interface, but uses the functionality of the third-party component interface.
Precautions:
L Adapter mode preferably in the detailed design phase do not consider it, it is not to solve the problem is still in the development phase, but to solve the problem of the project is in service, not a system analyst will be in the detailed design to consider the use of adapter mode, the main scenario used in this mode is to extend the application, As with the example above, the system expands to consider reducing the risk of code modification through adapter mode when it does not conform to the original design.
Once again, it is important to follow the principle of dependency inversion and the change of the Richter scale, otherwise, even in the case of adapter use, it will bring a great transformation.
(13) Adapter mode