基本概念:
定义:将对象组合成树形结构以表示“部分-整体”的层次结构
组合模式使客户端对单个对象和组合对象保持一致的方式处理。
适用场景
希望客户端可以忽略组合对象与单个对象的差异时
处理一个树形结构时
优点
清楚地定义分层次的复杂对象
让客户端忽略了层次的差异,方便对整个层次结构进行控制
符合开闭原则,在组合模式中增加新的含有子节点的节点或叶子结点的时候都很方便,无需对现有的类库进行任何修改。
缺点
限制类型时会较为复杂
使设计变得更加抽象
背景:
图片.png现在需要将整个目录打印出来,该怎么实现?
分析:
图片.png
CourseCatalog和Course继承同一个抽象类是为了
List<CatalogComponent> items = new ArrayList<CatalogComponent>();
可以把课程或者目录放到List里头去。将课程和目录当做是同一类组件处理。抽象类:
package com.geely.design.pattern.structural.composite;
/**
* Created by geely
*/
public abstract class CatalogComponent {
public void add(CatalogComponent catalogComponent){
throw new UnsupportedOperationException("不支持添加操作");
}
public void remove(CatalogComponent catalogComponent){
throw new UnsupportedOperationException("不支持删除操作");
}
public String getName(CatalogComponent catalogComponent){
throw new UnsupportedOperationException("不支持获取名称操作");
}
public double getPrice(CatalogComponent catalogComponent){
throw new UnsupportedOperationException("不支持获取价格操作");
}
public void print(){
throw new UnsupportedOperationException("不支持打印操作");
}
}
课程类:
package com.geely.design.pattern.structural.composite;
/**
* Created by geely
*/
public class Course extends CatalogComponent {
private String name;
private double price;
public Course(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String getName(CatalogComponent catalogComponent) {
return this.name;
}
@Override
public double getPrice(CatalogComponent catalogComponent) {
return this.price;
}
@Override
public void print() {
System.out.println("Course Name:"+name+" Price:"+price);
}
}
课程目录类:
package com.geely.design.pattern.structural.composite;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: wenbaipei
* @Date: 2019/11/14 19:28
* @Version 1.0
*/
public class CourseCatalog extends CatalogComponent {
private List<CatalogComponent> items = new ArrayList<CatalogComponent>();
private String name;
public CourseCatalog(String name) {
this.name = name;
}
@Override
public void add(CatalogComponent catalogComponent) {
items.add(catalogComponent);
}
@Override
public void remove(CatalogComponent catalogComponent) {
items.remove(catalogComponent);
}
@Override
public void print() {
for (CatalogComponent catalogComponent : items){
catalogComponent.print();
}
}
@Override
public String getName(CatalogComponent catalogComponent) {
return this.name;
}
}
测试类:
package com.geely.design.pattern.structural.composite;
/**
* Created by geely
*/
public class Test {
public static void main(String[] args) {
CatalogComponent linuxCourse = new Course("Linux课程",11);
CatalogComponent windowsCourse = new Course("Windows课程",11);
CatalogComponent javaCourseCatalog = new CourseCatalog("Java课程目录");
CatalogComponent mmallCourse1 = new Course("Java电商一期",55);
CatalogComponent mmallCourse2 = new Course("Java电商二期",66);
CatalogComponent designPattern = new Course("Java设计模式",77);
javaCourseCatalog.add(mmallCourse1);
javaCourseCatalog.add(mmallCourse2);
javaCourseCatalog.add(designPattern);
//javaCourseCatalog.add(javaCourseCatalog);
CatalogComponent imoocMainCourseCatalog = new CourseCatalog("慕课网课程主目录");
imoocMainCourseCatalog.add(linuxCourse);
imoocMainCourseCatalog.add(windowsCourse);
imoocMainCourseCatalog.add(javaCourseCatalog);
imoocMainCourseCatalog.print();
}
}
结果:
图片.png
问题:
在输出中只是并排地显示,没有从属关系。
修改:
@Override
public void print() {
System.out.println(this.name);
for (CatalogComponent catalogComponent : items){
System.out.print(" ");
catalogComponent.print();
}
}
结果:
图片.png
问题:
JAVA课程目录下的课程没有再缩进一格。
修改:
package com.geely.design.pattern.structural.composite;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: wenbaipei
* @Date: 2019/11/14 19:28
* @Version 1.0
*/
public class CourseCatalog extends CatalogComponent {
private List<CatalogComponent> items = new ArrayList<CatalogComponent>();
private String name;
private Integer level;
public CourseCatalog(String name, Integer level) {
this.name = name;
this.level = level;
}
@Override
public void add(CatalogComponent catalogComponent) {
items.add(catalogComponent);
}
@Override
public void remove(CatalogComponent catalogComponent) {
items.remove(catalogComponent);
}
@Override
public void print() {
System.out.println(this.name);
for (CatalogComponent catalogComponent : items) {
for (int i = 0; i < this.level; i++) {
System.out.print(" ");
}
catalogComponent.print();
}
}
@Override
public String getName(CatalogComponent catalogComponent) {
return this.name;
}
}
图片.png
源码
组合模式是将相同的抽象类的类型或者接口的类型,这两种类型的实例,转换为树状结构。
图片.png
网友评论