其他分享
首页 > 其他分享> > 设计模式-》访问者模式

设计模式-》访问者模式

作者:互联网

例子 访问者模式

    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!");
        }

个人总结

参考地址

标签:return,string,Visit,模式,._,override,设计模式,public,访问者
来源: https://www.cnblogs.com/icxldd/p/15804950.html