设计模式示例四 Observer(观察者)和Iterator(迭代器)

之所以把这两个模式放在一起说是因为C#语句的关系。这两种模式实现起来变得很轻松或者说不一样了。

Observer(观察者)
None.gif      class  Observer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public delegate void EventHandler(string state);
InBlock.gif        
public event EventHandler changeEvent;
InBlock.gif
InBlock.gif        
public void ChangeState(string state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (changeEvent != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                changeEvent(state);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
调用
None.gif          static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Observer obs 
= new Observer();
InBlock.gif            obs.changeEvent 
+= new Observer.EventHandler(obs_changeEvent);
InBlock.gif            obs.ChangeState(
"Change");
InBlock.gif            Console.Read();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
static   void  obs_changeEvent( string  state)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Console.WriteLine(
"I see:" + state);
ExpandedBlockEnd.gif        }

运行结果


Iterator(迭代器)
分别实现IEnumerable和IEnumerator接口的类
None.gif public   class  Tokens : IEnumerable
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public string[] elements;
InBlock.gif
InBlock.gif    
public Tokens(string source, char[] delimiters)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        elements 
= source.Split(delimiters);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public IEnumerator GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return new TokenEnumerator(this);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif public   class  TokenEnumerator : IEnumerator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private int position = -1;
InBlock.gif    
private Tokens t;
InBlock.gif
InBlock.gif    
public TokenEnumerator(Tokens t)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.t = t;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public bool MoveNext()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (position < t.elements.Length - 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            position
++;
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void Reset()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        position 
= -1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public object Current
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return t.elements[position];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
调用
None.gif          static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            Tokens tokens 
= new Tokens("a,b,c"new char[] dot.gif',' });
InBlock.gif            IEnumerator iterator 
= tokens.GetEnumerator();
InBlock.gif            
while (iterator.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string token = iterator.Current as string;
InBlock.gif                
if (token != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(token);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.Read();
ExpandedBlockEnd.gif        }

C#泛型的支持使得iterator的实现更好些。
结果

转载于:https://www.cnblogs.com/DavidFan/archive/2007/07/22/827031.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值