首页 > 代码库 > 简单Factory模式
简单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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。