类与类之间的隔离-不要与陌生人说话。

根据我的理解,要降低类之间的耦合,那么应该尽量让他们之间不要直接打交道。
或者正确的说,两个分工很明确的类,应该各自管理自己的东西,而不要去“打听”其他类的东西。
举个例子:
需求是这样的:
当一个类更新了数据后,需要发送邮件通知相关人员。
代码一:

None.gif public  DataAccess()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedBlockEnd.gif
        }

None.gif        
public   void  UpdateData(Data data)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//dot.gifdot.gif
            InBlock.gif//直接初始化myMail类,并调用发邮件方法。
InBlock.gif            
//完成数据更新后,利用mail通知

InBlock.gif
            new myMail().SendMail(mail);
ExpandedBlockEnd.gif        }

None.gif    }
None.gif
None.gif    
public   class  myMail
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public myMail()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void SendMail(MailArgs ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

代码二
专门定义一个事件类,来接受来之于数据处理类的触发事件,并在事件类里面来发送邮件。
那么作为数据更新类,只负责做好数据更新。
交由一个中间类来处理它的事件。

None.gif      public   class  DataAccess        
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
protected eventClass e;
InBlock.gif        
public DataAccess()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif
            e = new eventClass(this);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void UpdateData(Data data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//dot.gifdot.gif
InBlock.gif
InBlock.gif            
//利用事件通知
InBlock.gif
            if(updateEvent!=null)
InBlock.gif                updateEvent(
this);
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
定义事件#region 定义事件
InBlock.gif        
public delegate void UpdateEventHandler(object sender);
InBlock.gif        
public event UpdateEventHandler updateEvent;
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   class  eventClass
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public eventClass(DataAccess a)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            a.updateEvent 
+=new Fiberxon.HR.Biz.DataAccess.UpdateEventHandler(a_updateEvent);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void a_updateEvent(object sender)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
new myMail().SendMail(ex);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif
None.gif    
public   class  myMail
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public myMail()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void SendMail(MailArgs ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif}




 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值