Dahua Design Patterns Fifteen Adapter Patterns

Yao Ming said that the biggest difference between the CBA and the NBA is that translation is required in the NBA, but not in the CBA. Before the NBA, an interpreter was specially equipped for him. The interpreter was always by Yao Ming's side, especially on the court. All conversations between coaches and players and him were communicated through interpreters.


1. Adapter mode

Adapter mode (Adapter), the interface of a class is converted into another interface that the client wants. The Adapter pattern enables classes to work together that would otherwise not work because of incompatible interfaces.



What problem does the adapter mode pay attention to?

Simply put, what is needed is in front of you, but it cannot be used, and it cannot be transformed in a short time, so we try to adapt it.


Yao Ming can't speak English, but he wants to play in the NBA

The first way is to let Yao Ming learn English and immediately learn that he can understand and speak it, which is very difficult and unrealistic. Second, it is even more funny to let coaches and players learn Chinese. That can only find an interpreter for Yao Ming.

Without changing the team's coaches, players and Yao Ming, all we can do is find a way to find an adapter.


When the data and behavior of the system are correct, but the interface does not match, we should consider using an adapter, the purpose is to make an original object outside the control scope match an interface. The adapter pattern is mainly used when you want to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment.


In the design pattern of GoF, there are two types of adapter patterns, class adapter pattern and object adapter pattern. Since the class adapter pattern matches one interface with another interface through multiple inheritance, C#, VB.NET, JAVA, etc. Neither language supports multiple inheritance (supported by C++), that is, a class has only one parent class, so we are mainly talking about object adapters here.





2. When to use the adapter mode

Use an existing class, but if its interface, that is, its methods, are not the same as your requirements, you should consider using the adapter pattern.


It is used when two classes do the same or similar things, but have different interfaces.


The client code can call the same interface uniformly, which can be simpler, more direct and more compact


In fact, using the adapter mode is also a helpless move, and it feels a bit 'fixing the sheep'. It is a day when the software will be maintained, and the maintenance may be caused by different developers, different products, and different manufacturers. In this case, it's time for the adapter mode to show its strengths.

It is to consider using it in the later stage of software development or maintenance period. If it is in the design stage, it is necessary to design the classes with similar functions to the same interface.

Within the company, there should be no standard for the definition of classes and methods. It is best to design them in the early stage. If the interfaces are not the same, the adapter should not be considered first, but should be considered to unify the interface through refactoring.


Use the adapter mode to adapt when both parties are not easy to modify.


There are also times when the adapter pattern needs to be considered at the beginning of the design. For example, the company considers the use of third-party development components when designing the system, and the interface of this component is different from our own system interface, and we do not need to change our own interface in order to cater for it, even though we are developing In the design stage, you can also consider using the adapter mode to solve the problems of different interfaces.


The following is the Rockets game, the code to assign offensive and defensive tasks to guards, centers, and forwards when the coach calls a timeout

Defenders, centers, and forwards are all players, so there should be a player abstract class with methods for offense and defense.











3. .NET application of adapter mode

In .NET there is a very important adapter that the class library has implemented, that is DataAdapter, DataAdapter is used as an adapter between DataSet and data source to retrieve and save data. The DataAdapter provides this adapter by mapping the Fill (this changes the data in the DataSet to match the data in the data source) and Update (this changes the data in the Data Garden to match the data in the DataSet) classes.


If the problem of different interfaces can be prevented in advance, the problem of mismatch will not occur; when there is a small problem of inconsistent interfaces, refactoring in time will prevent the problem from expanding; only when the original design and code cannot be changed. Only consider adaptation at the time of the event, the control after the event is not as good as the control during the event, and the control during the event is not as good as the control before the event.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325433188&siteId=291194637