组合模式

眉间皱痕 提交于 2020-03-10 22:15:03

也称为整体-部分模式, 通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示
作用: 使客户端对单个对象和组合对象保持一致的方式处理。属于结构型模式

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

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("不支持打印操作");
    }


}
public class Course extends CourseComponent{

    private String name;

    private double price;

    public Course(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        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 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 void removeChild(CourseComponent catalogComponent) {
        items.remove(catalogComponent);
    }

    @Override
    public String getName(CourseComponent catalogComponent) {
        return this.name;
    }

    @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();
        }
    }
}
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设计模式", 1580);
    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();

}

 

================================================================================

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<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 : 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 static void main(String[] args) {

    System.out.println("============安全组合模式===========");

    File qq = new File("QQ.exe");
    File weChat = new File("weChat.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(weChat);
    root.add(office);

    System.out.println("=============show()方法=============");
    root.show();
    System.out.println("=============list()方法=============");
    root.list();

}

源码中的运用 HashMap的putAll方法;ArrayList的addAll方法;

Mybatis中的: WhereSqlNode

优点:
    1、清楚地定义分层次的复杂对象, 表示对象的全部或部分层次
    2、让客户端忽略了层次的差异, 方便对整个层次结构进行控制
    3、简化了客户端代码
    4、符合开闭原则
缺点:
    1、限制类型时会较为复杂
    2、使设计变得更加抽象

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!