Head First Design pattern Observer

     翻开Head First 设计模式看了下,第一章观察者模式,看地 不甚明了,学java 已经有一段时间了,这是我第三次翻看这本书,前些日子,买的编程珠玑,与程序员思维也都没能读下来,于是从网上了找了段代码看了下,感觉简洁明了,重点突出,比书中引导式学习强了许多。
     先看代码要比之后再看类图代码结构效果要好很多。下面举个案例
      实例描述:客户支付了订单款项,这时财务需要开具发票,出纳需要记账,配送员需要配货。
      代码实现:
/// <summary>
    /// 抽象观察者
    /// </summary>
    public interface ISubject
    {
        void Notify();
    }

    /// <summary>
    /// 工作岗位,作为这里的观察者的抽象
    /// </summary>
    public abstract class JobStation
    {
        public abstract void Update();
    }

    /// <summary>
    /// 具体主题,这里是客户
    /// </summary>
    public class Customer : ISubject
    {
        private string customerState;

        private IList<JobStation> observers = new List<JobStation>();

        /// <summary>
        /// 增加观察者
        /// </summary>
        /// <param name="observer"></param>
        public void Attach(JobStation observer)
        {
            this.observers.Add(observer);
        }

        /// <summary>
        /// 移除观察者
        /// </summary>
        /// <param name="observer"></param>
        public void Detach(JobStation observer)
        {
            this.observers.Remove(observer);
        }

        /// <summary>
        /// 客户状态
        /// </summary>
        public string CustomerState
        {
            get { return customerState; }
            set { customerState = value; }
        }

        public void Notify()
        {
            foreach (JobStation o in observers)
            {
                o.Update();
            }
        }
    }

    /// <summary>
    /// 会计
    /// </summary>
    public class Accountant : JobStation
    {
        private string accountantState;
        private Customer customer;

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

        /// <summary>
        /// 更新状态
        /// </summary>
        public override void Update()
        {
            if (customer.CustomerState == "已付款")
            {
                Console.WriteLine("我是会计,我来开具发票。");
                accountantState = "已开发票";
            }
        }
    }

    /// <summary>
    /// 出纳
    /// </summary>
    public class Cashier : JobStation
    {
        private string cashierState;
        private Customer customer;

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

        public override void Update()
        {
            if (customer.CustomerState == "已付款")
            {
                Console.WriteLine("我是出纳员,我给登记入账。");
                cashierState = "已入账";
            }
        }
    }

    /// <summary>
    /// 配送员
    /// </summary>
    public class Dilliveryman : JobStation
    {
        private string dillivierymanState;
        private Customer customer;

        public Dilliveryman(Customer customer)
        {
            this.customer = customer;
   `
      public override void Update()
        {
            if (customer.CustomerState == "已付款")
            {```
`

“` sole.WriteLine(“我是配送员,我来发货。”);
dillivierymanState = “已发货”;
}
}
}
客户端代码:
class Program
{
static void Main(string[] args)
{

        Customer subject = new Customer();

        subject.Attach(new Accountant(subject));
        subject.Attach(new Cashier(subject));
        subject.Attach(new  
  }// 看完代码不难发现,其实观察者模式无非就是在客户类中增加了一个 
  //盛JobStation抽象类集合的属性,利用多态可以接收不同的子类对象,在这个客户类中可以通过集合add,和remove 方法实现删除和添加观察者,在客户类中,遍历集合调用抽象类实现类的update Dilliv。
  ryman(subject));

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

        Console.Read();
    }
} 方法实现通知。

你可能感兴趣的:(设计模式)