简单Factory模式

#pragma once
#include "student.h"
#include "Teacher.h"

typedef enum _EPersonType{
    ePersonUndefin = 0,
    ePersonStudent,
    ePersonTeacher
}EPersonType;

class Factory
{
public:
    Factory(void);
    ~Factory(void);
    Person *CreatPerson(EPersonType personType);
};


Person* Factory::CreatPerson(EPersonType personType){
    Person *pPerson = 0;
    switch (personType)
    {
    case ePersonStudent:
        {
            pPerson = new Student();
            break;
        }
    case ePersonTeacher:
        {
            pPerson = new Teacher();
            break;
        }
    }
    return pPerson;
}

#pragma once
#include <iostream>
using namespace std;
class Person
{
public:
    Person(void);
    virtual ~Person(void);
};

#include "Person.h"
Person::Person(void)
{
}

Person::~Person(void)
{
}

#pragma once
#include "person.h"
class Student :
    public Person
{
public:
    Student(void);
    ~Student(void);
};

#include "Student.h"
Student::Student(void)
{
    cout<<"Student::Student()"<<endl;
}

#pragma once
#include "person.h"
class Teacher :
    public Person
{
public:
    Teacher(void);
    ~Teacher(void);
};

#include "Teacher.h"
Teacher::Teacher(void)
{
    cout<<"Teacher::Teacher()"<<endl;
}

#include "Factory.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Factory factory;
    factory.CreatPerson(ePersonStudent);//ePersonTeacher
    return 0;
}

 

posted @ 2014-07-11 17:02  酱酱爱  阅读(303)  评论(0编辑  收藏  举报