设计模式-》责任链模式(Design mode – responsibility chain mode)

例子 责任链 教会(同工,管理员,创建者)

例子 责任链 教会(同工,管理员,创建者)

  • 修改教会成员名称(同工)
  • 去教会祷告请假(管理员)
  • 教会关门,隐退(创建者)
    public abstract class AbsGuildManager
    {
        private string _name;
        protected AbsGuildManager SuperiorGuildManager { get; }


        protected AbsGuildManager(string name, AbsGuildManager superiorGuildManager)
        {
            _name = name;
            SuperiorGuildManager = superiorGuildManager;
        }

        public abstract void HandlerRequest(GuildRequest request);
    }



    public enum GuildRequestType
    {
        改名称,
        请假,
        关门
    }

    public class GuildRequest
    {
        public Guid UserId { get; }
        public GuildRequestType GuildRequestType { get; }

        public GuildRequest(GuildRequestType guildRequestType, Guid userId)
        {
            GuildRequestType = guildRequestType;
            UserId = userId;
        }
    }

    public class TongGongGuildManager : AbsGuildManager
    {
        public TongGongGuildManager(string name, AbsGuildManager superiorGuildManager) : base(name,
            superiorGuildManager)
        {
        }

        public override void HandlerRequest(GuildRequest request)
        {
            if (request.GuildRequestType == GuildRequestType.改名称)
            {
                Console.WriteLine($"同工同意:{request.UserId}修改名字成功");
            }
            else
            {
                SuperiorGuildManager?.HandlerRequest(request);
            }
        }
    }

    public class AdminGuildManager : AbsGuildManager
    {
        public AdminGuildManager(string name, AbsGuildManager superiorGuildManager) : base(name,
            superiorGuildManager)
        {
        }

        public override void HandlerRequest(GuildRequest request)
        {
            if (request.GuildRequestType == GuildRequestType.请假)
            {
                Console.WriteLine($"管理员同意:{request.UserId}请假成功");
            }
            else
            {
                SuperiorGuildManager?.HandlerRequest(request);
            }
        }
    }

    public class CreatorGuildManager : AbsGuildManager
    {
        public CreatorGuildManager(string name, AbsGuildManager superiorGuildManager) : base(name,
            superiorGuildManager)
        {
        }

        public override void HandlerRequest(GuildRequest request)
        {
            if (request.GuildRequestType == GuildRequestType.关门)
            {
                Console.WriteLine($"教会创始人同意:教会关门隐退");
            }
            else
            {
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var creator = new CreatorGuildManager("创建者", null);
            var admin = new AdminGuildManager("管理员", creator);
            var tongGong = new TongGongGuildManager("同工", admin);
            var request1 = new GuildRequest(GuildRequestType.关门,Guid.NewGuid());
            var request2 = new GuildRequest(GuildRequestType.改名称,Guid.NewGuid());
            var request3 = new GuildRequest(GuildRequestType.请假,Guid.NewGuid());
            
            tongGong.HandlerRequest(request1);
            tongGong.HandlerRequest(request2);
            tongGong.HandlerRequest(request3);
            
        }
    }

参考地址

————————

Example: responsibility chain Church (co worker, administrator, creator)

Example: responsibility chain Church (co worker, administrator, creator)

  • Modify the name of church members (co worker)
  • Go to church to pray and ask for leave (administrator)
  • Church closed, retired (founder)
    public abstract class AbsGuildManager
    {
        private string _name;
        protected AbsGuildManager SuperiorGuildManager { get; }


        protected AbsGuildManager(string name, AbsGuildManager superiorGuildManager)
        {
            _name = name;
            SuperiorGuildManager = superiorGuildManager;
        }

        public abstract void HandlerRequest(GuildRequest request);
    }



    public enum GuildRequestType
    {
        改名称,
        请假,
        关门
    }

    public class GuildRequest
    {
        public Guid UserId { get; }
        public GuildRequestType GuildRequestType { get; }

        public GuildRequest(GuildRequestType guildRequestType, Guid userId)
        {
            GuildRequestType = guildRequestType;
            UserId = userId;
        }
    }

    public class TongGongGuildManager : AbsGuildManager
    {
        public TongGongGuildManager(string name, AbsGuildManager superiorGuildManager) : base(name,
            superiorGuildManager)
        {
        }

        public override void HandlerRequest(GuildRequest request)
        {
            if (request.GuildRequestType == GuildRequestType.改名称)
            {
                Console.WriteLine($"同工同意:{request.UserId}修改名字成功");
            }
            else
            {
                SuperiorGuildManager?.HandlerRequest(request);
            }
        }
    }

    public class AdminGuildManager : AbsGuildManager
    {
        public AdminGuildManager(string name, AbsGuildManager superiorGuildManager) : base(name,
            superiorGuildManager)
        {
        }

        public override void HandlerRequest(GuildRequest request)
        {
            if (request.GuildRequestType == GuildRequestType.请假)
            {
                Console.WriteLine($"管理员同意:{request.UserId}请假成功");
            }
            else
            {
                SuperiorGuildManager?.HandlerRequest(request);
            }
        }
    }

    public class CreatorGuildManager : AbsGuildManager
    {
        public CreatorGuildManager(string name, AbsGuildManager superiorGuildManager) : base(name,
            superiorGuildManager)
        {
        }

        public override void HandlerRequest(GuildRequest request)
        {
            if (request.GuildRequestType == GuildRequestType.关门)
            {
                Console.WriteLine($"教会创始人同意:教会关门隐退");
            }
            else
            {
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var creator = new CreatorGuildManager("创建者", null);
            var admin = new AdminGuildManager("管理员", creator);
            var tongGong = new TongGongGuildManager("同工", admin);
            var request1 = new GuildRequest(GuildRequestType.关门,Guid.NewGuid());
            var request2 = new GuildRequest(GuildRequestType.改名称,Guid.NewGuid());
            var request3 = new GuildRequest(GuildRequestType.请假,Guid.NewGuid());
            
            tongGong.HandlerRequest(request1);
            tongGong.HandlerRequest(request2);
            tongGong.HandlerRequest(request3);
            
        }
    }

Reference address