• 设计模式》责任链模式


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

    • 修改教会成员名称(同工)
    • 去教会祷告请假(管理员)
    • 教会关门,隐退(创建者)
        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);
                
            }
        }
    
    

    参考地址

  • 相关阅读:
    AES加密解密
    水电缴费管理系统 需求分析与设计
    ORACLE SQL语句练习
    GUID全局唯一标识符(转)
    java 基础--多线程基础练习
    java基础List集合练习
    I/O输入输出流的练习
    java基础--集合练习
    java基础异常捕获处理
    java基础 接口练习
  • 原文地址:https://www.cnblogs.com/icxldd/p/15804937.html
Copyright © 2020-2023  润新知