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
 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章