Structure |
|
Intention |
Transforms the interface of one class into another interface that the customer wants. A d a P T e r mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together. |
Applicability |
- You want to use a class that already exists, and its interface doesn't fit your needs.
- You want to create a reusable class that can work with other unrelated classes or classes that are not predictable (that is, those that might not necessarily be compatible with the interface).
- (Applies to object a D a P T e r) you want to use some subclasses that already exist, but it is not possible to subclass each to match their interfaces. The object adapter can be adapted to its parent class interface.
|
1 usingSystem;2 3 classFrameworkxtarget4 {5 Virtual Public voidSomerequest (intx)6 {7 //Normal implementation of Somerequest goes here8 }9 }Ten One classFrameworkyadaptee A { - Public voidQuiteadifferentrequest (stringstr) - { theConsole.WriteLine ("quiteadifferentrequest = {0}", str); - } - } - + classOuradapter:frameworkxtarget - { + PrivateFrameworkyadaptee adaptee =Newframeworkyadaptee (); A Override Public voidSomerequest (inta) at { - stringb; -b =a.tostring (); - Adaptee. Quiteadifferentrequest (b); - } - } in - /// <summary> to ///Summary description for Client. + /// </summary> - Public classClient the { * voidGenericclientcode (Frameworkxtarget x) $ {Panax Notoginseng //We Assume this function contains client-side code - //knows about Frameworkxtarget. theX.somerequest (4); + //Other calls to Frameworkx go A // ... the } + - Public Static intMain (string[] args) $ { $Client C =NewClient (); -Frameworkxtarget x =NewOuradapter (); - C.genericclientcode (x); the return 0; - }Wuyi}
Adapter Mode
Adapter mode for structural design patterns