C# 设计模式(9)组合模式

组合模式

1. 运用递归

2. 组合模式:分透明和安全

代码实现:

项目提成

namespace CompositePattern
{
    public abstract class AbstractDomain
    {
        public string Name { get; set; }
        public int Percent { get; set; }
        public abstract void Commission(double money);
    }
    class Domain:AbstractDomain
    {
        List<Domain> domainChildList = new List<Domain>();

        public void AddChild(Domain domainChild)
        {
            this.domainChildList.Add(domainChild);
        }

        public override void Commission(double money)
        {
            double ret = money * this.Percent / 100;
            Console.WriteLine($"{this.Name} can get {ret} RMB");

            foreach (var domainChild in domainChildList)
            {
                domainChild.Commission(ret);
            }
        }
    }
}

代码调用:

    class Program
    {
        static void Main(string[] args)
        {
            double totalMoney = 1000000;
            var domains = BuildTree();
            domains.Commission(totalMoney);
        }

        private static Domain BuildTree()
        {

            Domain domain = new Domain()
            {
                Name = "能有的提成收入",
                Percent = 10
            };

            #region
            Domain domain1 = new Domain()
            {
                Name = "CEO",
                Percent = 30
            };
            Domain domain2 = new Domain()
            {
                Name = "各部门共有",
                Percent = 70
            };

            Domain domain21 = new Domain()
            {
                Name = "实施",
                Percent = 20
            };
            Domain domain22 = new Domain()
            {
                Name = "测试",
                Percent = 10
            };
            Domain domain23 = new Domain()
            {
                Name = "销售",
                Percent = 30
            };
            Domain domain24 = new Domain()
            {
                Name = "开发",
                Percent = 40
            };
            Domain domain241 = new Domain()
            {
                Name = "经理",
                Percent = 20
            };
            Domain domain242 = new Domain()
            {
                Name = "主管",
                Percent = 15
            };
            Domain domain243 = new Domain()
            {
                Name = "开发团队",
                Percent = 65
            };
            Domain domain2431 = new Domain()
            {
                Name = "项目组1",
                Percent = 50
            };
            Domain domain2432 = new Domain()
            {
                Name = "项目组2",
                Percent = 50
            };
            Domain domain24321 = new Domain()
            {
                Name = "项目经理",
                Percent = 20
            };
            Domain domain24322 = new Domain()
            {
                Name = "开发人员",
                Percent = 80
            };
            Domain domain243221 = new Domain()
            {
                Name = "高级开发人员",
                Percent = 40
            };
            Domain domain243222 = new Domain()
            {
                Name = "中级开发人员",
                Percent = 30
            };
            Domain domain243223 = new Domain()
            {
                Name = "初级开发人员",
                Percent = 20
            };
            Domain domain243224 = new Domain()
            {
                Name = "实习生",
                Percent = 10
            };
            Domain domain2432241 = new Domain()
            {
                Name = "实习生1",
                Percent = 25
            };
            Domain domain2432242 = new Domain()
            {
                Name = "实习生2",
                Percent = 25
            };
            Domain domain2432243 = new Domain()
            {
                Name = "实习生3",
                Percent = 25
            };
            Domain domain2432244 = new Domain()
            {
                Name = "实习生4",
                Percent = 25
            };
            domain243224.AddChild(domain2432241);
            domain243224.AddChild(domain2432242);
            domain243224.AddChild(domain2432243);
            domain243224.AddChild(domain2432244);

            domain24322.AddChild(domain243221);
            domain24322.AddChild(domain243222);
            domain24322.AddChild(domain243223);
            domain24322.AddChild(domain243224);

            domain2432.AddChild(domain24321);
            domain2432.AddChild(domain24322);

            domain243.AddChild(domain2431);
            domain243.AddChild(domain2432);

            domain24.AddChild(domain241);
            domain24.AddChild(domain242);
            domain24.AddChild(domain243);

            domain2.AddChild(domain21);
            domain2.AddChild(domain22);
            domain2.AddChild(domain23);
            domain2.AddChild(domain24);

            domain.AddChild(domain1);
            domain.AddChild(domain2);
            #endregion

            return domain;
        }
    }

结果:

 

posted @ 2020-12-02 14:35  NiKaFace  阅读(177)  评论(0编辑  收藏  举报