博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式之组合模式
阅读量:3915 次
发布时间:2019-05-23

本文共 5646 字,大约阅读时间需要 18 分钟。

定义:组合模式(Compasite Pattern)也称为整体-部分(Part-Whole)模式,它的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示

作用:使客户端对单个对象和组合对象保持一致的方式处理

属于结构型模式

适用场景

1.希望客户端可以忽略组合对象与单个对象的差异时;
2.对象层次具备整体和部分,呈树形结构(如树形菜单,操作系统目录结构,公司组织架构)

优点

1.清楚地定义分层次的复杂对象,表示对象的全部或部分层次
2.让客户端忽略了层次的差异,方便对整个层次结构进行控制
3.简化客户端代码
4.符合开闭原则

缺点

1.限制类型时会较为复杂
2.使设计变得更加抽象

例子

透明模式
public abstract class CourseComponent {
public void addChild(CourseComponent component){
throw new UnsupportedOperationException("不支持添加操作"); } public void removeChild(CourseComponent component){
throw new UnsupportedOperationException("不支持删除操作"); } public String getName(CourseComponent component){
throw new UnsupportedOperationException("不支持获取名称操作"); } public double getPrice(CourseComponent component){
throw new UnsupportedOperationException("不支持获取价格操作"); } public void print(){
throw new UnsupportedOperationException("不支持打印操作"); }}
public class CoursePage extends CourseComponent{
private List
itmes = new ArrayList<>(); private String name; private Integer level; public CoursePage(String name, Integer level) {
this.name = name; this.level = level; } @Override public void addChild(CourseComponent component) {
itmes.add(component); } @Override public void removeChild(CourseComponent component) {
itmes.remove(component); } @Override public String getName(CourseComponent component) {
return this.name; } @Override public void print() {
System.out.println(this.name); for(CourseComponent component1 : itmes){
if(this.level != null){
for(int i = 0; i < this.level; i ++){
System.out.print(" "); } for(int i = 0 ; i < this.level; i ++){
if(i == 0){
System.out.print("+"); } System.out.print("-"); } } component1.print(); } }}
public class Course extends CourseComponent{
private String name; private double price; public Course(String name, double price) {
this.name = name; this.price = price; } @Override public String getName(CourseComponent component) {
return this.name; } @Override public double getPrice(CourseComponent component) {
return this.price; } @Override public void print() {
System.out.println(name + ",价格: " + price + "元"); }}
public class Test {
public static void main(String[] args) {
System.out.println("======透明的组合模式=========="); CourseComponent javaBase = new Course("Java入门课程",6300); CourseComponent ai = new Course("人工智能",5000); CourseComponent packageCourse = new CoursePage("Java架构师课程",2); CourseComponent design = new Course("Java设计模式",1500); CourseComponent source = new Course("源码分析",3000); CourseComponent softSkill = new Course("软技能",3000); packageCourse.addChild(design); packageCourse.addChild(source); packageCourse.addChild(softSkill); CourseComponent catalog = new CoursePage("my课程目录",1); catalog.addChild(javaBase); catalog.addChild(ai); catalog.addChild(packageCourse); catalog.print(); }}
安全模式
public abstract class Directory {
protected String name; public Directory(String name){
this.name = name; } public abstract void show();}
public class File extends Directory{
public File(String name) {
super(name); } @Override public void show() {
System.out.println(this.name); }}
public class Folder extends Directory{
private List
dirs ; private Integer level; public Folder(String name,Integer level) {
super(name); this.level = level; this.dirs = new ArrayList<>(); } @Override public void show() {
System.out.println(this.name); for(Directory directory : dirs){
if(this.level != null){
for(int i = 0; i < this.level; i ++){
System.out.print(" "); } for(int i = 0 ; i < this.level; i ++){
if(i == 0){
System.out.print("+"); } System.out.print("-"); } } directory.show(); } } public boolean add(Directory dir){
return this.dirs.add(dir); } public boolean remove(Directory dir){
return this.dirs.remove(dir); } public Directory get(int index){
return this.dirs.get(index); } public void list(){
for(Directory dir : dirs){
System.out.println(dir.name); } }}
public class Test {
public static void main(String[] args) {
System.out.println("==========安全模式写法========="); File qq = new File("qq.exe"); File wx = new File("微信.exe"); Folder office = new Folder("办公软件",2); File word = new File("Word.exe"); File ppt = new File("PowerPoint.exe"); File excel = new File("Excel.exe"); office.add(word); office.add(ppt); office.add(excel); Folder root = new Folder("/opt/",1); root.add(qq); root.add(wx); root.add(office); System.out.println("=======show()========"); root.show(); System.out.println("======list()==========="); root.list(); }}

转载地址:http://skjrn.baihongyu.com/

你可能感兴趣的文章
接口幂等设计探索实践
查看>>
微服务很香--麻辣味,但要慢慢消化
查看>>
asp.net core 使用 TestServer 来做集成测试
查看>>
解锁环境变量在云原生应用中的各种姿势
查看>>
分享我的写作经验
查看>>
Azure 静态 web 应用集成 Azure 函数 API
查看>>
关于.NET5在IIS中部署的几个问题总结
查看>>
Wifi6网络
查看>>
真实经历:整整一年了,他是这样从程序员转型做产品经理的
查看>>
互联网时代供应链
查看>>
WPF 使用 Expression Design 画图导出及使用 Path 画图
查看>>
使用BeetleX访问redis服务
查看>>
.NET 应用如何优雅的做功能开关(Feature Flag)
查看>>
如何踢掉 sql 语句中的尾巴,我用 C# 苦思了五种办法
查看>>
从零开始实现 ASP.NET Core MVC 的插件式开发(九) - 如何启用预编译视图
查看>>
.NET应用如何优雅的实现功能定时开关
查看>>
netcore一键部署到linux服务器以服务方式后台运行
查看>>
从 3.1 到 5.0 —— OpenReservation 更新记
查看>>
还在犹豫是否迁移.NET5?这几个项目已经上线了!
查看>>
Kuma 1.0 GA发布,70多项新功能和改进
查看>>