Why use Patterns: patterns are a way of doing things, that is, the way to achieve a goal, or technology.
The purpose of the adapter model is to retain the services provided by existing classes and to provide customers with interfaces to meet customer needs. Class Adapter: The client defines the interface and implements the interface, the method in its own class library has a better implementation, but the method name is not the same, not for the user, you can define a class implementation of this interface, the method of the interface can be delegated to their own class library methods to achieve the effect, To meet the needs of customers. This approach is called interface adaptation.
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 {public void request ();} 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 Concretetarget (); Concretetarget.request (); Using a special function class, the adaptation class Target adapter = new adapter (); Adapter.request (); }}
Object adapters: Instead of using multiple inheritance or inheritance implementations, instead of using a direct association, or a delegate in a way that inherits, instead uses the global parameter to save the object that is being adapted, using that object to fit the content//share in front of the content
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 = new 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 (); }}
Java Presentation Adapter (adapter) mode