通过代码案例研究解释组合模式(附透明组合模式、安全组合模式的代码示例)
最编程
2024-03-23 14:37:15
...
“这是我参与8月更文挑战的第8天,活动详情查看:8月更文挑战”
一、组合模式的概念和角色
(一)、组合模式的概念
组合模式也称为整体-部分模式,它的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示,使得客户对单个对象和组合对象的使用具有一致性,属于结构型模式。
组合模式一般用来描述整体和部分的关系,它将对象组织到树形结构中,最顶层的节点成为根节点,根节点下面可以包含树枝节点和叶子节点,树枝节点下面又可以包含树枝节点和叶子节点。
(二)、组合模式的角色
1、抽象根节点(Component):定义系统各层次对象的共有方法和属性,可以预先定义一些默认行为和属性。
2、树枝节点(Composite):定义树枝节点的行为,存储字节点,组合树枝节点和叶子节点形成一个树形结构。
3、叶子节点(Leaf):叶子节点对象,其下再无分支,是系统层次遍历的最小单位。
二、组合模式的应用场景
1、希望客户端可以忽略组合对象和单个对象的差异时。
2、对象层次具备整体和部分,呈树形结构。
三、透明组合模式的代码示例
CourseComponent:
public abstract class CourseComponent {
public void addChild(CourseComponent catalogComponent) {
throw new UnsupportedOperationException("不支持添加操作");
}
public void removeChild(CourseComponent catalogComponent) {
throw new UnsupportedOperationException("不支持删除操作");
}
public String getName(CourseComponent catalogComponent) {
throw new UnsupportedOperationException("不支持获取名称操作");
}
public double getPrice(CourseComponent catalogComponent) {
throw new UnsupportedOperationException("不支持获取价格操作");
}
public void print() {
throw new UnsupportedOperationException("不支持打印操作");
}
}
CoursePackage:
public class CoursePackage extends CourseComponent {
private List<CourseComponent> items = new ArrayList<>();
private String name;
private Integer level;
public CoursePackage(String name, Integer level) {
this.name = name;
this.level = level;
}
@Override
public void addChild(CourseComponent catalogComponent) {
items.add(catalogComponent);
}
@Override
public String getName(CourseComponent catalogComponent) {
return this.name;
}
@Override
public void removeChild(CourseComponent catalogComponent) {
items.remove(catalogComponent);
}
@Override
public void print() {
System.out.println(this.name);
for (CourseComponent catalogComponent : items) {
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("-");
}
}
catalogComponent.print();
}
}
}
Course:
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 catalogComponent) {
return this.name;
}
@Override
public double getPrice(CourseComponent catalogComponent) {
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入门课程", 8280);
CourseComponent ai = new Course("人工智能", 5000);
CourseComponent packageCourse = new CoursePackage("Java架构师课程", 2);
CourseComponent design = new Course("Java设计模式", 1500);
CourseComponent source = new Course("源码分析", 2000);
CourseComponent softSkill = new Course("软技能", 3000);
packageCourse.addChild(design);
packageCourse.addChild(source);
packageCourse.addChild(softSkill);
CourseComponent catalog = new CoursePackage("课程主目录", 1);
catalog.addChild(javaBase);
catalog.addChild(ai);
catalog.addChild(packageCourse);
catalog.print();
}
}
四、安全组合模式的代码示例
Directory:
public abstract class Directory {
protected String name;
public Directory(String name) {
this.name = name;
}
public abstract void show();
}
Folder:
public class Folder extends Directory {
private List<Directory> 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 dir : dirs) {
if (dir != 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("-");
}
}
dir.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 : this.dirs) {
System.out.println(dir.name);
}
}
}
File:
public class File extends Directory{
public File(String name) {
super(name);
}
@Override
public void show() {
System.out.println(this.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 wps = new Folder("金山软件", 3);
wps.add(new File("WPS.exe"));
office.add(wps);
Folder root = new Folder("根目录", 1);
root.add(qq);
root.add(wx);
root.add(office);
System.out.println("----------show()方法效果-----------");
root.show();
System.out.println("----------list()方法效果-----------");
root.list();
}
}
五、组合模式的优缺点
(一)、优点
1、清楚地定义分层次的复杂对象,表示对象的全部或部分层次
2、让客户端忽略了层次的差异,方便对整个层次结构进行控制
3、简化客户端代码
4、符合开闭原则
(二)、缺点
1、限制类型时会较为复杂
2、使设计变得更加抽象