当前位置:网站首页>修饰模式和代理模式的异同
修饰模式和代理模式的异同
2022-06-13 09:25:00 【edui】
- 修饰模式和代理模式的异同
对装饰器模式来说,装饰者(Decorator)和被装饰者(Decoratee)都实现一个接口。对代理模式来说,代理类(Proxy Class)和真实处理的类(Real Class)都实现同一个接口。此外,不论我们使用哪一个模式,都可以很容易地在真实对象的方法前面或者后面加上自定义的方法。
不同点在于代理模式是直接编写一个代理类实现相同的接口,真实类被隐藏,修饰模式则是需要传入一个人被修饰类,不被隐藏,可以用一个类送去给多个修饰类去修饰
从思路上讲,代理模式是强调访问控制辅组什么的功能,修饰模式是增强功能的动态的,动态就是说修饰模式是对被修饰类的增强可以选择多个不同的修饰类,传入被修饰的对象岂可。代理对象用的是自己创造的被代理对象类,进行代替工作,而修饰类则是用的参数传过来的修饰对象对其进行增强功能增加额外功能,执行的时候代理类直接执行,外界来看是不知道被代理类的存在的,而修饰类执行需要传入被代理对象,执行的还是被代理的对象使用者需要传入被修饰的对象所以知道被修饰者的存在并且修饰不对原来的对象产生影响。
主要区分点还是对于需要加功能的对象的来源,是在实现的类内部创造还是传入,即对使用加功能类来说被加功能类是否需要自己操心的关心来源的。
参考文章《装饰器模式和代理模式的区别》
- 上代码感悟
基础类
点咖啡
public interface Coffee {
/** * 打印当前咖啡里有什么 */
void printMaterial();
}
public class BitterCoffee implements Coffee {
@Override
public void printMaterial() {
System.out.println("咖啡");
}
}
@Test
public void orderCoffee {
Coffee coffee = new BitterCoffee();
coffee.printMaterial(); // 咖啡
}
你喝了一口咖啡,觉得有点苦,于是你就想加点糖。
定义一个咖啡装饰器(加糖)。
public class CoffeeDecorator implements Coffee {
/** * 持有一个咖啡对象 */
private final Coffee coffee;
public CoffeeDecorator(Coffee coffee) {
this.coffee = coffee;
}
@Override
public void printMaterial() {
System.out.println("糖");
this.coffee.printMaterial();
}
}
@Test
public void addSugerIntoCoffee {
Coffee coffee = new BitterCoffee(); // 点了一杯苦咖啡
coffee = new SugarDecorator(coffee); // 给咖啡加了点糖
coffee.printMaterial(); // 糖 咖啡
}
约好的朋友来了,要给她点一杯咖啡,你知道咖啡很苦,决定直接点一杯加了糖的咖啡给她。
定义一个加糖咖啡的类(代理点咖啡这个行为)。
public class CoffeeProxy implements Coffee {
private final Coffee coffee;
public CoffeeProxy() {
this.coffee = new BitterCoffee();
}
@Override
public void printMaterial() {
System.out.println("糖");
this.coffee.printMaterial();
}
}
@Test
public void addSugerIntoCoffee {
Coffee coffee = new CoffeeProxy();
coffee.printMaterial(); // 糖 咖啡
}
边栏推荐
- LeetCode 583. Deleting two strings
- 1-4 message passing interface [CSP authentication]
- C language: five custom types
- Exploitation of competitive loopholes in attacking and defending world PWN play conditions
- LeetCode 6097. Match after replacing characters (Dictionary)
- 云计算企业崛起 甲骨文数据库市场主导地位动摇
- SpEL表达式 简单使用
- Alibaba senior experts analyze the standard design of protocol
- Sort() sort function
- VGA common resolution and calculation method
猜你喜欢
turtle库的使用数字时钟模拟时钟动态显示
[51nod p2106] an odd number of times [bit operation]
Trees and binary trees: traversal of binary trees
(bfs) acwing 847. Hierarchy of points in the graph
Classes and objects -- object model and this pointer
Standard template library (STL)
C language: preprocessing in program environment
(tree DP) acwing 285 A dance without a boss
LeetCode 1. Sum of two numbers
云计算企业崛起 甲骨文数据库市场主导地位动摇
随机推荐
C # introductory series (XIII) -- getting to know the structure for the first time
[Luogu p1090, ssl1040] merged fruit [pile]
删除软链接
ROS2之OpenCV人脸识别foxy~galactic~humble
VGA常用分辨率及计算方法
[51nod p2102] or subtraction and [bit operation]
Alibaba senior experts analyze the standard design of protocol
acwing 789. Range of numbers (dichotomy + suitable for understanding dichotomy boundary)
(dp+ memory) acwing 901 skiing
VDD, dvdd, avdd, VCC, afvdd, dovdd, iovdd
Jenkins accédant à l'authentification de l'utilisateur openldap
[implementation of depth first search]
Delete soft link
MySQL事务隔离级别和MVCC
Zigzag transformation
@Value does not take effect and extend/implement other classes cannot inject beans manually
(bfs) acwing 844. Labyrinth
Heap
(dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem
Classes and objects -- object model and this pointer