【設計模式學習筆記】組合模式
阿新 • • 發佈:2019-01-08
組合模式,適用於樹狀結構。模式有三個角色:組合部件,合成模組,葉子。組合部件是組合其餘兩者的父類。它是一個提供公共屬性,方法的抽象類。合成模組用於合成最終的組合物件,葉子節點是組合部件的底層,也就相當於樹枝與樹葉。該模式又為安全模式和透明模式。兩者卻別在於父類是否提供用於合成元件的所有方法。
文章目錄
好了,我們的公司最近招人。那麼就以人事的結構為例吧。
首先zhangxu是這個虛擬公司的BOSS。下面有兩個經理:lisi,wangwu。還有幾個招聘的小弟。
1 透明模式:
1.2 抽象組合部件
package composite.compositiontransparent; import java.util.ArrayList; import java.util.List; public abstract class Component { protected String name; private List<Component> list = new ArrayList<>(); public abstract void addSubNode(Component component); public void getInfo() { System.out.println(this.name); } public List<Component> getList() { return list; } public void setList(List<Component> list) { this.list = list; } }
1.3 合成模組
package composite.compositiontransparent;
public class Composite extends Component{
@Override
public void addSubNode(Component component) {
super.getList().add(component);
}
public Composite(String name) {
super.name = name;
}
}
1.4 葉子
package composite.compositiontransparent;
public class Leaf extends Component {
@Override
public void addSubNode(Component component) {
System.out.println("葉子節點不能新增節點");
}
public Leaf(String name) {
super.name = name;
}
}
1.5 分配人員並顯示
package composite.compositiontransparent;
public class Test {
public static void main(String[] args) {
Component component1 = new Composite("zhangxu");
Component component2 = new Composite("lisi");
Component component3 = new Composite("wangwu");
Component component4 = new Leaf("xiaozhen");
Component component5 = new Leaf("xiaochen");
Component component6 = new Leaf("xiaochenchen");
component1.addSubNode(component2);
component1.addSubNode(component3);
component2.addSubNode(component4);
component3.addSubNode(component5);
component1.addSubNode(component6);
getNodes(component1);
}
public static void getNodes(Component component) {
component.getInfo();
if(component.getList()==null || component.getList().size() == 0) {
return;
}
for(Component component2 :component.getList()) {
getNodes(component2);
}
}
}
1.6 結果
2 安全模式
2.2 抽象組合部件
package composite.compostitionsafe;
public abstract class Component {
protected String name;
protected String pname;
public void getInfo() {
System.out.println(this.name+"/屬於:"+pname);
}
}
2.3 合成模組
package composite.compostitionsafe;
import java.util.ArrayList;
import java.util.List;
public class Composite extends Component{
private List<Component> list = new ArrayList<>();
public void addSubNode(Component component) {
component.pname = this.name;
list.add(component);
}
public void getNodes(){
getInfo();
for(Component component :this.list){
if(Leaf.class.isInstance(component)){
((Leaf)component).getInfo();
continue;
}
((Composite) component).getNodes();
}
}
public Composite(String name) {
super.name = name;
}
}
2.4 葉子
package composite.compostitionsafe;
public class Leaf extends Component {
public Leaf(String name) {
super.name = name;
}
}
2.5 分配人員並顯示
package composite.compostitionsafe;
public class Test {
public static void main(String[] args) {
Composite composite1 = new Composite("zhangxu");
Composite composite2 = new Composite("lisi");
Composite composite3 = new Composite("wangwu");
Leaf leaf1 = new Leaf("xiaozhen");
Leaf leaf2 = new Leaf("xiaochen");
composite1.addSubNode(composite2);
composite1.addSubNode(composite3);
composite2.addSubNode(leaf1);
composite3.addSubNode(leaf2);
composite1.getNodes();
}
}
2.6 結果
參考
【菜鳥教程】:http://www.runoob.com/design-pattern/composite-pattern.html
本文原文地址: https://blog.csdn.net/baidu_30325009/article/details/85018521