Combination model-unified company structure

  Composite mode definition : Combine objects into a tree structure to represent the "part-whole" hierarchical structure. The combined mode enables users to have consistency in the use of single objects and combined objects.

  Use scenario :

  • When reflecting the partial and overall hierarchical structure
  • Ignore the difference between composite objects and single thinking, and use all objects in the composite structure uniformly (wholes and parts can be treated uniformly)

  Category :

  • Transparent method: The child inherits the parent and has all the methods of the parent class, so that there is no difference between the child class and the child class to the outside world, and has a completely consistent behavior interface (the method that allows overriding is meaningless).
  • Safe way: Subclass 1 inherits from the parent and does not declare meaningless methods; Subclass 2 inherits from the parent and can declare all parent methods and is meaningful. Here, subclass 1 and subclass 2 have different interfaces. When called by the client Need to make corresponding judgments.

  Code background : The head office and branch companies share a set of management systems. Both the head office and the branch have human and financial departments. One system is used by the entire company and requires a clear structure and consistent functions.
Insert picture description here

Abstract company category: There are functions to add and delete subordinate branches, as well as to display and perform duties.

    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();//履行职责
    }

Specific company category: used to implement interfaces and develop subordinate branches

    //具体公司类
    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();
            }
        }
    }

Corporate department: Whether it is a head office or a branch, there must be two departments: human resources and finance.

    //人力资源部
    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);
        }
    }

Client:

        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("南京办事处财务部"));
            root.Add(comp1);//总部添加办事处的结构

            ConcreteCompany comp2 = new ConcreteCompany("杭州办事处");
            comp2.Add(new HRDepartment("杭州办事处人力资源部"));
            comp2.Add(new FinanceDepartment("杭州办事处财务部"));
            root.Add(comp2);//总部添加办事处的结构

            Console.WriteLine("\n结构图:");
            root.Display(1);

            Console.WriteLine("\n职责:");
            root.LineOfDuty();

            Console.Read();

        }

Show results:
Insert picture description here

Guess you like

Origin blog.csdn.net/CharmaineXia/article/details/111057257