设计模式学习笔记十八——Mediator模式

动机:在系统构建过程中,经常出现多个对象相互关联交互的情况,对象之间常常会维持一种复杂的引用关系,面对变化能力弱。用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立地改变它们之间的变化。
 
场景:考虑一个文本编辑器,包含剪切菜单、粘帖按钮、文本编辑区域、剪贴板等对象,它们需要相互引用和交互。

结构



代码

None.gif namespace  DesignPattern.Mediator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract class Mediator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private IList<Control> controls;
InBlock.gif
InBlock.gif        
public Mediator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            controls 
= new IList<Control> ();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 管理交互
InBlock.gif
        public abstract void ControlChanged(Control control);
InBlock.gif
InBlock.gif        
public void Add(Control control)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            controls.Add(control);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class CutMediator : Mediator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public CutMediator()
InBlock.gif            : 
base()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void ControlChanged(Control control)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (control is CutMenuItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (control is PasteButton)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (control is TextArea)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (control is ClipBoard)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif namespace  DesignPattern.Mediator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract class Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Mediator mediator;
InBlock.gif
InBlock.gif        
public Control(Mediator mediator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.mediator = mediator;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public virtual void Changed()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mediator.ControlChanged(
this);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class CutMenuItem : Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public CutMenuItem(Mediator mediator)
InBlock.gif            : 
base(mediator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Changed()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//dot.gif
InBlock.gif
            base.Changed();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class PasteButton : Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public PasteButton(Mediator mediator)
InBlock.gif            : 
base(mediator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Changed()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//dot.gif
InBlock.gif
            base.Changed();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class TextArea : Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public TextArea(Mediator mediator)
InBlock.gif            : 
base(mediator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Changed()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//dot.gif
InBlock.gif
            base.Changed();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class ClipBoard : Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ClipBoard(Mediator mediator)
InBlock.gif            : 
base(mediator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Changed()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//dot.gif
InBlock.gif
            base.Changed();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


 要点: 
      1、本模式将多个对象间复杂的关联关系解耦,对多个对象间的控制逻辑进行集中管理,变“多个对象互相关联”为“多个对象和一个中介者关联”,简化了系统维护,能更好地面对变化。
      2、随着控制逻辑的复杂化,Mediator具体对象的实现可能相当复杂,这时候可对其进行分解处理。
      3、Facade模式是解耦系统外到系统内(单向)的对象关联关系;本模式是解耦系统内各个对象之间(双向)的关联关系。

转载于:https://www.cnblogs.com/Charly/archive/2007/07/26/832113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值