23 Design Patterns (5) - Adapter Pattern

definition:

Convert the interface of a class to another interface that the client wants. The adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.

 

Role:

Target role: This is the expected interface, that is, this type of interface meets our requirements.

 

  Source (Adapee) role: the interface we want to use, but this interface does not meet our requirements, that is, the interface that needs to be adapted now.

 

Adapter (Adaper) role: The adapter class is the core of the adapter pattern. The adapter converts the source interface to the target interface. Obviously, this role cannot be an interface, but must be a concrete class.

 

Classification:

1,   class adapter mode

class Adaptee {         
   publicvoid specificRequest() {         System.out.println("特殊请求,这个是源角色");    } }
/*这个是目标角色,所期待的接口*/

interface Target {        
   publicvoid request(); }

   Now I want to implement this Target interface, but I don't want to refactor, I want to use the existing Adaptee class, then you can define an adapter class, inherit the class you want to use, and implement the expected interface.

class Adapter extends Adaptee implementsTarget{
         publicvoid request() {
                   super.specificRequest();
         }
}


This way, using the adapter class and implementing the target interface completes the plan, test:

public class Test{
         publicstatic void main(String[] args) {
                   //使用特殊功能类,即适配类
                   Targetadapter = new Adapter();
                   adapter.request();
         }
}


2.   Object Adapter Pattern

The adapter class is associated with the existing Adaptee class and implements the standard interface. The advantage of this is that inheritance is no longer required.

class Adapter implements Target{
         privateAdaptee adaptee;

         publicAdapter (Adaptee adaptee) {
                   this.adaptee= adaptee;
         }

         publicvoid request() {
                   this.adaptee.specificRequest();
         }
}


We can imagine that the output result is the same as the class adapter pattern at this time, test:

public class Test{
         publicstatic void main(String[] args) {
                   Targetadapter = new Adapter(new Adaptee());
                   adapter.request();
         }
}

the difference:

Instead of using inheritance to connect to Adaptee classes,   the adapter pattern for objects uses delegation to connect to Adaptee classes.

 

advantage:

 

reusability

The system needs to use an existing class, and the interface of this class does not meet the needs of the system. Then these functions can be better reused through the adapter mode.

 

Extensibility

When implementing the adapter function, you can freely call the functions developed by yourself, so as to naturally expand the functions of the system.

 

shortcoming:

    Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that the A interface is called, but it is actually adapted to the implementation of the B interface. Therefore, the adapter mode is not suitable for using it in the detailed design stage. It is a compensation mode, which is specially used for later expansion and modification of the system.

 

Applicable scene:

 

1. The interface of the existing class does not meet our needs;

 

2. Create a reusable class so that the class can work with other unrelated or unforeseen classes;

 

3. Use some existing subclass without subclassing it to match the interface.

 

4. The class developed by the old system has already implemented some functions, but the client can only access it in the form of another interface, but we don't want to manually change the original class.

 

summary:

 

The adapter mode is not suitable for using it in the detailed design stage. It is a compensation mode, which is specially used for later expansion and modification of the system. The adapter mode is more like a remedy.


Reprinted from: https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247483839&idx=1&sn=68787de393fe59d7b1d09a85595a17d7&scene=21#wechat_redirect

Guess you like

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