原作者: 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";
}
}