Visitor 访问者(行为型模式)--学习笔记

         问题的提出:在软件构建过程中,由于需求的改变,某些类层次结构中常常需要增加新的行为(方法),如果直接在基类中做这样的更改,将会给子类带来很繁重的变更负担,甚至破坏原有设计。例如下面的代码:
None.gif public   abstract   class  Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract void Draw();
InBlock.gif    
//问题:由于Shape中增加了MoveTo方法,其各个子类将不得不随之更改
InBlock.gif
    public abstract void MoveTo(Point p); //新增加的方法,子类需要override该方法,
ExpandedBlockEnd.gif
}

None.gif
None.gif
public   class  Rectangle:Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public override void Draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
public   class  Circle:Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public override void Draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
public   class  Line:Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public override void Draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
解决思路:在不更改类层次结构的前提下,在运行时根据需要透明地为类层次结构上的各个类动态添加新的操作,从而避免上述问题。
代码:
None.gif public   abstract   class  Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract void Draw();
InBlock.gif    
//预料到将来可能会引入新的操作
InBlock.gif
    public abstract void accept(ShapeVisitor v, Context context); 
ExpandedBlockEnd.gif}

None.gif
None.gif
public   abstract   class  ShapeVisitor
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract void Visit(Rectangle shape, Context context); //#1
InBlock.gif
    public abstract void Visit(Circle shape, Context context); //#2
InBlock.gif
    public abstract void Visit(Line shape, Context context); //#3
ExpandedBlockEnd.gif
}

None.gif
None.gif
public   class  Rectangle:Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public override void Draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public override void Accept(ShapeVisitor v)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        v.Visit(
this,context);//调用#1 
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif
None.gif
public   class  Circle:Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public override void Draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public override void Accept(ShapeVisitor v)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        v.Visit(
this, context);//调用#2
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif
public   class  Line:Shape
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public override void Draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public override void Accept(ShapeVisitor v)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        v.Visit(
this, context);//调用#3
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif
None.gif
//
None.gif

None.gif
class  App
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    ShapeVisitor visitor ;
InBlock.gif    
public App (ShapeVisitor visitor)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.visitor = visitor ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void Process (Shape shape)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        shape.Accept(visitor);    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// *******
None.gif
App app  =   new  App( new  MyVisitor());
None.gifapp.Process(
new  line());


转载于:https://www.cnblogs.com/mapk/archive/2007/04/11/709706.html

你可能感兴趣的:(Visitor 访问者(行为型模式)--学习笔记)