观察者模式实践

观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。

【原创】欢迎转贴 07.gif

Observer.cs Code
None.gif      //  可被观察类的基类
None.gif
     public   abstract   class  Observable
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {        
InBlock.gif        
protected ArrayList observerList = new ArrayList();
InBlock.gif
InBlock.gif        
//添加观察者
InBlock.gif
        public virtual void AddObserver(IObserver observer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            observerList.Add(observer);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//删除观察者
InBlock.gif
        public virtual void RemoveObserver(IObserver observer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            observerList.Remove(observer);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//清除观察者
InBlock.gif
        public virtual void ClearObservers()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            observerList.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 通知观察者
InBlock.gif         
/// </summary>
InBlock.gif        
/// <param name="argName"></param>
ExpandedSubBlockEnd.gif        
/// <param name="argValue"></param>

InBlock.gif        public virtual void NotifyObservers(string argName,object argValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach(object o in observerList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//通知观察者进行更新
InBlock.gif
                ((IObserver)o).Update(argName,argValue);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
// 观察者的接口
None.gif
     public   interface  IObserver
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
void Update(string argName,object argValue);
ExpandedBlockEnd.gif     }

None.gif
None.gif
None.gif    
// 可被观察类
None.gif
     public   class  Data:Observable
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public Data()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.NotifyObservers("name",value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.NotifyObservers("age",value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
// 一种观察者,只关心名字
None.gif
     public   class  NameObserver:IObserver
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public string describe  ;
InBlock.gif
InBlock.gif        
public NameObserver()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IObserver 成员#region IObserver 成员
InBlock.gif        
public void Update(string argName,object argValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(argName == "name" && argValue is string)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                describe 
= "His name is changed to  " + (string)argValue ;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif
None.gif    
// 另一种观察者,只关心年龄
None.gif
     public   class  AgeObserver:IObserver
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public string describe  ;
InBlock.gif
InBlock.gif        
public AgeObserver()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IObserver 成员#region IObserver 成员
InBlock.gif        
public void Update(string argName,object argValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             
if(argName == "age" && argValue is int)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int age = (int)argValue;
InBlock.gif                
if( age > 100)
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                    describe 
= "Is he in the world ?";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if(age <= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    describe 
= "I think he is too young" ;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    describe 
= "His age is changed to " + (int)argValue ;
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif
None.gif

Form2.cs 代码

None.gif          private  Data data  =   new  Data();
None.gif        
private  NameObserver ob  =   new  NameObserver();
None.gif        
private  AgeObserver ob2  =   new  AgeObserver();
None.gif
None.gif        
private   void  textBox1_TextChanged( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                data.Name 
= textBox1.Text;
InBlock.gif 
InBlock.gif                ShowInfo();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  textBox4_TextChanged( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 data.Age 
= Int32.Parse(textBox4.Text);
InBlock.gif
InBlock.gif                ShowInfo();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  ShowInfo()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            textBox2.Text 
=  ob.describe ;
InBlock.gif            textBox3.Text 
=  ob2.describe ;
ExpandedBlockEnd.gif          }

None.gif
None.gif        
private   void  Form2_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            data.AddObserver(ob);
InBlock.gif            data.AddObserver(ob2);
ExpandedBlockEnd.gif        }

None.gif

转载于:https://www.cnblogs.com/QuitGame/archive/2005/04/11/135657.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值