• C# 访问者模式(Visitor)


    理解:元素是固定的,作用于元素的状态是可以变化的。就好比一日三餐(元素)是固定的,但是每一餐吃的食物(状态)又是可以不断变化的。

    代码:

    using System.Windows.Forms; 
    namespace DesignMode.Visitor
    {

        //不变的元素
        public class Breakfast
        {
            public Breakfast()
            {
                _name = "早餐";
            }

            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public void Have(Food food)
            {
                food.HaveBreakfast(this);
            }
        }

        public class Lunch
        {
            public Lunch()
            {
                _name = "午餐";
            }

            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public void Have(Food food)
            {
                food.HaveLunch(this);
            }
        }

        public class Dinner
        {
            public Dinner()
            {
                _name = "晚餐";
            }

            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public void Have(Food food)
            {
                food.HaveDinner(this);
            }
        }


        //抽象变化点
        public abstract class Food
        {
            //把每一个不变的元素都要考虑进去
            public abstract void HaveBreakfast(Breakfast breakfast);

            public abstract void HaveLunch(Lunch lunch);

            public abstract void HaveDinner(Dinner dinner);
        }


        public class Noodle : Food
        {
            public override void HaveBreakfast(Breakfast breakfast)
            {
                MessageBox.Show(breakfast.Name + "吃面条");
            }

            public override void HaveLunch(Lunch lunch)
            {
                MessageBox.Show(lunch.Name + "吃面条");
            }

            public override void HaveDinner(Dinner dinner)
            {
                MessageBox.Show(dinner.Name + "吃面条");
            }
        }

        public class Rice : Food
        {
            public override void HaveBreakfast(Breakfast breakfast)
            {
                MessageBox.Show(breakfast.Name + "吃大米");
            }

            public override void HaveLunch(Lunch lunch)
            {
                MessageBox.Show(lunch.Name + "吃大米");
            }

            public override void HaveDinner(Dinner dinner)
            {
                MessageBox.Show(dinner.Name + "吃大米");
            }
        }

        public class KFC : Food
        {
            public override void HaveBreakfast(Breakfast breakfast)
            {
                MessageBox.Show(breakfast.Name + "吃KFC");
            }

            public override void HaveLunch(Lunch lunch)
            {
                MessageBox.Show(lunch.Name + "吃KFC");
            }

            public override void HaveDinner(Dinner dinner)
            {
                MessageBox.Show(dinner.Name + "吃KFC");
            }
        }
    }

    客户端代码:

             

    private void btn_Visitor_Click(object sender, EventArgs e) 

             {
                Noodle noodle = new Noodle();
                Rice rice = new Rice();
                KFC kfc = new KFC();

                Breakfast breakfast = new Breakfast();
                breakfast.Have(noodle);

                Lunch lunch = new Lunch();
                lunch.Have(kfc);
                lunch.Have(rice);

                Dinner dinner = new Dinner();
                dinner.Have(rice);
                dinner.Have(noodle);
            }

  • 相关阅读:
    微信小程序开发学习--页面事件和数据绑定
    检测小程序版本更新提示
    vue-cli脚手架
    链表中倒数第K个结点
    调整数组顺序使奇数位于偶数前面
    数值的整数次方(十二)
    二进制中1的个数(十一)
    覆盖矩形(十)
    变态跳台阶(九)
    跳台阶(八)
  • 原文地址:https://www.cnblogs.com/kavilee/p/2375441.html
Copyright © 2020-2023  润新知