本文主要是介绍【设计模式】分公司是一个部门? ---- 组合模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一,概述
概念:将对象组合成树形结构以表示“部分整体”的层次结构。组合模式使得用户对单个对象和使用具有一致性。
Client:客户端
Component:组合模式中对象接口声明,在适当情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理其子部件。
SubMenu:表示节点对象,但是没有子节点。
Composite:定义有枝节点的行为,用来存储子部件,实现Component接口中与子部件有关的操作,比如增加和删除。
二,基本代码(C#)
由于C++中STL --- list 对于抽象对象的识别和支持跟C##不同,没有写成C++的形式。
不能由基类声明的抽象方法,调用子类的具体实现!!!
using System;
using System.Collections.Generic;
using System.Text;namespace 组合模式
{class Program{static void Main(string[] args){Composite root = new Composite("root");root.Add(new Leaf("Leaf A"));root.Add(new Leaf("Leaf B"));Composite comp = new Composite("Composite X");comp.Add(new Leaf("Leaf XA"));comp.Add(new Leaf("Leaf XB"));root.Add(comp);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"));Leaf leaf = new Leaf("Leaf D");root.Add(leaf);root.Remove(leaf);root.Display(1);Console.Read();}}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);}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 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);}}}class Leaf : Component{public Leaf(string name): base(name){ }public override void Add(Component c){Console.WriteLine("Cannot add to a leaf");}public override void Remove(Component c){Console.WriteLine("Cannot remove from a leaf");}public override void Display(int depth){Console.WriteLine(new String('-', depth) + name);}}
}
三,公司管理系统 采用组合模式
using System;
using System.Collections.Generic;
using System.Text;namespace 组合模式
{class Program{static void Main(string[] args){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);Console.WriteLine("\n结构图:");root.Display(1);Console.WriteLine("\n职责:");root.LineOfDuty();Console.Read();}}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();//履行职责}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){Console.WriteLine(new String('-', depth) + name);foreach (Company component in children){component.Display(depth + 2);}}//履行职责public override void LineOfDuty(){foreach (Company component in children){component.LineOfDuty();}}}//人力资源部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) + name);}public override void LineOfDuty(){Console.WriteLine("{0} 员工招聘培训管理", name);}}//财务部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) + name);}public override void LineOfDuty(){Console.WriteLine("{0} 公司财务收支管理", name);}}
}
这篇关于【设计模式】分公司是一个部门? ---- 组合模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!