首页 > 其他分享 >CSharp: Composite Pattern in donet core 3

CSharp: Composite Pattern in donet core 3

时间:2022-10-06 16:44:21浏览次数:42  
标签:core Name Composite Pattern 计算机科学 姓名 new public string

 

 /// <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();
    }
    /// <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}");
        }
    }
    /// <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>
        /// 子类对象 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()
        {
            Console.WriteLine($"\n姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}");
            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:
        姓名:老大 工作在 软件 部门.描述:讲师

成员:

姓名:族长 工作在 规划监督管理 部门.描述:校长

姓名:老六 工作在 数学 部门.描述:数学系主任
        姓名:老大 工作在 软件 部门.描述:讲师
        姓名:老二 工作在 软件 部门.描述:讲师

姓名:老幺 工作在 计算机科学. 部门.描述:计算机科学系主任
        姓名:老三 工作在 计算机科学 部门.描述:讲师
        姓名:老五 工作在 计算机科学 部门.描述:讲师
我的画
*红色 正方形
*黄色 圆
*组
**蓝色 圆
**蓝色 正方形

  

标签:core,Name,Composite,Pattern,计算机科学,姓名,new,public,string
From: https://www.cnblogs.com/geovindu/p/16757929.html

相关文章

  • CSharp: Flyweight Pattern in donet core 3
     ///<summary>///The'Flyweight'interface///享元模式FlyweightPattern///geovindu,GeovinDuedit///车辆///</summary>int......
  • 使用 ASP.NET Core 封装的 JWT
    一、用NuGet安装:Microsoft.AspNetCore.Authentication.JwtBearer二、在appsetting中添加配置节点"JWT":{"SigningKey":"","ExpireSeconds":""}pu......
  • Python: Factory Method Pattern
     #Python3.9#工厂方法模式FactoryMethodPatternfrom__future__importannotationsfromabcimportABC,abstractmethodimportpygame#pipinstallpyg......
  • CSharp: Facade Pattern in donet core 3
     ///<summary>///外观模式FacadePattern///geovindu,GeovinDueidt///机器人颜色///</summary>publicclassRobotColor{......
  • ASP .NET CORE WEB 部署到Docker
    一、新建asp.net core web 网站,二、新建个dockerfile文件 FROMmcr.microsoft.com/dotnet/sdk:6.0ASbuildWORKDIR/appWORKDIR/srcCOPY..RUNdotnetres......
  • CSharp: Adapter Pattern in donet core 3
     ///<summary>///适配器模式AdapterPatternA///geovindu,GeovinDuedit///</summary>interfaceIRectangle{voidAboutMe(......
  • SparkCore:累加器和广播变量
    累加器累加器(分布式共享只写变量):用来把Executor端变量信息聚合到Driver端。在Driver程序中定义的变量,在Executor端的每个Task都会得到这个变量的一份新的副本,每......
  • Servlet—— urlPattern配置
    ServleturlPattern配置  Servlet要想被访问,必须配置其访问路径(urlPattern)  1、一个Servlet可以配置多个urlPattern      2、urlPattern配置规则   ......
  • Asp-Net-Core开发笔记:集成Hangfire实现异步任务队列和定时任务
    前言最近把Python写的数据采集平台往.NetCore上迁移,原本的采集任务使用多进程+线程池的方式来加快采集速度,使用Celery作为异步任务队列兼具定时任务功能,这套东西用着还行......
  • CSharp: Abstract Factory in donet core 3
     ///<summary>///AbstractFactory抽像工厂///geovindu,GeovinDueidt///</summary>publicinterfaceIShape{voidDraw();......