当前位置:网站首页>组合模式、透明方式和安全方式
组合模式、透明方式和安全方式
2022-06-26 05:36:00 【绿毛水怪12138】
什么是组合模式
组合模式(composite):将对象组合成树形结构以表示“部分-整体”的层次关系。使得用户对单个对象和组合对象的使用具有一致性。
UML图

组合模式的使用场景
来源博客
1、如果你需要实现树状对象结构,可以考虑使用组合模式。
组合模式为你提供了两种共享公共接口的基本元素类型: 简单叶节点和复杂容器。 容器中可以包含叶节点和其他容器。 这使得你可以构建树状嵌套递归对象结构。
2、如果你希望客户端代码以相同方式处理简单和复杂元素, 可以使用该模式。
组合模式中定义的所有元素共用同一个接口。 在这一接口的帮助下, 客户端不必在意其所使用的对象的具体类。
小练习
实现一个商品树结构

package composite;
public class test {
public static void main(String[] args) {
incGoods incGoods1=new incGoods("服装");
incGoods incGoods2=new incGoods("男装");
incGoods2.add(new speGoods("衬衫"));
incGoods2.add(new speGoods("夹克"));
incGoods1.add(incGoods2);
incGoods2=new incGoods("女装");
incGoods2.add(new speGoods("裙子"));
incGoods2.add(new speGoods("套装"));
incGoods1.add(incGoods2);
incGoods1.display(1);
}
}
package composite;
public abstract class Goods {
protected String name;
public Goods(String name){
this.name=name;
}
public abstract void add(Goods goods);
public abstract void remove(Goods goods);
public abstract void display(int depth);
}
package composite;
public class speGoods extends Goods{
public speGoods(String name){
super(name);
}
@Override
public void add(Goods goods) {
// TODO Auto-generated method stub
System.out.println("不能添加商品");
}
@Override
public void remove(Goods goods) {
// TODO Auto-generated method stub
System.out.println("不能删除商品");
}
@Override
public void display(int depth) {
// TODO Auto-generated method stub
System.out.println(name+"-"+depth);
}
}
package composite;
import java.util.ArrayList;
import java.util.List;
public class incGoods extends Goods{
List<Goods> goods;
public incGoods(String name){
super(name);
goods=new ArrayList<Goods>();
}
@Override
public void add(Goods goods) {
// TODO Auto-generated method stub
this.goods.add(goods);
}
@Override
public void remove(Goods goods) {
// TODO Auto-generated method stub
goods.remove(goods);
}
@Override
public void display(int depth) {
// TODO Auto-generated method stub
System.out.println(name+"-"+depth);
for(Goods goodss:goods){
goodss.display(depth+1);
}
}
}
透明方式、安全方式
在组合模式的UML图中我们可以看到,Ledf类继承了Component类的add和remove方法,但这两个方法对于leaf类没有实现的必要(但必须要实现),这种方式减少了客户端对象处理时的判断。这叫做透明方式
安全方式,将add和remove方法在Component中除去,在compisite中直接实现。这种方式必须要在客户端对象处理时做具体判断,例如:
在透明方式下,
Component a=new Leaf();
Component b=new Composite();
b.add(a);
在安全方式下,
Leaf a=new Leaf();
Composite b=new Composite();
b.add(a);
如何选择,需要根据具体情况。
与其他设计模式的关系
桥接模式、 状态模式和策略模式 (在某种程度上包括适配器模式) 的接口非常相似。 实际上, 它们都基于组合模式——即将工作委派给其他对象, 不过也各自解决了不同的问题。 模式并不只是以特定方式组织代码的配方, 你还可以使用它们来和其他开发者讨论模式所解决的问题。
你可以在创建复杂组合树时使用建造者模式, 因为这可使其构造步骤以递归的方式运行。
责任链模式通常和组合模式结合使用。 在这种情况下, 叶组件接收到请求后, 可以将请求沿包含全体父组件的链一直传递至对象树的底部。
你可以使用迭代器模式来遍历组合树。
你可以使用访问者模式对整个组合树执行操作。
你可以使用享元模式实现组合树的共享叶节点以节省内存。
组合和装饰模式的结构图很相似, 因为两者都依赖递归组合来组织无限数量的对象。
装饰类似于组合, 但其只有一个子组件。 此外还有一个明显不同: 装饰为被封装对象添加了额外的职责, 组合仅对其子节点的结果进行了 “求和”。
但是, 模式也可以相互合作: 你可以使用装饰来扩展组合树中特定对象的行为。
大量使用组合和装饰的设计通常可从对于原型模式的使用中获益。 你可以通过该模式来复制复杂结构, 而非从零开始重新构造。
边栏推荐
- C# 40. Byte[] to hexadecimal string
- Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
- bingc(继承)
- C# 39. Conversion between string type and byte[] type (actual measurement)
- 使用Jedis監聽Redis Stream 實現消息隊列功能
- Mysql 源码阅读(二)登录连接调试
- 12 multithreading
- Written before father's Day
- How to make your big file upload stable and fast?
- LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
猜你喜欢

Uni app ceiling fixed style
![[red team] what preparations should be made to join the red team?](/img/03/f246f18f8925167dbd5e9d63912faa.png)
[red team] what preparations should be made to join the red team?

Redis usage and memory optimization
![C# 40. Byte[] to hexadecimal string](/img/3e/1b8b4e522b28eea4faca26b276a27b.png)
C# 40. Byte[] to hexadecimal string

操作符的优先级、结合性、是否控制求值顺序【详解】

Replacing domestic image sources in openwrt for soft routing (take Alibaba cloud as an example)

cartographer_ optimization_ problem_ 2d

Ribbon负载均衡服务调用

SDN based DDoS attack mitigation

【C语言】深度剖析数据在内存中的存储
随机推荐
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
skimage. morphology. medial_ axis
The news of thunderbolt
cartographer_local_trajectory_builder_2d
Tp5.0 framework PDO connection MySQL error: too many connections solution
The most refined language interprets the event dispatcher (also known as the event scheduler)
uniCloud云开发获取小程序用户openid
Leetcode513.找出树的左下角的值
慢慢学JVM之缓存行和伪共享
[MySQL] MySQL million level data paging query method and its optimization
1212312321
cross entropy loss = log softmax + nll loss
redis探索之布隆过滤器
As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
Learn cache lines and pseudo sharing of JVM slowly
Data storage: the difference between MySQL InnoDB and MyISAM
Owasp-top10 in 2021
Introduction to GUI programming to game practice (II)
Introduction to alluxio