控制器
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace RGeos.FrameWork.MVC 7 { 8 //控制类 9 public interface IController10 {11 string Name { get; }12 IExecutor Executor { get; }13 IObserver Observer { get; }14 }15 public abstract class AbstractController : IController16 {17 protected string mName;18 protected IExecutor mExecutor;19 protected IObserver mObserver;20 //从外部传入被观察者21 //控制器内部构造观察者,并向被观察者注册,观察者和控制器总是成对出现22 protected AbstractController(string name, IExecutor executor)23 {24 this.mName = name;25 this.mExecutor = executor;26 }27 28 public virtual IExecutor Executor29 {30 get31 {32 return this.mExecutor;33 }34 }35 36 public virtual string Name37 {38 get39 {40 return this.mName;41 }42 }43 44 public virtual IObserver Observer45 {46 get47 {48 return this.mObserver;49 }50 }51 //业务逻辑52 public virtual void HandExecuter()53 {54 }55 public virtual bool OnRemove(object o, params object[] assistant)56 {57 return this.mExecutor.OnRemove(o, assistant);58 }59 public virtual bool OnAdd(object o, params object[] assistant)60 {61 return this.mExecutor.OnAdd(o, assistant);62 }63 public virtual bool OnReplace(object o, params object[] assistant)64 {65 return this.mExecutor.OnReplace(o, assistant);66 }67 68 }69 }
被观察者
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace RGeos.FrameWork.MVC 7 { 8 //被观察者,执行者 9 public interface IExecutor 10 { 11 string Name { get; } 12 13 bool IsStateChanged { get; } 14 15 bool NeedRefresh { get; } 16 //观察者集合 17 ListObservers { get; } 18 19 bool OnAdd(object o, params object[] assistant); 20 bool OnRemove(object o, params object[] assistant); 21 bool OnReplace(object o, params object[] assistant); 22 23 void NotifyObservers(); 24 //添加观察者 25 bool RegisterObserver(IObserver view); 26 //注销观察者 27 bool UnRegisterObserver(IObserver view); 28 //注销所有观察者 29 bool UnRegisterObservers(); 30 } 31 public abstract class AbstractExecutor : IExecutor, ICloneable 32 { 33 protected bool mIsStateChanged; 34 protected string mName; 35 protected bool mNeedRefresh; 36 protected List m_observers; 37 public bool IsStateChanged 38 { 39 get 40 { 41 return this.mIsStateChanged; 42 } 43 } 44 45 public string Name 46 { 47 get 48 { 49 return this.mName; 50 } 51 } 52 public bool NeedRefresh 53 { 54 get 55 { 56 return this.mNeedRefresh; 57 } 58 } 59 60 public List Observers 61 { 62 get 63 { 64 return this.m_observers; 65 } 66 } 67 68 protected AbstractExecutor(string name) 69 { 70 this.mName = name; 71 this.mNeedRefresh = true; 72 this.mIsStateChanged = false; 73 this.m_observers = new List (); 74 } 75 protected AbstractExecutor() 76 { 77 this.mNeedRefresh = true; 78 this.mIsStateChanged = false; 79 this.m_observers = new List (); 80 } 81 82 83 public virtual object Clone() 84 { 85 return null; 86 } 87 88 public virtual bool RegisterObserver(IObserver observer) 89 { 90 if (this.m_observers == null) 91 { 92 this.m_observers = new List (); 93 } 94 if (!this.m_observers.Contains(observer)) 95 { 96 this.m_observers.Add(observer); 97 return true; 98 } 99 return false;100 }101 102 public virtual bool UnRegisterObserver(IObserver observer)103 {104 if (this.m_observers != null)105 {106 while (this.m_observers.Contains(observer))107 {108 return this.m_observers.Remove(observer);109 }110 }111 return false;112 }113 114 public virtual bool UnRegisterObservers()115 {116 if (this.m_observers != null)117 {118 this.m_observers.Clear();119 this.m_observers = null;120 return true;121 }122 return false;123 }124 125 public virtual bool OnAdd(object o, params object[] assistant)126 {127 this.mIsStateChanged = true;128 this.NotifyObservers();129 return true;130 }131 public virtual bool OnRemove(object o, params object[] assistant)132 {133 this.mIsStateChanged = true;134 this.NotifyObservers();135 return true;136 }137 public virtual bool OnReplace(object o, params object[] assistant)138 {139 this.mIsStateChanged = true;140 this.NotifyObservers();141 return true;142 }143 public virtual void NotifyObservers()144 {145 if (this.mNeedRefresh)146 {147 if (this.mIsStateChanged)148 {149 foreach (IObserver observer in this.m_observers)150 {151 observer.OnUpdate(this);152 }153 }154 }155 this.mIsStateChanged = false;156 }157 }158 159 }
观察者
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace RGeos.FrameWork.MVC 7 { 8 //观察者 9 public interface IObserver10 {11 IController Controller { get; }12 IExecutor Executor { get; }13 string Name { get; }14 15 void InitializeObserver();16 //更新观察者17 void OnUpdate(IExecutor currentExecutor);18 }19 public abstract class AbstractObserver : IObserver20 {21 protected IController mController;22 protected IExecutor mExecutor;23 protected string mName;24 25 public abstract void InitializeObserver();26 public abstract void OnUpdate(IExecutor currentExecutor);27 28 public virtual IController Controller29 {30 get31 {32 return this.mController;33 }34 }35 36 public virtual IExecutor Executor37 {38 get39 {40 return this.mExecutor;41 }42 }43 44 public virtual string Name45 {46 get47 {48 return this.mName;49 }50 }51 //观察者通过控制器引用,调用被观察者的方法,更新其他的观察者。52 //此时观察者和被观察者不通信53 protected AbstractObserver(string name, IController controller)54 {55 this.mName = name;56 this.mController = controller;57 this.mExecutor = controller.Executor;58 }59 //在控制器中构造观察者时,将被观察者传入观察者,这样观察者可以调用被观察者中的数据。60 //此时观察者和被观察者通信61 protected AbstractObserver(string name, IController controller, IExecutor executor)62 {63 this.mName = name;64 this.mController = controller;65 this.mExecutor = executor;66 }67 }68 69 }
这个是MVC还是MVP?说不清。