• CSharp: Composite Pattern in donet core 3


    /// <summary>
        ///组合模式 Composite Pattern
        /// geovindu,Geovin Du edit
        /// 
        /// </summary>
        interface IEmployee
        {
     
            /// <summary>
            /// To set an employee name
            /// 姓名
            /// </summary>
            string Name { get; set; }
    
            /// <summary>
            /// To set an employee department
            /// 部门
            /// </summary>
            string Dept { get; set; }
       
    
            /// <summary>
            /// To set an employee designation
            /// 描述
            /// </summary>
            string Designation { get; set; }
      
            /// <summary>
            /// To display an employee details
            /// 清单详情
            /// </summary>
            void DisplayDetails();
    
           // string getToString();
    
        }
        /// <summary>
        /// Leaf Node
        /// </summary>
        class Employee : IEmployee
        {
            /// <summary>
            /// 姓名:
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 部门
            /// </summary>
            public string Dept { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string Designation { get; set; }
            /// <summary>
            /// 详情清单 Details of a leaf node
            /// </summary>
            public void DisplayDetails()
            {
               // Console.WriteLine($"\t姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}");
                Console.WriteLine(getToString());
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>     
            public string getToString()
            {
                return $"\t姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}";
            }
        }
        /// <summary>
        /// 父类 Non-leaf node
        /// </summary>
        class CompositeEmployee : IEmployee
        {
    
            /// <summary>
            /// 姓名:
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 部门
            /// </summary>
            public string Dept { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string Designation { get; set; }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public string getToString()
            {
                //return $"{nameof(Name)}: {Name}, {nameof(Dept)}: {Dept}, {nameof(Designation)}: {Designation}";
                return $"\t姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}";
            }
    
            /// <summary>
            /// 子类对象 The container for child objects
            /// </summary>
            private List<IEmployee> subordinateList = new List<IEmployee>();
    
            /// <summary>
            /// 添加 To add an employee
            /// </summary>
            /// <param name="e"></param>
            public void AddEmployee(IEmployee e)
            {
                subordinateList.Add(e);
            }
    
            /// <summary>
            /// 移除 To remove an employee
            /// 
            /// </summary>
            /// <param name="e"></param>
            public void RemoveEmployee(IEmployee e)
            {
                subordinateList.Remove(e);
            }
    
            /// <summary>
            /// 详情清单 Details of a composite node
            /// </summary>
            public void DisplayDetails()
            {
    
                //getToString();
                //Console.WriteLine($"\n姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}");
                Console.WriteLine(getToString());
    
                foreach (IEmployee e in subordinateList)
                {
                
                    e.DisplayDetails();
                }
            }
        }
    

      

     /// <summary>
        ///组合模式 Composite Pattern
        /// geovindu,Geovin Du edit
        /// </summary>
        public class GraphicObject
        {
    
            /// <summary>
            /// 
            /// </summary>
            public virtual string Name { get; set; } = "组";
            /// <summary>
            /// 
            /// </summary>
            public string Color;
            /// <summary>
            /// 
            /// </summary>
            private readonly Lazy<List<GraphicObject>> children
              = new Lazy<List<GraphicObject>>();
            /// <summary>
            /// 
            /// </summary>
            public List<GraphicObject> Children => children.Value;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sb"></param>
            /// <param name="depth"></param>
            private void Print(StringBuilder sb, int depth)
            {
                sb.Append(new string('*', depth))
                  .Append(string.IsNullOrWhiteSpace(Color) ? string.Empty : $"{Color} ")
                  .AppendLine($"{Name}");
                foreach (var child in Children)
                    child.Print(sb, depth + 1);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                var sb = new StringBuilder();
                Print(sb, 0);
                return sb.ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public class Circle : GraphicObject
        {
            public override string Name => "圆";
        }
        /// <summary>
        /// 
        /// </summary>
        public class Square : GraphicObject
        {
            public override string Name => "正方形";
        }
    

      

    调用:

     //组合模式
                Console.WriteLine("***组合模式Composite Pattern Demo. ***");
    
                #region Mathematics department
                //2 lecturers work in Mathematics department
                Employee mathTeacher1 = new Employee { Name = "老大", Dept = "数学", Designation = "讲师" };
                Employee mathTeacher2 = new Employee { Name = "老二", Dept = "数学", Designation = "讲师" };
    
                //The college has a Head of Department in Mathematics
                CompositeEmployee hodMaths = new CompositeEmployee { Name = "老六", Dept = "数学", Designation = "数学系主任" };
    
                //Lecturers of Mathematics directly reports to HOD-Maths
                hodMaths.AddEmployee(mathTeacher1);
                hodMaths.AddEmployee(mathTeacher2);
                #endregion
    
                #region Computer Science department
                //3 lecturers work in Computer Sc. department
                Employee cseTeacher1 = new Employee { Name = "老三", Dept = "计算机科学", Designation = "讲师" };
                Employee cseTeacher2 = new Employee { Name = "老四", Dept = "计算机科学", Designation = "讲师" };
                Employee cseTeacher3 = new Employee { Name = "老五", Dept = "计算机科学", Designation = "讲师" };
    
                //The college has a Head of Department in Computer science
                CompositeEmployee hodCompSc = new CompositeEmployee { Name = "老幺", Dept = "计算机科学.", Designation = "计算机科学系主任" };
    
                //Lecturers of Computer Sc. directly reports to HOD-CSE
                hodCompSc.AddEmployee(cseTeacher1);
                hodCompSc.AddEmployee(cseTeacher2);
                hodCompSc.AddEmployee(cseTeacher3);
                #endregion
    
                #region Top level management
                //The college also has a Principal
                CompositeEmployee principal = new CompositeEmployee { Name = "族长", Dept = "规划监督管理", Designation = "校长" };
    
                //Head of Departments's of Maths and Computer Science directly reports to Principal.
                principal.AddEmployee(hodMaths);
                principal.AddEmployee(hodCompSc);
                #endregion
    
                /* 
                 * Printing the leaf-nodes and branches in the same way.
                 * i.e. in each case, we are calling DisplayDetails() method.
                */
                Console.WriteLine("\nDetails of a Principal(校长) object is as follows:");
                //Prints the complete structure
                principal.DisplayDetails();
    
                Console.WriteLine("\nDetails of a HOD(系主任) object is as follows:");
                //Prints the details of Computer Science department
                hodCompSc.DisplayDetails();
    
                //Leaf node
                Console.WriteLine("\nDetails of an individual employee(leaf node一般教职员) is as follows:");
                mathTeacher1.DisplayDetails();
    
                /*
                 * Suppose, one Computer Science lecturer(老幺) 
                 * is leaving now from the organization.
                 */
                hodCompSc.RemoveEmployee(cseTeacher2);
                Console.WriteLine("\n成员:");
                principal.DisplayDetails();
    
    
                var drawing = new GraphicObject { Name = "我的画" };
                drawing.Children.Add(new Square { Color = "红色" });
                drawing.Children.Add(new Circle { Color = "黄色" });
    
                var group = new GraphicObject();
                group.Children.Add(new Circle { Color = "蓝色" });
                group.Children.Add(new Square { Color = "蓝色" });
                drawing.Children.Add(group);
    
                Console.WriteLine(drawing);
    

      

    输出:

    ***组合模式Composite Pattern Demo. ***
    
    Details of a Principal(校长) object is as follows:
    
    姓名:族长 工作在 规划监督管理 部门.描述:校长
    
    姓名:老六 工作在 数学 部门.描述:数学系主任
            姓名:老大 工作在 数学 部门.描述:讲师
            姓名:老二 工作在 数学 部门.描述:讲师
    
    姓名:老幺 工作在 计算机科学. 部门.描述:计算机科学系主任
            姓名:老三 工作在 计算机科学 部门.描述:讲师
            姓名:老四 工作在 计算机科学 部门.描述:讲师
            姓名:老五 工作在 计算机科学 部门.描述:讲师
    
    Details of a HOD(系主任) object is as follows:
    
    姓名:老幺 工作在 计算机科学. 部门.描述:计算机科学系主任
            姓名:老三 工作在 计算机科学 部门.描述:讲师
            姓名:老四 工作在 计算机科学 部门.描述:讲师
            姓名:老五 工作在 计算机科学 部门.描述:讲师
    
    Details of an individual employee(leaf node一般教职员) is as follows:
            姓名:老大 工作在 软件 部门.描述:讲师
    
    成员:
    
    姓名:族长 工作在 规划监督管理 部门.描述:校长
    
    姓名:老六 工作在 数学 部门.描述:数学系主任
            姓名:老大 工作在 软件 部门.描述:讲师
            姓名:老二 工作在 软件 部门.描述:讲师
    
    姓名:老幺 工作在 计算机科学. 部门.描述:计算机科学系主任
            姓名:老三 工作在 计算机科学 部门.描述:讲师
            姓名:老五 工作在 计算机科学 部门.描述:讲师
    我的画
    *红色 正方形
    *黄色 圆
    *组
    **蓝色 圆
    **蓝色 正方形
    

      

  • 相关阅读:
    三:理解Page类的运行机制(例:在render方法中生成静态文件)
    给力的2011,我来了
    推荐全球最大图标搜索FindIcons.com
    C#读图片EXIF信息
    WCF分布式开发步步为赢系列
    几个微软经典开源的项目源代码
    用jQuery合并表格中相同文本的相邻单元格
    收集脚本SQL
    事在人为,境由心造
    据说月薪过了6000的应届生,都知道这些网站
  • 原文地址:https://www.cnblogs.com/geovindu/p/16757929.html
Copyright © 2020-2023  润新知