1. Overview
Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible to work together those classes that would otherwise not work together because of incompatible interfaces.
2. Issues to solve
That is, the adapter mode makes it possible for those classes that cannot work together because the interface is incompatible.
3. Roles in the pattern
3.1 Destination Interface (target): the interface that the customer expects. The target can be a specific or abstract class, or it can be an interface.
3.2 Classes that need to be adapted (Adaptee): A class or an adapter class that needs to be adapted.
3.3 Adapter (Adapter): Converts the original interface into a target interface by wrapping an object that needs to be adapted.
4. Pattern Interpretation
Note: In Gof design mode, there are two types of adapter modes, class adapter mode and object Adapter mode. Because the class adapter pattern matches one interface to another through multiple inheritance, while languages such as C # and Java do not support multiple inheritance, this is just an introduction to object adapters.
4.1 Class diagram for Adapter mode
4.2 Code implementation of the adapter mode
<summary>/// Define the interface expected by the client/// </summary> public class Target {// <summary > ////Use virtual decoration so subclasses can rewrite //</summary> public virtual void Request () { Console.WriteLine ("This is a common request"); } } <summary>/// Define the classes that need to be adapted//// </summary> public class Adaptee {public void Specificrequest () { Console.WriteLine ("This is a special request."); } <summary>/ /define Adapter ///</summary> public class Adapter:target { // Establish a private Adeptee object adaptee adaptee = new Adaptee (); <summary>//// by rewriting the request () method on the surface, it becomes the actual call to Specificrequest ()/// </summary> public override void Request () { adaptee. Specificrequest (); } }
4.3 Client code
Class program { static void Main (string[] args) { //For the client, the call is the target request () Target target = New Adapter (); Target. Request (); Console.read (); } }
5. Pattern Summary
5.1 Advantages
5.1.1 through the adapter, the client can invoke the same interface, which is therefore transparent to the client. This is simpler, more straightforward, and more compact.
5.1.2 the existing classes and solves the problem that the existing class and reuse environment requirements are inconsistent.
5.1.3 Decouples the target class from the adapter class by introducing an adapter class to reuse existing adapter classes without modifying the original code.
5.1.41 object adapters can adapt multiple different adapter classes to the same target, that is, the same adapter can be used to match the matching class and its subclasses to the target interface.
5.2 Disadvantages
For an object adapter, the implementation of the replacement adapter is more complex.
5.3 Applicable Scenarios
5.3.1 systems require the use of existing classes, and the interfaces of these classes do not conform to the interface of the system.
5.3.2 wants to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future.
5.3.32 classes do things the same or similar, but with different interfaces.
5.3.4 the old system developed classes have implemented some functionality, but the client can only be accessed as a different interface, but we do not want to manually change the original class.
5.3.5 uses third-party components, 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.
6. Examples of adapter applications
6.1 Developers who have used ADO should have used DataAdapter, which is used as an adapter between a dataset and a data source. DataAdapter provides this adapter by mapping fill and update.
6.2 Phone Power Adapter
Design mode-Adapter mode