C ++ Design Patterns: Guest Mode

Guest mode: In layman's terms, is to define a role of visitors, when the specified roles access by visitors to access.

Invasive guest mode is moderate, only a plus provide reception of foreign visitors interfaces to be accessed inside the class.

Guest mode advantages:

  1. In line with the principle of single responsibility. Load specific elements of role is responsible for the data, and to show the role of visitors in charge of the report, two very different responsibilities clearly separated, their interpretation of the change.
  2. Excellent scalability due to segregation of duties, continue to increase the operation of the data is very fast.

Guest mode scenarios:

  1. Object structure rarely changes corresponding to the object class, but often need to define a new operation on the object structure.
  2. The need for objects for a structure of objects in many different and unrelated operations, and these operations need to avoid "contamination" class of these objects, we do not want to modify these classes when adding new operations.
class Visitor;

//被访问类基类:  网站
class Website
{
public:
    virtual ~Website(){ std::cout << "~Website()" << std::endl;}
    virtual void accept(Visitor&) = 0;
};

//被访问类具体实现类:  淘宝网
class TaoBao : public Website
{
public:
    void accept(Visitor &v) override;

    void shopping();
};

//被访问类具体实现类: 优酷
class YouKu : public Website
{
public:
    void accept(Visitor &v) override;

    void playVideo();
};

//访客类基类
class Visitor
{
public:
    Visitor() = default;
    Visitor(const std::string &name) : m_name(name){}

    virtual ~Visitor(){ std::cout << "~Visitor()" << std::endl;}
    virtual void visit(TaoBao &web) = 0;
    virtual void visit(YouKu &web) = 0;

protected:
    std::string m_name{"unknow"};
};

//访客具体实现类:  普通游客用户
class GeneralVisitor : public Visitor
{
public:
    void visit(TaoBao &web) override;
    void visit(YouKu &web) override;
};

//访客具体实现类:  VIP用户
class VIPVisitor : public Visitor
{
public:
    VIPVisitor(const std::string &name) : Visitor(name){}

    void visit(TaoBao &web) override;
    void visit(YouKu &web) override;

};

//
void YouKu::accept(Visitor &v)
{
    v.visit(*this);
}

void YouKu::playVideo()
{
    std::cout << "Watch the video" << std::endl;
}

void TaoBao::accept(Visitor &v)
{
    v.visit(*this);
}

void TaoBao::shopping()
{
    std::cout << "Online shopping" << std::endl;
}

void GeneralVisitor::visit(TaoBao &web)
{
    web.shopping();
}

void GeneralVisitor::visit(YouKu &web)
{
    web.playVideo();
}

void VIPVisitor::visit(TaoBao &web)
{
    std::cout << m_name << ": ";
    web.shopping();
}

void VIPVisitor::visit(YouKu &web)
{
    std::cout << m_name << ": ";
    web.playVideo();
}

//测试
int main()
{
    TaoBao tb;
    YouKu yk;
    GeneralVisitor gVisitor;
    VIPVisitor vVisitor{"zhangsan"};

    yk.accept(gVisitor);
    tb.accept(gVisitor);

    yk.accept(vVisitor);
    tb.accept(vVisitor);

    return 0;
}

, When a larger number of access classes, from the above method for the preparation of a large amount of code needed view corresponding class in the visitor. Additional methods visitor class is implemented depends on the concrete class visitor class, not dependent on the abstract class.

Guess you like

Origin www.cnblogs.com/chengjundu/p/11762747.html