zoukankan      html  css  js  c++  java
  • 设计模式》责任链模式

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

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

    参考地址

  • 相关阅读:
    android问题及其解决-优化listView卡顿和怎样禁用ListView的fling
    平安科技移动开发二队技术周报(第三期)
    机房重构(个人版)——类图
    php-wamp环境搭建
    ajax 通过return 返回data值
    cocos2d-x中六种持续性动作
    Android SimpleAdapter
    jquery 判断当前上传文件大小限制上传格式 搭配thinkphp实现上传即预览(模拟异步上传)
    【转】我的第一个Python小程序
    python官网
  • 原文地址:https://www.cnblogs.com/icxldd/p/15804937.html
Copyright © 2011-2022 走看看