当前位置:网站首页>Similarities and differences between decoration mode and agency mode

Similarities and differences between decoration mode and agency mode

2022-06-13 10:30:00 edui

  • The similarities and differences between the decoration mode and the proxy mode

For decorator mode , Decorator (Decorator) And the decorator (Decoratee) All implement an interface . For the proxy model , proxy class (Proxy Class) And real processing classes (Real Class) All implement the same interface . Besides , No matter which mode we use , It is easy to add custom methods before or after the methods of real objects .
The difference is that the proxy pattern is to directly write a proxy class to implement the same interface , Real classes are hidden , Modification mode is to pass in a modified class , Not hidden , You can send one class to multiple decorating classes to decorate
In terms of thinking , The proxy mode emphasizes the function of access control subgroup , Decorating patterns are dynamic enhancements , Dynamic means that the modification mode is an enhancement of the modified class. You can select multiple different modified classes , Pass in the modified object . The proxy object uses its own created proxy object class , Carry out substitute work , The decorated class is used to enhance its functions and add additional functions with the modified object passed from the parameter , When executing, the proxy class directly executes , The outside world does not know the existence of the proxy class , The decorated class needs to pass in the proxied object , The user needs to pass in the modified object, so he knows the existence of the modified object and the modification will not affect the original object .

The main distinguishing point is the source of the object that needs to add functions , Is it created inside the implemented class or passed in , That is, whether the added function class needs to worry about the source of concern for the use of the added function class .
Reference article 《 The difference between decorator mode and proxy mode

  • Understanding of upper code
    Foundation class
    Order coffee
public interface Coffee {
    
    /** *  Print what's in the current coffee  */
    void printMaterial();
}

public class BitterCoffee implements Coffee {
    
    @Override
    public void printMaterial() {
    
        System.out.println(" coffee ");
    }
}

@Test
public void orderCoffee {
    
    Coffee coffee = new BitterCoffee();
    coffee.printMaterial(); //  coffee 
}

You took a sip of coffee , Feel a little bitter , So you want to add some sugar .

Define a coffee decorator ( Add sugar ).

public class CoffeeDecorator implements Coffee {
    
    /** *  Hold a coffee object  */
    private final Coffee coffee;

    public CoffeeDecorator(Coffee coffee) {
    
        this.coffee = coffee;
    }

    @Override
    public void printMaterial() {
    
        System.out.println(" sugar ");
        this.coffee.printMaterial();
    }
}

@Test
public void addSugerIntoCoffee {
    
    Coffee coffee = new BitterCoffee(); //  Ordered a cup of bitter coffee 
    coffee = new SugarDecorator(coffee); //  Add some sugar to the coffee 
    coffee.printMaterial(); //  sugar   coffee 
}

An appointment is coming , I want to order her a cup of coffee , You know coffee is bitter , Decided to order her a cup of coffee with sugar .

Define a sugar coffee class ( The act of ordering coffee ).

public class CoffeeProxy implements Coffee {
    
    private final Coffee coffee;

    public CoffeeProxy() {
    
        this.coffee = new BitterCoffee();
    }

    @Override
    public void printMaterial() {
    
        System.out.println(" sugar ");
        this.coffee.printMaterial();
    }
}

@Test
public void addSugerIntoCoffee {
    
    Coffee coffee = new CoffeeProxy();
    coffee.printMaterial(); //  sugar   coffee 
}
原网站

版权声明
本文为[edui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130925197485.html