• 设计模式》访问者模式


    例子 访问者模式

    • Element -> AbsPlatform -》 User,Guild,GuildMember
    • Visitor -> AbsVisitor -》 教会管理员,教会创建者,平台管理员
    • ObjectStructure -> BusinessReport
        public abstract class AbsPlatform
        {
            /// <summary>
            /// visitor 获取数据
            /// </summary>
            /// <param name="absVisitor"></param>
            /// <returns></returns>
            public abstract string Accept(AbsVisitor absVisitor);
        }
    
        public class UserPlatform : AbsPlatform
        {
            public User _user;
    
            public UserPlatform(User user)
            {
                _user = user;
            }
    
            public override string Accept(AbsVisitor absVisitor)
            {
                return absVisitor.Visit(this);
            }
        }
    
        public class GuildPlatform : AbsPlatform
        {
            public Domain.Guild _guild;
    
            public GuildPlatform(Domain.Guild guild)
            {
                _guild = guild;
            }
    
            public override string Accept(AbsVisitor absVisitor)
            {
                return absVisitor.Visit(this);
            }
        }
    
        public class GuildMemberPlatform : AbsPlatform
        {
            public GuildMember _guildMember;
    
            public GuildMemberPlatform(GuildMember guildMember)
            {
                _guildMember = guildMember;
            }
    
            public override string Accept(AbsVisitor absVisitor)
            {
                return absVisitor.Visit(this);
            }
        }
    
        public abstract class AbsVisitor
        {
            /// <summary>
            /// 获取数据
            /// </summary>
            /// <param name="engineer"></param>
            /// <returns></returns>
            public abstract string Visit(UserPlatform engineer);
    
            public abstract string Visit(GuildPlatform manager);
    
            public abstract string Visit(GuildMemberPlatform guildMemberPlatform);
        }
    
        public class PlatformAdminVisitor : AbsVisitor
        {
            public override string Visit(UserPlatform engineer)
            {
                string registerSource = engineer._user.RegisterType == RegisterType.App ? "APP" : "H5";
                return (
                    $"目前是平台管理员,{engineer._user.Name}加入{engineer._user.JoinGuildCount}个教会,注册来源为{registerSource}");
            }
    
            public override string Visit(GuildPlatform manager)
            {
                return ($"目前是平台管理员,教会名:{manager._guild.Name},教会人数:{manager._guild.GuildMemberCount}");
            }
    
            public override string Visit(GuildMemberPlatform guildMemberPlatform)
            {
                return (
                    $"目前是平台管理员,教会成员名:{guildMemberPlatform._guildMember.Name},教会成员加入时间{guildMemberPlatform._guildMember.CreateTime.ToString("yyyy-M-d dddd")}");
            }
        }
    
        public class GuildAdminVisitor : AbsVisitor
        {
            public override string Visit(UserPlatform engineer)
            {
                return $"目前是教会管理员,没有权限查看用户信息";
            }
    
            public override string Visit(GuildPlatform manager)
            {
                return ($"目前是教会管理员,教会名:{manager._guild.Name}");
            }
    
            public override string Visit(GuildMemberPlatform guildMemberPlatform)
            {
                return ($"目前是教会管理员,教会成员名:{guildMemberPlatform._guildMember.Name}");
            }
        }
    
        public class GuildOwnerVisitor : AbsVisitor
        {
            public override string Visit(UserPlatform engineer)
            {
                return ($"目前是教会创始人,没有权限查看用户信息");
            }
    
            public override string Visit(GuildPlatform manager)
            {
                return($"目前是教会创始人,教会名:{manager._guild.Name},教会人数:{manager._guild.GuildMemberCount}");
            }
    
            public override string Visit(GuildMemberPlatform guildMemberPlatform)
            {
                return(
                    $"目前是教会创始人,教会成员名:{guildMemberPlatform._guildMember.Name},教会成员加入时间{guildMemberPlatform._guildMember.CreateTime.ToString("yyyy-M-d dddd")}");
            }
        }
    
        public class BusinessReport
        {
            private List<AbsPlatform> _absPlatforms;
    
            public BusinessReport(List<AbsPlatform> absPlatforms)
            {
                _absPlatforms = absPlatforms;
            }
    
            public void ShowReport(AbsVisitor visitor)
            {
                foreach (var platform in _absPlatforms)
                {
                    Console.WriteLine(platform.Accept(visitor));
                }
            }
        }
    
    
            static void Main(string[] args)
            {
                var Users = ObjectBuilder.BuilderUsers(50).Select(x => { return new UserPlatform(x); }).Cast<AbsPlatform>()
                    .ToList();
                var Guilds = ObjectBuilder.BuilderGuilds(50).Select(x => { return new GuildPlatform(x); })
                    .Cast<AbsPlatform>().ToList();
                var GuildMembers = ObjectBuilder.BuilderGuildMembers(50).Select(x => { return new GuildMemberPlatform(x); })
                    .Cast<AbsPlatform>()
                    .ToList();
                var report = new BusinessReport(Users.Concat(Guilds).Concat(GuildMembers).ToList());
    
                // report.ShowReport(new PlatformAdminVisitor());
                // report.ShowReport(new GuildAdminVisitor());
                report.ShowReport(new GuildOwnerVisitor());
    
                Console.WriteLine("Hello World!");
            }
    
    

    个人总结

    • Element数据源
    • Visitor不同的查询方式
    • ObjectStructure 程序运行的上下文

    参考地址

  • 相关阅读:
    基于visual Studio2013解决面试题之1101差值最小
    基于visual Studio2013解决面试题之1009兄弟字符串
    基于visual Studio2013解决面试题之1007鸡蛋和篮子
    基于visual Studio2013解决面试题之1006判断升序
    4.16 删除重复记录
    4.9 当相应行存在时更新
    4.8 在表中编辑记录
    4.7 阻止对某几列插入
    4.6 一次向多个表中插入记录
    4.4 从一个表向另外的表中复制行
  • 原文地址:https://www.cnblogs.com/icxldd/p/15804950.html
Copyright © 2020-2023  润新知