visit pattern

#include "Publicinc.hpp"

enum Gender
{
    Man = 0,
    Woman = 1,
};

class  CStudent;
class  CTeacher;

class  CVisitor
{
public:
     virtual ~CVisitor() {}
     virtual  void  visit(CStudent* student) = 0;
     virtual  void  visit(CTeacher* teacher) = 0;
};

class  CPerson
{
public:
    CPerson(const string& name, Gender gender)
        : m_name(name)
        , m_gender(gender)
    {
    }

    virtual  ~CPerson() {}

    virtual  void  Accept(CVisitor* visitor) = 0;

    string  GetName() const { return m_name; }
    Gender  GetGender() const { return m_gender; }

protected:
    string m_name;
    Gender m_gender;
};

class  CStudent : public CPerson
{
public:
    CStudent(const string& name, Gender gender, const string& number)
        : CPerson(name, gender)
        , m_number(number)
    {
    }

    virtual  void  Accept(CVisitor* visitor)
    {
        visitor->visit(this);

    }

    string  GetNumber() const

    {
        return m_number;
    }

private:
    string  m_number;
};

class  CTeacher : public CPerson
{
public:
    CTeacher(const string& name, Gender gender, int servicetime)
        : CPerson(name, gender)
        , m_servicetime(servicetime)
    {
    }

    virtual  void  Accept(CVisitor* visitor)
    {
        visitor->visit(this);
    }

    int  GetServiceTime() const
    {
        return m_servicetime;
    }

private:
    int  m_servicetime;
};

class  CPrinter : public CVisitor
{
public:
    virtual  void  visit(CStudent* student)
    {
        printf("name = {%s}\n", student->GetName().c_str());
        printf("gender = {%s}\n", student->GetGender() == Man ? "man" : "woman");
        printf("number = {%s}\n", student->GetNumber().c_str());
        printf("***********************\n");
    }

    virtual  void  visit(CTeacher* teacher)
    {
        printf("name = {%s}\n", teacher->GetName().c_str());
        printf("gender = {%s}\n", teacher->GetGender() == Man ? "man" : "woman");
        printf("number = {%d}\n", teacher->GetServiceTime());
        printf("***********************\n");
    }
};

class  Organization
{
    typedef  std::list   MemberList;
public:
    void  AddMember(CPerson* person)
    {
        m_persons.push_back(person);
    }

    void  RemoveMember(CPerson* person)
    {
        m_persons.remove(person);
    }

    void  PrintMember(CVisitor* visitor)
    {
        for (auto person : m_persons) {
            person->Accept(visitor);
        }
    }

    ~Organization()
    {
        for (auto& person : m_persons) {
            delete person;
            person = nullptr;
        }
        m_persons.clear();
    }

private:
    MemberList   m_persons;
};

void   TestCPersonVisitor()
{
    Organization  organ;
    organ.AddMember(new  CStudent("xiaoming", Man, "0812204"));
    organ.AddMember(new  CStudent("xiaohua",  Man, "0812205"));
    organ.AddMember(new  CStudent("xiaodong",   Man, "0812206"));
    organ.AddMember(new  CTeacher("xiaoli", Woman, 4));

    CPrinter  printer;
    organ.PrintMember(&printer);
}

你可能感兴趣的:(C++)