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

【原创】欢迎转贴 观察者模式实践

Observer.cs Code
观察者模式实践    // 可被观察类的基类
观察者模式实践
    public abstract class Observable
{        
观察者模式实践        
protected ArrayList observerList = new ArrayList();
观察者模式实践
观察者模式实践        
//添加观察者
观察者模式实践
        public virtual void AddObserver(IObserver observer)
{
观察者模式实践            observerList.Add(observer);
观察者模式实践        }

观察者模式实践
观察者模式实践        
//删除观察者
观察者模式实践
        public virtual void RemoveObserver(IObserver observer)
{
观察者模式实践            observerList.Remove(observer);
观察者模式实践        }

观察者模式实践
观察者模式实践        
//清除观察者
观察者模式实践
        public virtual void ClearObservers()
{
观察者模式实践            observerList.Clear();
观察者模式实践        }

观察者模式实践

观察者模式实践        
/// 通知观察者
观察者模式实践         
/// </summary>
观察者模式实践        
/// <param name="argName"></param>
观察者模式实践        
/// <param name="argValue"></param>
观察者模式实践        public virtual void NotifyObservers(string argName,object argValue)
{
观察者模式实践            
foreach(object o in observerList)
{
观察者模式实践                
//通知观察者进行更新
观察者模式实践
                ((IObserver)o).Update(argName,argValue);
观察者模式实践            }

观察者模式实践        }

观察者模式实践    }

观察者模式实践
观察者模式实践    
//观察者的接口
观察者模式实践
    public interface IObserver
{
观察者模式实践        
void Update(string argName,object argValue);
观察者模式实践     }

观察者模式实践
观察者模式实践
观察者模式实践    
//可被观察类
观察者模式实践
    public class Data:Observable
{
观察者模式实践        
public Data()
{
观察者模式实践        }

观察者模式实践
观察者模式实践        
public string Name
{
观察者模式实践            
set
{
观察者模式实践                
this.NotifyObservers("name",value);
观察者模式实践            }

观察者模式实践        }

观察者模式实践
观察者模式实践        
public int Age
{
观察者模式实践            
set
{
观察者模式实践                
this.NotifyObservers("age",value);
观察者模式实践            }

观察者模式实践        }

观察者模式实践    }

观察者模式实践
观察者模式实践    
//一种观察者,只关心名字
观察者模式实践
    public class NameObserver:IObserver
{
观察者模式实践        
public string describe  ;
观察者模式实践
观察者模式实践        
public NameObserver()
{
观察者模式实践        }

观察者模式实践
观察者模式实践

观察者模式实践        
public void Update(string argName,object argValue)
{
观察者模式实践            
if(argName == "name" && argValue is string)
{
观察者模式实践                describe 
= "His name is changed to  " + (string)argValue ;
观察者模式实践            }

观察者模式实践          }

观察者模式实践        
#endregion
观察者模式实践    }

观察者模式实践
观察者模式实践    
//另一种观察者,只关心年龄
观察者模式实践
    public class AgeObserver:IObserver
{
观察者模式实践        
public string describe  ;
观察者模式实践
观察者模式实践        
public AgeObserver()
{
观察者模式实践        }

观察者模式实践

观察者模式实践        
public void Update(string argName,object argValue)
{
观察者模式实践             
if(argName == "age" && argValue is int)
{
观察者模式实践                
int age = (int)argValue;
观察者模式实践                
if( age > 100)
{
观察者模式实践                    describe 
= "Is he in the world ?";
观察者模式实践                }

观察者模式实践                
else if(age <= 0)
{
观察者模式实践                    describe 
= "I think he is too young" ;
观察者模式实践                }

观察者模式实践                
else
{
观察者模式实践                    describe 
= "His age is changed to " + (int)argValue ;
观察者模式实践
观察者模式实践                }

观察者模式实践            }

观察者模式实践        }

观察者模式实践        
#endregion
观察者模式实践    }

观察者模式实践
观察者模式实践

Form2.cs 代码

观察者模式实践        private Data data = new Data();
观察者模式实践        
private NameObserver ob = new NameObserver();
观察者模式实践        
private AgeObserver ob2 = new AgeObserver();
观察者模式实践
观察者模式实践        
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
观察者模式实践            
try
{
观察者模式实践                data.Name 
= textBox1.Text;
观察者模式实践 
观察者模式实践                ShowInfo();
观察者模式实践            }

观察者模式实践            
catch
{
观察者模式实践                
观察者模式实践            }

观察者模式实践        }

观察者模式实践
观察者模式实践        
private void textBox4_TextChanged(object sender, System.EventArgs e)
{
观察者模式实践            
try
{
观察者模式实践                 data.Age 
= Int32.Parse(textBox4.Text);
观察者模式实践
观察者模式实践                ShowInfo();
观察者模式实践            }

观察者模式实践            
catch
{
观察者模式实践                
观察者模式实践            }

观察者模式实践        }

观察者模式实践
观察者模式实践        
private void ShowInfo()
{
观察者模式实践            textBox2.Text 
=  ob.describe ;
观察者模式实践            textBox3.Text 
=  ob2.describe ;
观察者模式实践          }

观察者模式实践
观察者模式实践        
private void Form2_Load(object sender, System.EventArgs e)
{
观察者模式实践            data.AddObserver(ob);
观察者模式实践            data.AddObserver(ob2);
观察者模式实践        }

观察者模式实践

相关文章: