设计模式—puremvc拓展之中介模式、代理模式、观察者模式和命令模式

设计模式

代理模式(静态代理/动态代理)Proxy

在这里插入图片描述

Subject是顶层接口,RealSubject是真实对象,Proxy是代理对象,代理对象持有被代理对象的引用,客户端调用代理对象的方法,同时也调用被代理对象的方法,但是在代理对象前后增加一些处理。

1.interface/class:

interface subject(){ void func();}   or   class abstract subject(){abstract void func();}

2.proxy and realSubject realize interface/class:
class realSubject : subject {void func(){}}
class Proxy : subject
{ 
  realSubject origin;
  Proxy()
  {
     this.origin = new realSubject();
  }
  Proxy(realSubject ori)
  {
     this.origin = ori;
  }
  void func()
  {
     //before the func:do sth 
     //func from realSubject
     //after the func:do sth
  }
}
3.proxy can use functions from proxy

PS:proxy含有realSubject的引用,主要是通过引用再通过实现接口/抽象类的方式实现调用realSubject的functions。
静态代理:手动创建代理,在编译器就存在。
动态代理:在运行时动态创建的代理。

观察者模式 Publish/Subscrible

为了解耦和后期维护,应该使用interface和event

传统思路

抽象通知者(被观察者)
abstract class subject
{  private Ilist<Observer> observers = new List<Observer>();
   //增加和删除观察者
   public void Attach(Observer observer)
   {observers.Add(observer);}
   public void Detach(Observer observer)
   {observers.Remove(observer);}
   //通知
   public void Notify()
   {foreach(Observer obs in observers){o.Update();}}  
}
抽象观察者
abstract class Observer
{   public abstract void Update(); } 
通知者派生类
class ChildSubject:Subject
{
    private string subjectState;
    //具体被观察者状态
    public string SubjectState
    {
      get{return subjectState;}
      set{subjectState = value;}
    }
}
观察者派生类
class ChildObserver:Observer
{
   private string name;
   private string observerState;
   private ChildSubject subject;
   public ChildObserver(ChildSubject subject,string name)
   {
      this.subject = subject;
      this.name = name;
   }
   public override void Update()
   {
     observerState = subject.SubjectState;
     Console.WriteLine("{0}{1}",name,observerState);
   }
   public ChildSubject Subject
   {
      get{return subject;}
      set{subject = value;}
   }
}

命令模式()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ODCdrdy2-1605836570469)(/Users/gaea/Library/Application Support/typora-user-images/image-20201119091006583.png)]

abstract class command only need to know who is receiver

abstract class command
{
  protected Object receiver;
  public command(Object receiver)
  {
    this.receiver = receiver;
  }
  abstract public void ExcuteCommand();
}

Detailcommand classes

class DetailCommand1 : command
{
  public DetailCommand1(Object receiver)
   : base(receiver)
   {}
  public override void ExcuteCommand()
  {
    receiver.func1();
  }
}
class DetailCommand2 : command
{
   public DetailCommand2(Object receiver)
     : base(receiver)
     {}
  public override void ExcuteCommand()
  {
    receiver.func2();
  }
}

Invoker class

class Invoke
{
   private Command command;//to realize commands conveniently, we may need List,Dic sth else to save commands
   public void SetCommand(Command c)
   {
     this.command = c;
     //commands.Add(c);
   }
   public void ExcuteCommand()
   {
     ommand.Excute();
     /*
     foreach(Command c in commands)
     {
         c.Excute();
     }
     */
   }
}

receiver class

class Receiver
{
      public void func1()
      {
        Console.WriteLine("this us func2!")
      }
      public void finc2()
      {
        Console.WriteLine("this is func1!")
      }
}

Mediator 中介者模式

​

如上图所示 将繁琐的关系图用中介(联合国)来将他们相连。

抽象Mediator会有一个抽象方法,其派生类需要去实现这个方法。

委托中介的对象是一个类或者该类的派生对象。

abstract class Mediator<T>
{
  List<T> target;//此处的意思就是中介者需要保存所有target的引用
  protected Mediator mediator;
  public Mediator<T>(mediator)
  {
    this.mediator = mediator;
  }
  public void init target(List<T> list)
  {
     this.target = list;
  }
  public abstract void HandleEvents(string msg,Object o);
}
class UnitedNationsSecurityCouncil:Mediator<T> //实现上面的抽象方法
{
   
}
class Target 
{
   Mediator mediator;
   public Target(Mediator mediator)
   {
     this.mediator = m;
   }
   public void HandleEvents(string msg)
   {
     mediator.HandleEvents(msg,this);
   }
   public void GetMsg(string msg)
   {
     //add code
   }
}

中介者的要点就是中介和目标要互存引用,目标可以调用中介的方法,中介可以调用目标的方法。但是为了达到松耦合的目的,我们应当使用抽象类。

相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页