定义
组合模式也成为整体-部分模式,它的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口表示,使得客户对单个对象和组合对象的使用具有一致性,属于结构型模式。
组合模式包含的三个角色:
抽象根节点:定义系统各层次对象共有的方法和属性,可以预定义一些默认的行为和属性。
树枝节点:定义树枝节点的行为,储存子节点,组合树枝节点和叶子节点形成一个树形结构。
叶子节点:叶子节点对象,其下再无分支,是系统层次遍历的最小单位。
组合模式,在代码的具体实现上,有两种不同的方式,分别是透明组合模式和安全组合模式。
透明组合模式
比如要设计一个咕泡课程的关系结构,有AI课程,架构师课程,前端课程,架构师课程里面又包含设计模式,源码分析,软技能。这三个技能和架构师课程是属于整体和部分的关系,就可以用组合模式来设计。
先建立一个顶层的抽象组件,CourseComponent类。
public abstract class CourseComponent {
public void addChild(CourseComponent courseComponent){
throw new UnsupportedOperationException("不支持添加子节点操作");
}
public void removeChild(CourseComponent courseComponent){
throw new UnsupportedOperationException("不支持删除子节点操作");
}
public String getName(CourseComponent courseComponent){
throw new UnsupportedOperationException("不支持获取名字操作");
}
public double getPrice(CourseComponent courseComponent){
throw new UnsupportedOperationException("不支持获取价格操作");
}
public void print(){
throw new UnsupportedOperationException("不支持打印操作");
}
}
这边为什么不用抽象的方法呢,是因为用了抽象的方法,其子类就必须要实现这些方法,这样就体现不出各个子类的差异出来,所以,子类继承这个抽象类后,只需要重写有差异的方法覆盖这些方法即可。
下面创建课程类Course,和课程包CoursePackage类。
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 courseComponent) {
return this.name;
}
@Override
public double getPrice(CourseComponent courseComponent) {
return this.price;
}
@Override
public void print() {
System.out.println(name + ",价格:" + price + "元" );
}
}
import java.util.ArrayList;
import java.util.List;
public class CoursePackage extends CourseComponent {
private List<CourseComponent> items = new ArrayList<CourseComponent>();
private String name;
private Integer level;
public CoursePackage(String name, Integer level) {
this.name = name;
this.level = level;
}
@Override
public void addChild(CourseComponent courseComponent) {
items.add(courseComponent);
}
@Override
public void removeChild(CourseComponent courseComponent) {
items.remove(courseComponent);
}
@Override
public String getName(CourseComponent courseComponent) {
return this.name;
}
@Override
public void print() {
System.out.println(this.name);
for (CourseComponent item : 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("-");
}
}
item.print();
}
}
}
编写测试代码。
public class Test {
public static void main(String[] args) {
System.out.println("========透明混合模式=======");
CourseComponent javaBase = new Course("Java入门课程", 8280);
CourseComponent ai = new Course("人工智能", 5000);
CourseComponent coursePackage = new CoursePackage("架构师课程", 2);
CourseComponent design = new Course("设计模式",2000);
CourseComponent web = new Course("web课程",3000);
CourseComponent python = new Course("python",4000);
coursePackage.addChild(design);
coursePackage.addChild(web);
coursePackage.addChild(python);
CourseComponent catalog = new CoursePackage("课程主目录",1);
catalog.addChild(javaBase);
catalog.addChild(ai);
catalog.addChild(coursePackage);
catalog.print();
}
}
查看结果。
透明组合模式把所有公共的方法都定义在Component里面,这么做的好处是客户端无需辨别是叶子节点还是树枝节点,他们具备完全一致的接口。缺点是叶子节点会继承一些它不需要的接口,这与设计模式的接口隔离原则相违背。
安全组合模式
安全组合模式是只规定系统各个层级中最一致的行为,而把组合自身的方法放在各自当中。
举个例子,电脑中的文件夹下面可以存放文件和文件夹,所以文件就是树枝节点,文件夹就是子节点。这里用安全组合模式来实现目录结构。先创建最顶层的抽象组件Directory类,提供抽象的show方法。
public abstract class Directory {
protected String name;
public Directory(String name){
this.name = name;
}
public abstract void show();
}
再分别创建File类和Folder类。
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<Directory> dirs;
private Integer level;
public Folder(String name, Integer level) {
super(name);
this.level = level;
this.dirs = new ArrayList<Directory>();
}
@Override
public void show() {
System.out.println(this.name);
for (Directory dir : this.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("-");
}
}
//打印名称
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);
}
}
}
看看测试代码。
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();
}
}
查看结果。
安全组合模式的好处是接口定义职责清晰,符合设计模式 单一职责原则和接口隔离原则,缺点是客户端需要区分树枝节点和叶子节点,这样才能正确处理各个层级的操作,客户端无法依赖抽象,违背了设计模式的依赖倒置原则。