欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#组合模式实例详解

程序员文章站 2023-11-20 22:40:40
本文实例讲述了c#组合模式。分享给大家供大家参考。具体如下: company.cs如下: using system; using system...

本文实例讲述了c#组合模式。分享给大家供大家参考。具体如下:

company.cs如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication1
{
  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 abstract void lineofduty();
  }
}

concretecompany.cs如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication1
{
  public 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();
      }
    }
  }
}

financedepartment.cs如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication1
{
  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)+name);
    }
    public override void lineofduty()
    {
      console.writeline("{0} 财务支付管理",name);
    }
  }
}

hrdepartment.cs如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication1
{
  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)+name);
    }
    public override void lineofduty()
    {
      console.writeline("{0} 招聘培训管理",name);
    }
  }
}

program.cs如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication1
{
  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);
      root.display(1);
      console.readkey();
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。