Design Patterns - Proxy Pattern and Decorative Design Pattern

1. Proxy mode and decorative design mode

                                                                                    - I've never really trusted my memory, so I write them all down

 

    Why write these two modes together? Because the proxy mode and the decorative design mode are stupidly unclear. After reading a lot of articles, they only say that the difference between the two is: the decorative design mode is to add functions, and the proxy mode is to limit functions . It's so hard to understand, I believe this blog will definitely help you clearly distinguish between decoration design pattern and proxy pattern. The following describes the decoration design pattern and the proxy pattern respectively, and then explains the difference between the two in detail.

 

1. Decorative Design Patterns

Decorative Design Pattern : Dynamically extend the functionality of an object without having to change the original class file and use inheritance. It wraps the real object by creating a wrapper object, which is a decoration.

Description: Extend the original class (that is, add the function mentioned above).

 

For example, there is such a requirement: there is a function of sending information, and now it is necessary to add the sending time before sending the information.

    Here is the original function:

/**Information interface*/
interface Message{
   public void sendMsg(String content);
}

/**send messages*/
class Sms implements Message{
    public void sendMsg(String content){
        System.out.println("Information content: "+content);
    }
}

/**Simulate sending message test*/
class Test{
    @Test
    public void test(){
        Message msg = Sms();
        msg.sendMsg("You are so funny today!");//The console prints out the message content
    }
}

   

    Below is the code after adding the function

//Message interface and Sms class are the same as above;

//below is the decoration object
class Sms_new implements Message{
    Sms sms;
    public Sms_new(Sms sms){
        this.sms = sms;
    }
    public void sendMsg(String content){
        System.out.println("Current time: "+new Date());
        sms.sendMsg(content);
    }
}

/**Simulate sending message test*/
class Test{
    @Test
    public void test(){
        Message msg = Sms_new(new Sms());
        msg.sendMsg("You are so funny today!");//The console prints out the current time and information content
    }
}

 Did you find it, the decoration mode only adds a layer of decoration to the original class (increases the display time function), and does not have control over the original class. For example, subvert the function of the original class. The proxy mode is that the proxy class has a reference to the original class, the initiative is in the hands of the proxy class, and it has the function of determining the original class. look at proxy mode

 

2. Proxy mode

    

Proxy design pattern : Control access to objects without having to change the original class file and use inheritance.

Description: The decoration design pattern emphasizes extension, and the proxy pattern emphasizes control.

The same is the example of the decorated pattern above, we modify the decoration class; it immediately becomes the proxy design pattern

// Only send messages to members
class Sms_new implements Message{
    Sms sms;
    public Sms_new(Sms sms){
        this.sms = sms;
    }
    public void sendMsg(String content){
        System.out.println("Current time: "+new Date());
        if(isVip()){
              sms.sendMsg(content);
        }they{
              System.out.println("Sorry, you don't have permission"):
        }
        
    }
}

 The control that the proxy mode emphasizes is like the above example, the functions of the proxy class can not be performed by the proxy class. At this point, I believe you have basically understood the difference between the decorator pattern and the proxy pattern.

      Here is just a brief description of the concepts and differences between the two modes. There are still many difficulties in practical application. For example, the proxy mode is divided into static proxy and dynamic proxy. The practicality of static proxy is not very high, just like the examples we listed above. It is also difficult to study the dynamic proxy thoroughly. At present, there are two implementation methods: JDK method and CGLIB method. I'll write this later

 

    

Guess you like

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