创作背景:各位朋友,我们继续C#编程模式的探讨,这次探讨的模式是组合模式。它和桥接模式一样,是一种结构型设计模式,允许使用者将对象组合成树形结构来展示其“部分和整体”的层次结构。要求同样比较严格,用户对单个对象和组合对象的使用需具有一致性。
组合模式我的理解就像是俄罗斯套娃,大圈套小圈😃😃。上代码!!!
1、Component管理类,也可以当作节点使用。
/// <summary>
/// 抽象一个类,管理Component的子部件
/// </summary>
public abstract class Component
{
protected string name;
public Component(string name) { this.name = name; }
/// <summary>
/// 增加节点
/// </summary>
/// <param name="component"></param>
public abstract void Add(Component component);
/// <summary>
/// 移除节点
/// </summary>
/// <param name="component"></param>
public abstract void Remove(Component component);
/// <summary>
/// 显示结构
/// </summary>
public abstract string Display(int level);
}
2、Leaf末端节点类。
/// <summary>
/// 叶子节点(无增加节点和删除节点功能)
/// </summary>
public class Leaf : Component
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
public Leaf(string name) : base(name) { }
public override void Add(Component component) { }
public override void Remove(Component component) { }
/// <summary>
/// 显示层级
/// </summary>
/// <param name="level"></param>
public override string Display(int level)
{
string strResult = string.Empty;
strResult = (new string('-', level) + name);
return strResult;
}
}
3、Composite组合类。
/// <summary>
/// 定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作
/// </summary>
public class Composite : Component
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
public Composite(string name) : base(name) { }
/// <summary>
/// 一个子对象集合,用来存储其下属的枝节点和叶节点
/// </summary>
private List<Component> children = new List<Component>();
/// <summary>
/// 增加子节点
/// </summary>
/// <param name="component"></param>
public override void Add(Component component)
{
children.Add(component);
}
/// <summary>
/// 移除子节点
/// </summary>
/// <param name="component"></param>
public override void Remove(Component component)
{
children.Remove(component);
}
/// <summary>
/// 遍历所有节点,显示层级结构
/// </summary>
/// <param name="level"></param>
/// <returns></returns>
public override string Display(int level)
{
string strResult = string.Empty;
strResult = (new string('-', level) + name);
// 遍历其子节点并显示
foreach (Component component in children)
{
strResult += "\r\n" + component.Display(level + 2);
}
return strResult;
}
}
4、客户端调用。
/// <summary>
/// 组合模式测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_comb_Click(object sender, EventArgs e)
{
// 生成树根,并为其增加两个叶子节点
Combination.Component root = new Composite("Root");
root.Add(new Leaf("Leaf A in Root"));
root.Add(new Leaf("Leaf B in Root"));
// 为根增加两个枝节点
Combination.Component branchX = new Composite("Branch X in Root");
Combination.Component branchY = new Composite("Branch Y in Root");
root.Add(branchX);
root.Add(branchY);
// 为BranchX增加页节点
branchX.Add(new Leaf("Leaf A in Branch X"));
// 为BranchX增加枝节点
Combination.Component branchZ = new Composite("Branch Z in Branch X");
branchX.Add(branchZ);
// 为BranchY增加叶节点
branchY.Add(new Leaf("Leaf in Branch Y"));
// 为BranchZ增加叶节点
branchZ.Add(new Leaf("Leaf in Branch Z"));
// 显示树
tbox_Comb.Text = root.Display(1);
}
5、程序运行截图。
以上就是小编对组合模式的浅浅的理解,希望对各位有所帮助。