观察者模式

原作者: http://www.cnblogs.com/wangjq/archive/2012/07/12/2587966.html
修改版作者:https://blog.csdn.net/lynon/article/details/28865055
观察者模式普通版:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 观察模式
{
    public interface ISubject
    {
        void Notify();
    }

    public abstract class JobStation
    {
        public abstract void Update();
    }

    public class Customer : ISubject
    {
        private string customerState;
        private IList<JobStation> observers = new List<JobStation>();

        public void Attach(JobStation observer)
        {
            this.observers.Add(observer);
        }

        public void Detach(JobStation observer)
        {
            this.observers.Remove(observer);
        }

        public string CustomerState
        {
            get { return customerState; }
            set { customerState = value; }
        }

        public void Notify()
        {
            foreach (JobStation o in observers)
            {
                o.Update();                                        //无类别调用子类继承父类的方法;
            }
        }
    }

    public class Accountant : JobStation
    {
        private string accountantState;
        private Customer customer;

        public Accountant(Customer customer)
        {
            this.customer = customer;
        }

        public override void Update()
        {
            if(customer.CustomerState=="paid")
            {
                Console.WriteLine("Accountant,invoice");
                accountantState = "invoice";
            }
        }
    }

    public class Cashier : JobStation
    {
        private string cashierState;
        private Customer customer;
        public Cashier (Customer customer)
        {
            this.customer = customer;
        }

        public override void Update()
        {
            if (customer.CustomerState == "paid")
            {
                Console.WriteLine("cashier,check in");
                cashierState = "check in";
            }
        }
    }

    public class Dilliveryman : JobStation
    {
        public string dilliverymanState;
        private Customer customer;
        public Dilliveryman(Customer customer)
        {
            this.customer = customer;
        }

        public override void Update()
        {
            if(customer.CustomerState=="paid")
            {
                Console.WriteLine("dilliveryman,deliver");
                dilliverymanState = "deliver";
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer subject = new Customer();
            subject.Attach(new Accountant(subject));
            subject.Attach(new Cashier(subject));
            subject.Attach(new Dilliveryman(subject));

            subject.CustomerState = "paid";

            subject.Notify();                                         //简单版
        }
    }
}

进阶版:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 观察者模式2
{
    public interface ISubject
    {
        void Notify();
    }

    public delegate void CustomerEventHandler();

    public class Customer : ISubject
    {
        private string customerState;

        public event CustomerEventHandler Update;

        public void Notify()
        {
           /* if(Update!=null)
            {
                Update();                    //使用事件来通知给订阅者
            }*/
            Update?.Invoke();                //简化事件通知
        }

        public string CustomerState
        {
            get { return customerState; }
            set { customerState = value; }
        }
    }

    public class Accountant
    {
        private string accountantState;
        public Accountant()
        {

        }

        public void GiveInvoice()
        {
            Console.WriteLine("会计,开票");
            accountantState = "已开票";
        }
    }

    public class Cashier
    {
        private string cashierState;
        public void Recoded()
        {
            Console.WriteLine("出纳,登记");
            cashierState = "已登记";
        }
    }

    public class Dilliveryman
    {
        private string dilliverymanState;
        public void Dilliver()
        {
            Console.WriteLine("配送员,发货");
            dilliverymanState = "已发货";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer subject = new Customer();
            Accountant accountant = new Accountant();
            Cashier cashier = new Cashier();
            Dilliveryman dilliveryman = new Dilliveryman();

            subject.Update += accountant.GiveInvoice;                                //注册事件
            //subject.Update += new CustomerEventHandler(accountant.GiveInvoice);    //等效
            subject.Update += cashier.Recoded;
            //subject.Update += new CustomerEventHandler(cashier.Recoded);
            subject.Update += dilliveryman.Dilliver;
            //subject.Update += new CustomerEventHandler(dilliveryman.Dilliver);

            subject.CustomerState = "已付款";
            subject.Notify();

            Console.ReadKey();
        }
    }
}


策略模式

 abstract class StragetyHandle
    {
        private string course;

        public string Course { get => course; set => course = value; }

        public abstract string Say();

    }
    class Chinese : StragetyHandle
    {
        public Chinese()
        {
            Course = "chinese";
        }

        public override string Say()
        {
            return Course;
        }
    }
    class MathC : StragetyHandle
    {
        public MathC()
        {
            Course = "math";
        }

        public override string Say()
        {
            return Course;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Dictionary<string, StragetyHandle> my = new Dictionary<string, StragetyHandle>();
            
            my["chinese"] = new Chinese();
            my["math"] = new MathC();

            string c1 = "chinese";
            Console.WriteLine("---c1--->" + my[c1].Say());

            string c2 = "math";
            Console.WriteLine("---c2--->" + my[c2].Say());
        }
    }

也可以不对say 抽象。

    abstract class StragetyHandle
    {
        private string course;

        public string Course { get => course; set => course = value; }

        public string Say()
        {
            return Course;
        }

    }
    class Chinese : StragetyHandle
    {
        public Chinese()
        {
            Course = "chinese";
        }
    }
    class MathC : StragetyHandle
    {
        public MathC()
        {
            Course = "math";
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值