当前位置:网站首页>修饰模式和代理模式的异同
修饰模式和代理模式的异同
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(); // 糖 咖啡
}
边栏推荐
- Tree and binary tree: basic operation and implementation of binary tree
- 【动态规划】入门篇
- (dp+ memory) acwing 901 skiing
- 攻防世界-PWN-shell
- Storage mode of drawings
- Class template
- Summary of string, vector and array learning
- [51nod p2106] an odd number of times [bit operation]
- Amadahl's Law (a little thought)
- turtle库的使用数字时钟模拟时钟动态显示
猜你喜欢
![[51nod p3216] Award [bit operation]](/img/f2/109fb126d026951ffd766781223052.jpg)
[51nod p3216] Award [bit operation]

SQL ROW_ The number() function uses
![[51nod p3395] n-bit gray code [bit operation]](/img/91/e127e68ae2897933b795414d50c70f.jpg)
[51nod p3395] n-bit gray code [bit operation]

C language: file operation
![[ssl1280] full arrangement](/img/58/85c456127a406bf5b30ee1d204981d.jpg)
[ssl1280] full arrangement

(state compression dp+good) acwing 291 Mondrian's dream

Jenkins accédant à l'authentification de l'utilisateur openldap
![[51nod p2106] an odd number of times [bit operation]](/img/f4/600395cd0ecb44235c65805f379bd4.jpg)
[51nod p2106] an odd number of times [bit operation]
![[implementation of depth first search]](/img/10/4f150e4fa0d4edf01483a72b881afe.jpg)
[implementation of depth first search]

The rise of cloud computing enterprises and the shaking of Oracle database market dominance
随机推荐
Alibaba senior experts analyze the standard design of protocol
Haproxy + keepalived for high availability load balancing of MySQL
C language: file operation
VDD,DVDD,AVDD,VCC,AFVDD,DOVDD,IOVDD
Zigzag transformation
LeetCode 5289. 公平分发饼干(DFS)
LeetCode 6098. Count the number of subarrays whose score is less than K (prefix and + binary search)
C language: dynamic memory management
Amadahl's Law (a little thought)
架构师必备:系统容量现状checklist
[51nod p2673] shortest path [heap optimization Dijk]
英国出台粮食安全计划抵御粮食供应危机
(dfs+ tree DP) acwing 846 Center of gravity of tree
Exercise 8-3 rotate the array to the right (20 points)
LeetCode 583. Deleting two strings
说说MySQL索引机制
Jenkins accédant à l'authentification de l'utilisateur openldap
Figure: concept of figure
拜登:希望尽快签署两党枪支安全改革法案
(topological sorting +bfs) acwing 848 Topological sequence of digraph