1. Concept: convert an interface into another interface that the customer wants. (This mode allows incompatible classes to work together)
2. UML diagram
(1)ObjectAdapterSchema Structure
(2)ClassAdapterSchema Structure
3.Code
(1)Object Adapter
Public interface target {public void request ();} public class adaptee {public void specificrequest () {system. Out. println ("this is the original standard interface! ") ;}} Public class adapter implements target {private adaptee adapteet = NULL; Public void request () {adapteet. specificrequest ();}}
(2)Class Adapter
Public interface target {public void request ();} public class adaptee {public void specificrequest () {system. Out. println ("this is the original standard interface! ") ;}} Public class adapter extends adaptee implements target {public void request () {super. specificrequest ();}}
4. Application scenarios
Use the adapter mode in the following situations:
1. The system needs to use an existing class, and such an interface does not meet the requirements of the system.
2. Create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future. These source classes do not necessarily have very complex interfaces.
3. (For object adapters) in the design, you need to change the interfaces of multiple existing sub-classes. If you use the class adapter mode, you need to create an adapter for each sub-class, which is not practical.