我读设计模式之组合模式
如果需求中体现部分与整体层次的结构,并且希望用户可以忽略组合对象与单个对象的不同
统一地使用组合结构中的所有对象时,就应该考虑使用组合模式。
组合模式让客户可以一致地使用组合结构和单个对象
一般实现:
范例:TreeView的简单实现:Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///如果需求中体现部分与整体层次的结构,并且希望用户可以忽略组合对象与单个对象的不同
///统一地使用组合结构中的所有对象时,就应该考虑使用组合模式。
///组合模式让客户可以一致地使用组合结构和单个对象
///
///举例:
///TreeView的实现
namespace CompositePattern
{
class Program
{
static void Main(string[] args)
{
ConcreteCompany root = new ConcreteCompany("Century Display Ltd");
root.Add(new HRDepartment("Century Display HR"));
root.Add(new FinanceDepartment("Century Display Finance"));
ConcreteCompany childCompany = new ConcreteCompany("ShangHai Ltd");
childCompany.Add(new HRDepartment("ShangHai HR"));
childCompany.Add(new FinanceDepartment("ShangHai Finance"));
root.Add(childCompany);
root.Display(0);
Console.Read();
}
}
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 class ConcreteCompany : Company
{
private List<Company> lists = new List<Company>();
public ConcreteCompany(string name)
: base(name)
{ }
public override void Add(Company c)
{
lists.Add(c);
}
public override void Remove(Company c)
{
lists.Remove(c);
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth)+this._name);
foreach (Company c in lists)
{
c.Display(depth+2);
}
}
}
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)
{
Console.WriteLine(new string('-', depth) + this._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)
{
Console.WriteLine(new string('-', depth) + this._name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///如果需求中体现部分与整体层次的结构,并且希望用户可以忽略组合对象与单个对象的不同
///统一地使用组合结构中的所有对象时,就应该考虑使用组合模式。
///组合模式让客户可以一致地使用组合结构和单个对象
///
///举例:
///TreeView的实现
namespace CompositePattern
{
class Program
{
static void Main(string[] args)
{
ConcreteCompany root = new ConcreteCompany("Century Display Ltd");
root.Add(new HRDepartment("Century Display HR"));
root.Add(new FinanceDepartment("Century Display Finance"));
ConcreteCompany childCompany = new ConcreteCompany("ShangHai Ltd");
childCompany.Add(new HRDepartment("ShangHai HR"));
childCompany.Add(new FinanceDepartment("ShangHai Finance"));
root.Add(childCompany);
root.Display(0);
Console.Read();
}
}
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 class ConcreteCompany : Company
{
private List<Company> lists = new List<Company>();
public ConcreteCompany(string name)
: base(name)
{ }
public override void Add(Company c)
{
lists.Add(c);
}
public override void Remove(Company c)
{
lists.Remove(c);
}
public override void Display(int depth)
{
Console.WriteLine(new string('-',depth)+this._name);
foreach (Company c in lists)
{
c.Display(depth+2);
}
}
}
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)
{
Console.WriteLine(new string('-', depth) + this._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)
{
Console.WriteLine(new string('-', depth) + this._name);
}
}
}
Code
public abstract class TreeView
{
public string name;
public TreeView(string name)
{
this.name = name;
}
public abstract void Add(TreeView node);
public abstract void Remove(TreeView node);
public abstract void Show(int depth);
}
ublic class Tree:TreeView
{
List<TreeView> items = new List<TreeView>();
public Tree(string name) : base(name) { }
public override void Add(TreeView node)
{
items.Add(node);
}
public override void Remove(TreeView node)
{
items.Remove(node);
}
public override void Show(int depth)
{
Console.WriteLine(new string('-', depth) + this.name);
foreach (TreeView node in items)
{
node.Show(depth + 2);
}
}
}
class Node : TreeView
{
List<TreeView> items = new List<TreeView>();
public Node(string name) : base(name) { }
public override void Add(TreeView node)
{
items.Add(node);
}
public override void Remove(TreeView node)
{
items.Remove(node);
}
public override void Show(int depth)
{
Console.WriteLine(new string('-', depth) + this.name);
foreach (TreeView node in items)
{
node.Show(depth + 2);
}
}
}
class Demo1
{
static void Main()
{
Tree tree = new Tree("Company");
Node node = new Node("IS&T");
tree.Add(node);
Node child = new Node("ISD");
node.Add(child);
child = new Node("EAS");
node.Add(child);
child = new Node("CIM");
node.Add(child);
node = new Node("HR");
tree.Add(node);
tree.Show(0);
Console.Read();
}
}
public abstract class TreeView
{
public string name;
public TreeView(string name)
{
this.name = name;
}
public abstract void Add(TreeView node);
public abstract void Remove(TreeView node);
public abstract void Show(int depth);
}
ublic class Tree:TreeView
{
List<TreeView> items = new List<TreeView>();
public Tree(string name) : base(name) { }
public override void Add(TreeView node)
{
items.Add(node);
}
public override void Remove(TreeView node)
{
items.Remove(node);
}
public override void Show(int depth)
{
Console.WriteLine(new string('-', depth) + this.name);
foreach (TreeView node in items)
{
node.Show(depth + 2);
}
}
}
class Node : TreeView
{
List<TreeView> items = new List<TreeView>();
public Node(string name) : base(name) { }
public override void Add(TreeView node)
{
items.Add(node);
}
public override void Remove(TreeView node)
{
items.Remove(node);
}
public override void Show(int depth)
{
Console.WriteLine(new string('-', depth) + this.name);
foreach (TreeView node in items)
{
node.Show(depth + 2);
}
}
}
class Demo1
{
static void Main()
{
Tree tree = new Tree("Company");
Node node = new Node("IS&T");
tree.Add(node);
Node child = new Node("ISD");
node.Add(child);
child = new Node("EAS");
node.Add(child);
child = new Node("CIM");
node.Add(child);
node = new Node("HR");
tree.Add(node);
tree.Show(0);
Console.Read();
}
}