码迷,mamicode.com
首页 > 其他好文 > 详细

简单Factory模式

时间:2014-07-13 00:36:51      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   div   

#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;
}

 

简单Factory模式,布布扣,bubuko.com

简单Factory模式

标签:style   blog   color   os   io   div   

原文地址:http://www.cnblogs.com/zhidao-chen/p/3838157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!