lxg

导航

 

1.定义:
抽象访问者(Visitor)角色:声明了一个或者多个访问操作,形成所有的具体元素角色必须实现的接口。
具体访问者(ConcreteVisitor)角色:实现抽象访问者角色所声明的接口,也就是抽象访问者所声明的各个访问操作。
抽象节点(Element)角色:声明一个接受操作,接受一个访问者对象作为一个参量。
具体节点(ConcreteElement)角色:实现了抽象元素所规定的接受操作。
结构对象(ObiectStructure)角色:有如下的一些责任,可以遍历结构中的所有元素;如果需要,
提供一个高层次的接口让访问者对象可以访问每一个元素;如果需要,可以设计成一个复合对象或者一个聚集,如列(list,vector)或集合(set)。
2.代码实现:

 

 

 

 

 

 

 

 

 

 

 

 

 3.测试如下:

  CShapeManager* m_pShapeManager = new CShapeManager();
    CRectShape* rectShape = new CRectShape(QRect(0,0,100,100));
    CCircleShape* circleShape = new CCircleShape(QRect(0,0,100,100));
    CTriangleShape* triangleShape = new CTriangleShape(QRect(0,0,100,100));
    m_pShapeManager->addShape(rectShape);
    m_pShapeManager->addShape(circleShape);
    m_pShapeManager->addShape(triangleShape);

    CDrawVisitor vistor;
    m_pShapeManager->getShape(0)->visitDraw(&vistor);
    m_pShapeManager->getShape(1)->visitDraw(&vistor);
    m_pShapeManager->getShape(2)->visitDraw(&vistor);

    m_pShapeManager->removeShape(rectShape);
    m_pShapeManager->removeShape(0);
    qDebug()<<m_pShapeManager->getSize();
    m_pShapeManager->acceptShape(&vistor);

 

posted on 2020-07-05 09:30  lxg_7105  阅读(133)  评论(0编辑  收藏  举报