当前位置:网站首页>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
}
边栏推荐
- Go path package
- 中国SaaS产业全景图谱
- Weekend book: power BI data visualization practice
- vivo大规模 Kubernetes 集群自动化运维实践
- Programming principles
- Wait for someone with "source" | openharmony growth plan student challenge registration to start
- 大O记法解释
- 测试人员必须掌握的测试用例
- Spark source code (I) how spark submit submits jars and configuration parameters to spark server
- Node-RED系列(二四):在Node-RED中使用mysql节点实现数据库的增删改查
猜你喜欢

检验冗余码是否出错题型解法-摘录

ASCII码值是怎么计算的,怎么计算arccos的值

全栈开发实战|SSM框架整合开发

Matplotlib learning notes

index查list 注入的是mysql 执行的是oracle

The first laravel workflow engine released the official version of v1.0

信息文档管理与配置管理

Matlab hub motor analysis fuzzy PID control vertical vibration analysis

Install Kubernetes 1.24

【ELM分类】基于粒子群优化卷积神经网络CNN结合极限学习机ELM实现数据分类附matlab代码
随机推荐
首个Laravel工作流引擎发布 V1.0正式版
Redundancy code question type -- the difference between adding 0 after
信息文档管理与配置管理
[elm classification] data classification based on particle swarm optimization convolution neural network CNN combined with limit learning machine elm with matlab code
Solution to qt5.12 unable to input Chinese (unable to switch Chinese input method) in deepin system
Apple zoom! It's done so well
第一章 第一节
WebRTC服务端工程实践和优化探索
go-zero微服务实战系列(三、API定义和表结构设计)
C# Oracle 多表查询
It was so simple to implement system call
Node-RED系列(二四):在Node-RED中使用mysql节点实现数据库的增删改查
记几次略有意思的 XSS 漏洞发现
Introduction to knowledge map
Idea remote debugging jar submitted by spark submit
Pytorch basis (II) -- tensor and gradient
On the exploitation of a horizontal ultra vires vulnerability
Docker部署Mysql
Oracle custom data type question
JS local storage