设计模式实践-组合模式
场景
站点和仪表目录。
实现代码
组合抽象类:
namespace DesignPatterns.Composite
{
/// <summary>
/// 站点组合抽象
/// </summary>
public abstract class SiteComponent
{
/// <summary>
/// 组合名称
/// </summary>
protected string Name;
/// <summary>
/// Initializes a new instance of the <see cref="SiteComponent" /> class.
/// </summary>
/// <param name="name">组合名称</param>
protected SiteComponent(string name)
{
this.Name = name;
}
/// <summary>
/// 添加组合
/// </summary>
/// <param name="siteComponent">组合对象</param>
public abstract void Add(SiteComponent siteComponent);
/// <summary>
/// 移除组合
/// </summary>
/// <param name="siteComponent">组合对象</param>
public abstract void Remove(SiteComponent siteComponent);
/// <summary>
/// 显示组合下的所有元素
/// </summary>
/// <param name="depth">本节点深度</param>
public abstract void Display(int depth);
}
}
站点类
namespace DesignPatterns.Composite
{
/// <summary>
/// 站点组合类
/// </summary>
public class Site : SiteComponent
{
/// <summary>
/// 子站点列表
/// </summary>
private readonly List<SiteComponent> _sublist = new List<SiteComponent>();
/// <summary>
/// Initializes a new instance of the <see cref="Site" /> class.
/// </summary>
/// <param name="name">站点名称</param>
public Site(string name) : base(name)
{
}
/// <summary>
/// 添加子站点组合
/// </summary>
/// <param name="siteComponent">组合对象</param>
public override void Add(SiteComponent siteComponent)
{
this._sublist.Add(siteComponent);
}
/// <summary>
/// 删除子站点组合
/// </summary>
/// <param name="siteComponent">组合对象</param>
public override void Remove(SiteComponent siteComponent)
{
this._sublist.Remove(siteComponent);
}
/// <summary>
/// 显示组合下的所有元素
/// </summary>
/// <param name="depth">本节点深度</param>
public override void Display(int depth)
{
Console.WriteLine(new string('-', depth) + this.Name);
foreach (var site in this._sublist)
{
site.Display(depth + 2);
}
}
}
}
仪表类
namespace DesignPatterns.Composite
{
/// <summary>
/// 仪表组合类
/// </summary>
public class Meter : SiteComponent
{
/// <summary>
/// Initializes a new instance of the <see cref="Meter" /> class.
/// </summary>
/// <param name="name">仪表名称</param>
public Meter(string name) : base(name)
{
}
public override void Add(SiteComponent siteComponent)
{
throw new NotImplementedException();
}
public override void Remove(SiteComponent siteComponent)
{
throw new NotImplementedException();
}
/// <summary>
/// 显示本节点
/// </summary>
/// <param name="depth">本节点深度</param>
public override void Display(int depth)
{
Console.WriteLine(new string('-', depth) + this.Name);
}
}
}
相关调用
SiteComponent rootSite = new Site("总站");
SiteComponent otherSite01 = new Site("分站1");
otherSite01.Add(new Meter("仪表1"));
otherSite01.Add(new Meter("仪表2"));
rootSite.Add(otherSite01);
rootSite.Add(new Site("分站2"));
rootSite.Add(new Site("分站3"));
SiteComponent otherSite02 = new Site("分站4");
otherSite02.Add(new Meter("仪表1"));
otherSite02.Add(new Meter("仪表2"));
otherSite02.Add(new Meter("仪表3"));
otherSite02.Add(new Meter("仪表4"));
otherSite02.Add(new Meter("仪表5"));
rootSite.Add(otherSite02);
rootSite.Display(1);
Out:
-总站
---分站1
-----仪表1
-----仪表2
---分站2
---分站3
---分站4
-----仪表1
-----仪表2
-----仪表3
-----仪表4
-----仪表5