当前位置:网站首页>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
}
边栏推荐
猜你喜欢
Electrolytic capacitor, tantalum capacitor, ordinary capacitor
ASCII码值是怎么计算的,怎么计算arccos的值
Necessary for Architects: system capacity status checklist
等个有“源”人|OpenHarmony 成长计划学生挑战赛报名启动
Webrtc server engineering practice and optimization exploration
冗余码题型--后面加0的区别
QTcpServer. QTcpSocket. Differences between qudpsockets
[bearing fault decomposition] ITD bearing fault signal decomposition based on MATLAB [including Matlab source code 1871]
Cynthia项目缺陷管理系统
Matlab hub motor analysis fuzzy PID control vertical vibration analysis
随机推荐
2022年尾矿复训题库及模拟考试
二叉树简介
On the exploitation of a horizontal ultra vires vulnerability
WIN7无法被远程桌面问题
Go zero microservice Practice Series (III. API definition and table structure design)
电解电容、钽电容、普通电容
检验冗余码是否出错题型解法-摘录
Smart210 uses SD card to burn uboot
[cloud enjoying freshness] community weekly · vol.66- Huawei partners and Developers Conference 2022 wonderful agenda announcement
2022年劳务员-通用基础(劳务员)上岗证题目及答案
六月集训(第13天) —— 双向链表
The first laravel workflow engine released the official version of v1.0
Vivo large scale kubernetes cluster automation operation and maintenance practice
Index query list injects MySQL and executes Oracle
Redis初始安装和使用【玩转华为云】
【云享新鲜】社区周刊·Vol.66-华为伙伴暨开发者大会2022精彩议程公布
Node red series (27): instructions for S7 node of the extension node
Design of simple electronic clock based on single chip microcomputer
Interrupt handling mechanism
IDEA 续命插件