Java设计模式之18 ——组合模式

组合模式,也称为部分整体模式。是一种结构型设计模式。在组合模式中,将对象组合成树形结构以表示“部分--整体”的层次结构,使得银行对单个对象和组合对象的使用具有一致性。

一般的,在表示对象的部分-整体层次结构时,使用组合模式。或者在从一个整体中能独立出部分模块或功能的场景。下面我们先设计一组安全的组合模式。

 

1

package compositepatten;

public abstract class Component {

    protected String name;

    public Component(String name) {
        super();
        this.name = name;
    }
    public abstract void doSomething();
}

2

package compositepatten;

import java.util.ArrayList;
import java.util.List;

public class Composite extends Component{

    private List<Component> components = new ArrayList<>();
    
    public Composite(String name) {
        super(name);
    }
    public void addChild(Component component){
        components.add(component);
    }
    public Component getChild(int i){
        return components.get(i);
    }
    public void removeChild(Component component){
        components.remove(component);
    }
    @Override
    public void doSomething() {
        System.out.println(name);
        if (null != components) {
            for (Component c : components) {
                c.doSomething();
            }
        }
    }
}
 

3

package compositepatten;

public class Leaf extends Component{

    public Leaf(String name) {
        super(name);
    }
    @Override
    public void doSomething() {
        System.out.println(name);
    }
}
 

4

package compositepatten;

public class Client {

    public static void main(String[] args) {
        //构造一个根节点
        Composite root = new Composite("ROOT");
        //构造两个支节点
        Composite branch1 = new Composite("BRANCH1");
        Composite branch2 = new Composite("BRANCH2");
        //构造两个叶子节点
        Leaf leaf1 = new Leaf("LEAF1");
        Leaf leaf2 = new Leaf("LEAF2");
        
        //将叶子节点添加到支节点中
        branch1.addChild(leaf1);
        branch2.addChild(leaf2);
        //将支节点添加到根节点中
        root.addChild(branch1);
        root.addChild(branch2);
        //执行方法
        root.doSomething();
    }
}
 

输出结果:

ROOT
BRANCH1
LEAF1
BRANCH2
LEAF2

还有一种透明的组合方式,

1.

package compositepatten;

public abstract class Component {

    protected String name;

    public Component(String name) {
        super();
        this.name = name;
    }
    public abstract void doSomething();
    public abstract void addChild(Component component);
    public abstract Component getChild(int i);

    public abstract void removeChild(Component component);
}
 

2.

package compositepatten;

import java.util.ArrayList;
import java.util.List;

public class Composite extends Component{

    private List<Component> components = new ArrayList<>();
    
    public Composite(String name) {
        super(name);
    }
    public void addChild(Component component){
        components.add(component);
    }
    public Component getChild(int i){
        return components.get(i);
    }
    public void removeChild(Component component){
        components.remove(component);
    }
    @Override
    public void doSomething() {
        System.out.println(name);
        if (null != components) {
            for (Component c : components) {
                c.doSomething();
            }
        }
    }
}
 

3.

package compositepatten;

public class Leaf extends Component{

    private String message = "叶子节点没有子节点";
    public Leaf(String name) {
        super(name);
    }
    @Override
    public void doSomething() {
        System.out.println(name);
    }
    @Override
    public void addChild(Component component) {
        throw new UnsupportedOperationException(message);
        
    }
    @Override
    public Component getChild(int i) {
        throw new UnsupportedOperationException(message);
    }
    @Override
    public void removeChild(Component component) {
        throw new UnsupportedOperationException(message);
        
    }
}
4.

package compositepatten;

public class Client {

    public static void main(String[] args) {
        //构造一个根节点
//        Composite root = new Composite("ROOT");
        //
        Component root = new Composite("ROOT");
        //构造两个支节点
        Composite branch1 = new Composite("BRANCH1");
        Composite branch2 = new Composite("BRANCH2");
        //构造两个叶子节点
        Leaf leaf1 = new Leaf("LEAF1");
        Leaf leaf2 = new Leaf("LEAF2");
        
        //将叶子节点添加到支节点中
        branch1.addChild(leaf1);
        branch2.addChild(leaf2);
        //将支节点添加到根节点中
        root.addChild(branch1);
        root.addChild(branch2);
        //执行方法
        root.doSomething();
    }
}
 

输出结果: 

ROOT
BRANCH1
LEAF1
BRANCH2
LEAF2
 

猜你喜欢

转载自blog.csdn.net/frank_develpoer/article/details/81566139