组合模式(Composite Mode):组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。这种模式创建了一个包含自己对象组的类,该类提供了修改相同对象组的方式。
目录
1. 意图:
又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象
定义:将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
2. 如何使用:
如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。树枝内部组合该接口,并且含有内部属性list里面放Component.
优点:1.高层模块调用简单。2.节点自由增加。
缺点:1.在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
2.多个嵌套很容易出错,最好有个树状图,然后根据树状图来完成初始化。最后在校验一下,避免出错。
使用场景:部分,整体场景。如树形菜单,文件,文件夹的管理。
注意事项:定义时为具体类。
3. UML模板:
4. 代码:
public class CompositeMode : MonoBehaviour
{
void Start()
{
//生成树根root,跟上长出两叶LeafA和LeafB
Composite root = new Composite("root");
root.Add(new Leaf("Leaf A"));
root.Add(new Leaf("Leaf B"));
//跟上长出分支CompositeX,分支上也有两叶 LeafXA和LeafXB
Composite comp = new Composite("Composite X");
comp.Add(new Leaf("Leaf XA"));
comp.Add(new Leaf("Leaf XB"));
root.Add(comp);
//在Composite X上再长出分支CompositeXY,分支上也有两叶LeafXYA和LeafXYB
Composite comp2 = new Composite("Composite XY");
comp2.Add(new Leaf("Leaf XYA"));
comp2.Add(new Leaf("Leaf XYB"));
comp.Add(comp2);
root.Add(new Leaf("Leaf C"));
//根部又长出两叶LeafC和LeafD,可惜LeafD没长牢,被风吹走了
Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf);
//显示大树的样子
root.Display(1);
}
}
public abstract class Component
{
protected string name;
public Component(string name)
{
this.name = name;
}
//通常都用Add和Remove方法来提供增加或移除树叶或树枝的功能
public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int depth);
}
public class Leaf : Component
{
public Leaf(string name) : base(name)
{
}
//由于叶子没有再增加分支和树叶,所以Add和Remove方法实现它没有意义,但这样做可以消除
//叶节点和枝节点对象在抽象层次的区别,它们具备完全一致的接口
public override void Add(Component c)
{
Debug.Log("Cannot add to a leaf");
}
//叶节点的具体方法,此处是显示其名称和级别
public override void Display(int depth)
{
Debug.Log(new string('-',depth)+name);
}
public override void Remove(Component c)
{
Debug.Log("Cannot remove from a leaf");
}
}
public class Composite : Component
{
//一个子对象集合用来存储其下属的枝节点和叶节点
private List<Component> children = new List<Component>();
public Composite(string name):base(name)
{
}
public override void Add(Component c)
{
children.Add(c);
}
public override void Display(int depth)
{
Debug.Log(new string('/',depth)+name);
foreach (Component component in children)//显示其节点名称,并对其下级进行遍历
{
component.Display(depth+2);
}
}
public override void Remove(Component c)
{
children.Remove(c);
}
}
5. 实例:
UML图
代码
public class CompositeModeDemo : MonoBehaviour
{
void Start()
{
ConcreteCompany root = new ConcreteCompany("北京总公司");
root.Add(new HRDepartment("总公司人力资源部"));
root.Add(new FinanceDepartment("总公司财务部"));
ConcreteCompany comp = new ConcreteCompany("上海华东分公司");
comp.Add(new HRDepartment("华东分公司人力资源部"));
comp.Add(new FinanceDepartment("华东分公司财务部"));
root.Add(comp);
ConcreteCompany comp1 = new ConcreteCompany("南京办事处");
comp1.Add(new HRDepartment("南京办事处人力资源部"));
comp1.Add(new FinanceDepartment("南京办事处财务部"));
comp.Add(comp1);
ConcreteCompany comp2 = new ConcreteCompany("杭州办事处");
comp2.Add(new HRDepartment("杭州办事处人力资源部"));
comp2.Add(new FinanceDepartment("杭州办事处财务部"));
comp.Add(comp2);
Debug.Log("\n结构图:");
root.Display(1);
Debug.Log("\n职责:");
root.LineOfDuty();
}
}
public abstract class Company
{
protected string name;
public Company(string name)
{
this.name = name;
}
public abstract void Add(Company c);//增加
public abstract void Remove(Company c);//移除
public abstract void Display(int depth);//显示
//增加"旅行职责"方法,不同的部门需履行不同的职责
public abstract void LineOfDuty();// 旅行职责
}
public class ConcreteCompany : Company
{
private List<Company> children = new List<Company>();
public ConcreteCompany(string name) : base(name)
{
}
public override void Add(Company c)
{
children.Add(c);
}
public override void Remove(Company c)
{
children.Remove(c);
}
public override void Display(int depth)
{
Debug.Log(new string('-', depth) + name);
foreach (Company component in children)
{
component.Display(depth + 2);
}
}
//履行职责
public override void LineOfDuty()
{
foreach (Company component in children)
{
component.LineOfDuty();
}
}
}
public class HRDepartment : Company
{
public HRDepartment(string name) : base(name) { }
public override void Add(Company c)
{
}
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Debug.Log(new String('-', depth) + name);
}
public override void LineOfDuty()
{
Debug.LogFormat("{0}员工招聘培训管理", name);
}
}
public class FinanceDepartment : Company
{
public FinanceDepartment(string name) : base(name)
{
}
public override void Add(Company c)
{
}
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Debug.Log(new String('-', depth) + name);
}
public override void LineOfDuty()
{
Debug.LogFormat("{0}公司财务收支管理", name);
}
}