1.创建方法
概念: 这类方法将”调用构造函数生成对象”这个过程进行包装
作用: 将对象的创建细节进行隐藏, 并更好的体现代码意图, 从而增强代码的可维护性
模式图:
Department(部门)
Programmer(程序员)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| struct Programmer {}; struct Department { auto createEmployee() { return new Programmer(); } };
int main(void) { Department department{}; department.createEmployee(); return 0; }
|
2.静态创建方法
概念: 将原有的, 作为类成员函数的创建方法修改为一个类静态方法
模式图: 如创建方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| struct Programmer {}; struct Department { static auto createEmployee() { return new Programmer(); } };
int main(void) { Department::createEmployee(); return 0; }
|
3.简单工厂
概念: 在创建方法的基础之上, 让方法可以根据给定参数的不同来创建不同对象
模式图:
Department(部门)
Programmer Designer
↓ ↓
Programmer(程序员)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include <stdexcept>
enum class EmployeeType { Programmer, Designer, };
struct Employee { virtual ~Employee() {} };
struct Programmer : public Employee {}; struct Designer : public Employee {};
struct Department { Employee* createEmployee(EmployeeType type) { switch (type) { case EmployeeType::Programmer: return new Programmer(); case EmployeeType::Designer: return new Designer(); default: throw std::invalid_argument("unkown type.") } }; }
int main(void) { Department department{}; department.createEmployee(Employee::Designer); return 0; }
|
存在问题:Department类目前只能够创建类型位Programmer和Designer的员工(对象), 如果某一天, 需要增加新的员工类型, 便不得不再次修改createEmployee方法的实现细节
4.工厂方法模式
概念: 将对象的创建过程下放到不同的子类工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <iostream> #include <cstdio>
struct Employee { virtual ~Employee(){} virtual int registerAccount() const = 0; };
struct Programmer : public Employee { int registerAccount() const override { const auto accountNo = rand(); std::cout << "A programmer account: " << accountNo << ".\n"; return accountNo; } };
struct Designer : public Employee { int registerAccount() const override { const auto accountNo = rand(); std::cout << "A designer account: " << accountNo << ".\n"; return accountNo; } };
struct Department { virtual ~Department(){} virtual Employee* createEmployee() const = 0; void onboard() const { Employee* employee = this->createEmployee(); employee->registerAccount(); delete employee; } };
struct ITDepartment : Department { Employee* createEmployee() const override { return new Programmer(); } }
struct UIDepartment : Department { Employee* createEmployee() const override { return new Designer(); } }
int main(void) { ITDepartment itDepartment{}; itDepartment.onboard(); return 0; }
|
5.抽象工厂
概念: 产生产品族,根据产品族进行一系列业务操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include <iostream> #include <vector> #include <unordered_map>
struct Employee { virtual ~Employee() {} virtual int registerAccount() const = 0; };
struct Project { virtual ~Project() {} virtual void assignTo(int) const = 0; };
struct Programmer : public Employee { int registerAccount() const override { const auto accountNo = rand(); std::cout << "A programmer account: " << accountNo << ".\n"; return accountNo; } };
struct Designer : public Employee { int registerAccount() const override { const auto accountNo = rand(); std::cout << "A designer account: " << accountNo <<".\n"; return accountNo; } };
struct ITProject : public Project { void assignTo(int accountNo) const override { std::cout << "Assign an IT project to" << accountNo << ".\n"; } };
struct UIProject : public Project { void assignTo(int accountNo) const override { std::cout << "Assign an UI project to" << accountNo << ".\n"; } };
struct Department { virtual ~Department() {} virtual Employee* createEmployee() const = 0; virtual Project* createProject() const = 0; };
struct ITDepartment : public Department { Employee* createEmployee() const override { return new Programmer(); } Project* createProject() const override { return new ITProject(); } };
struct UIDepartment : public Department { Employee* createEmployee() const override { return new Designer(); } Project* createProject() const override { return new UIProject(); } };
struct DepartmentManager { const Department& department; std::vector<Project*> projects; std::unordered_map<int, Employee*> employees; explicit DepartmentManager(const Department& department) : department(department) { } auto createProject() { const auto project = department.createProject(); projects.push_back(project); return project; } int createEmployee() { const auto employee = department.createEmployee(); const auto accountNo = employee->registerAccount(); employees[accountNo] = employee; return accountNo; } };
int main(void) { ITDepartment itDepartment{}; DepartmentManager itDepartmentManager {itDepartment}; const auto project = itDepartmentManager.createProject(); const auto accountNo = itDepartmentManager.createEmployee(); project->assignTo(accountNo);
return 0; }
|