设计模式-组合模式

组合模式

定义

​ 组合(Composite Pattern)模式的定义:有时又叫作整体-部分(Part-Whole)模式,它是一种将对象组合成树状的层次结构的模式,用来表示“整体-部分”的关系,使用户对单个对象和组合对象具有一致的访问性,属于结构型设计模式

特点

​ 在组合模式中,整个树形结构中的对象都属于同一种类型,带来的好处就是用户不需要辨别是树枝节点还是叶子节点,可以直接进行操作,给用户的使用带来极大的便利。

组合模式的主要优点有:

  1. 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;

  2. 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;
    其主要缺点是:

  3. 设计较复杂,客户端需要花更多时间理清类之间的层次关系;

  4. 不容易限制容器中的构件;

  5. 不容易用继承的方法来增加构件的新功能;

结构

组合模式包含以下主要角色。

  1. 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。(总的抽象类或接口,定义一些通用的方法,比如新增、删除)
  2. 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于继承或实现抽象构件。
  3. 树枝构件(Composite)角色 / 中间构件:是组合中的分支节点对象,它有子节点,用于继承和实现抽象构件。它的主要作用是存储和管理子部件,通常包含 add()、remove() 等方法。

组合模式分为透明式的组合模式和安全式的组合模式。

实现

透明式

public abstract class Component {

    private String name;

    void add(Component component){
        throw new UnsupportedOperationException();
    }

    public Component(String name){
        this.name = name;
    }

    void remove(Component component){
        throw new UnsupportedOperationException();
    }

    abstract void print();

    public String getName() {
        return name;
    }

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

public class UnitComposite extends Component{

    public UnitComposite(String name){
        super(name);
    }

    List<Component> list = new ArrayList<Component>();

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public void setName(String name) {
        super.setName(name);
    }

    @Override
    void add(Component component){
        list.add(component);
    }
    @Override
    void remove(Component component){
       list.remove(component);
    }

    @Override
    void print() {
        System.out.println("单位名称:"+getName()+"--------------");
        list.forEach(Component::print);
    }



}
public class DeptLeaf extends Component{

 public DeptLeaf(String name){
  super(name);
 }

 @Override
 public String getName() {
  return super.getName();
 }

 @Override
 public void setName(String name) {
  super.setName(name);
 }


 @Override
 void print() {
  System.out.println("科室名称:"+getName());
 }
}
public class Client {

 public static void main(String[] args) {
     Component root = new UnitComposite("总公司");

     Component a1 = new UnitComposite("分公司1");

     Component a2 = new UnitComposite("分公司12");

     Component a3 = new UnitComposite("分公司3");

     Component leaf1 = new DeptLeaf("设计部");

     Component leaf2 = new DeptLeaf("开发部");

     Component leaf3 = new DeptLeaf("销售部");

     Component leaf4 = new DeptLeaf("财务部");

     root.add(a1);
     root.add(a3);

     a1.add(leaf1);
     a1.add(leaf2);

     a2.add(leaf3);
     a2.add(leaf4);

     a1.add(a2);

     root.print();

     //a1.print();

    // a2.print();


 }
}

安全式

类似透明式,两个类有点小变动。

  • Componet删除add remove方法
  • Client Component类型更改为UnitComposite
public abstract class Component {

    private String name;

    public Component(String name){
        this.name = name;
    }

    abstract void print();

    public String getName() {
        return name;
    }

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

 public static void main(String[] args) {
     UnitComposite root = new UnitComposite("总公司");

     UnitComposite a1 = new UnitComposite("分公司1");

     UnitComposite a2 = new UnitComposite("分公司12");

     UnitComposite a3 = new UnitComposite("分公司3");

     Component leaf1 = new DeptLeaf("设计部");

     Component leaf2 = new DeptLeaf("开发部");

     Component leaf3 = new DeptLeaf("销售部");

     Component leaf4 = new DeptLeaf("财务部");

     root.add(a1);
     root.add(a3);

     a1.add(leaf1);
     a1.add(leaf2);

     a2.add(leaf3);
     a2.add(leaf4);

     a1.add(a2);

     root.print();

     //a1.print();

    // a2.print();


 }
}
posted @ 2022-01-26 22:34  天横  阅读(71)  评论(0)    收藏  举报