一,理解
1,书上的说法:
将对象组合成属性结构以表示”部分和整体“的层次结构。组合模式得使用使用户单个对象和组合对象的使用具有一致性。
2,自己的看法:
组合模式能使单个和组合的使用具有一致性
二,用法:
1,什么时候用?
(1)当发现去求重视体现部分与整体层次的结构的时候,
(2)你希望客户可以忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时,就应该考虑用组合模式了。
2,如何用?
(1)添加一个部分和总体都有的方法的类,让他们继承
3,重点是什么?
(1)会有一公分独享集合用来寻出其下属的枝节点和叶节点
(2)树枝和树叶实现接口统一
4,优缺点是什么?
(1)优点:
方便了对单个对象和组合对象的使用
可以自由加节点
(2)缺点:
使用了继承,而不是接口,违反了依赖倒置原则
依赖倒置原则:
- 高层模块不应该依赖低层模块,两者都应该依赖抽象
- 抽象不应该依赖细节
- 细节应该依赖抽象
5,其他相关的举例:
(1)文件夹的分类,文件夹下的文件的管理等
6,透明方式和安全方式:
(1)透明方式:
指在页节点不能实现接口所有的方法,这样是为了让他们都有相同的行为接口,但有些方法对他不能使用,没有意义的。
(2)安全方法:
在接口中只有叶节点中的所有方法,枝节点中有其他的方法,但是这样接口就不能统一,也给客户端带来了不便。
三,代码展示:
例子是一棵树有叶子还有树枝,如何显示树的结构:
1,写一个有相同方法的抽象类component:
abstract class Component
{
protected string name;
public Component (string name)
{
this.name = name;
}
public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int depth);
}
2,写一个树叶的节点的类,并继承Component:
class Leap:Component
{
public Leap(string name)
: base(name)
{ }
public override void Add(Component c)
{
Console.WriteLine("不支持添加页");
}
public override void Remove(Component c)
{
Console.WriteLine("不支持迁移页");
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
}
}
3,写一个枝节点的类,用来存储子部件和事项与子部件有关的操作
class Compsite : Component
{
private List<Component> children = new List<Component>();
public Compsite (string name)
: base (name)
{}
public override void Add(Component c)
{
children.Add(c);
}
public override void Remove(Component c)
{
children.Remove(c);
}
public override void Display(int depth)//显示节点名称,并对下级进行遍历
{
Console.WriteLine(new String('-',depth)+name );
foreach (Component component in children )
{
component.Display(depth +2);
}
}
}
4,客户端代码:
static void Main(string[] args)
{
Compsite root = new Compsite("root");
root.Add(new Leap("leafA"));
root.Add(new Leap("leafB"));
Compsite comp = new Compsite("component");
comp.Add(new Leap("leafXA"));
comp.Add(new Leap("leafXB"));
root.Add(comp);
root.Display(1);
Console.ReadKey();
}