DEM 机制实现Observer模式

该博客转载自https://www.cnblogs.com/Masterpiece/archive/2004/12/21/80093.html ,涉及Java和C#相关内容,但未给出具体信息。
    在上一篇 Observer 模式在eHR中的应用中提到了Observer模式在eHR中的应用,其中处理方式是用的传统的设计原则,但是无论在java中还是c#中,都有了DEM模型,所以采用DEM模型来实现Observer模式会更快,切在扩展性上也有一定的提高(比如不需要必须在Observer中实现Update函数,而只要符合代理约定即可)。
    下面我就用DEM模型来重新修改程序 
   
None.gif using  System;
None.gif
None.gif
namespace  Observer.DEMObserver
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DEMObserverTest 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    
InBlock.gif    
public class DEMObserverTest
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public DEMObserverTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 虚拟主题
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public class Subject
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
public delegate void UpdateDelegate();
InBlock.gif            
public event UpdateDelegate UpdateHandler;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 添加观察者
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="observerDelegate"></param>

InBlock.gif            public void AttachObserver(UpdateDelegate observerDelegate)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                UpdateHandler
+=observerDelegate;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 分离观察者
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <param name="observerDelegate"></param>

InBlock.gif            public void DetachObserver(UpdateDelegate observerDelegate)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                UpdateHandler
-=observerDelegate;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 通知方法
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public void NotifyObservers()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(UpdateHandler!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    UpdateHandler();
ExpandedSubBlockEnd.gif                }

InBlock.gif                Console.ReadLine();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 组织架构修改主题
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public class OrgChgSubject:Subject
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 组织架构变化观察者
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public class OrgChgObserver
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public void UpdateDelegateImplement()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"*******组织架构改动主题*******");
InBlock.gif                Console.WriteLine(
"1.组织架构已经发生改变!");
InBlock.gif                Console.WriteLine(
"*****************************");
InBlock.gif                Console.WriteLine(
"*******绩效系统的观察者*******");
InBlock.gif                Console.WriteLine(
"1.已经接受到组织改动主题的通知");
InBlock.gif                Console.WriteLine(
"2.更新绩效管理系统的签核人");
InBlock.gif                Console.WriteLine(
"3.更新完毕");
InBlock.gif                Console.WriteLine(
"*****************************");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
    在另外的一个主类中的DEM调用代码如下:
None.gif using  System;
None.gif
None.gif
namespace  Observer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 主类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MainClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
非DEM实现#region 非DEM实现
InBlock.gif
//            OrgChgSubject o_OrgChgSubject=new OrgChgSubject();
InBlock.gif
//            OrgChgObserver o_OrgChgObserver=new OrgChgObserver();
InBlock.gif
//            o_OrgChgSubject.AttachObserver(o_OrgChgObserver);
InBlock.gif
//            o_OrgChgSubject.NotifyObservers();
InBlock.gif
//            Console.ReadLine();
ExpandedSubBlockEnd.gif
            #endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif            
DEM实现#region DEM实现
InBlock.gif            DEMObserver.DEMObserverTest.OrgChgSubject o_OrgChgSubject
=new Observer.DEMObserver.DEMObserverTest.OrgChgSubject();
InBlock.gif            DEMObserver.DEMObserverTest.OrgChgObserver o_OrgChgObserver
=new Observer.DEMObserver.DEMObserverTest.OrgChgObserver();
InBlock.gif            o_OrgChgSubject.AttachObserver(
new DEMObserver.DEMObserverTest.Subject.UpdateDelegate(o_OrgChgObserver.UpdateDelegateImplement));
InBlock.gif            o_OrgChgSubject.NotifyObservers();
ExpandedSubBlockEnd.gif            
#endregion

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Masterpiece/archive/2004/12/21/80093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值