1. Scene
2. Category
Class 2.1 Agent
Existing classes with special features but not conforming to our existing standard interface class Adaptee {public void Specificrequest () {System.out.println ("The adapted class has special features ...");}} Target interface, or standard interface interface target {public void request ()}//target class, only provides normal function class Concretetarget implements target {public Vo ID request () {System.out.println ("ordinary 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 Concretetarg ET (); concretetarget.request ();//Use a special function class, that is, the adaptation class target adapter = new adapter (); Adapter.request ();}}
2.2 Object Proxies
//adapter class, directly associated with the appropriate class, while implementing a standard interfaceclassAdapterImplementstarget{//Direct correlation is adapted class Privateadaptee adaptee; //you can pass through the constructor to the adapted class object that needs to be adapted. PublicAdapter (adaptee adaptee) { This. Adaptee =adaptee; } Public voidrequest () {//here is the way to accomplish special functions using delegates This. Adaptee.specificrequest (); }} //Test Class Public classClient { Public Static voidMain (string[] args) {//using normal function classesTarget Concretetarget =NewConcretetarget (); Concretetarget.request (); //use a special function class, the adaptation class,//you need to first create an object of the appropriate class as a parameterTarget adapter =NewAdapter (Newadaptee ()); Adapter.request (); }}
Design mode first bullet-adapter mode