|
| 1 | +/** |
| 2 | + * @file factory_method.cpp |
| 3 | + * @author Xuhua Huang |
| 4 | + * @brief |
| 5 | + * @version 0.1 |
| 6 | + * @date 2026-04-10 |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2026 |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include <functional> |
| 13 | +#include <iostream> |
| 14 | +#include <map> |
| 15 | +#include <memory> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +class registrar_t { |
| 19 | +public: |
| 20 | + virtual ~registrar_t() = default; |
| 21 | + |
| 22 | + virtual void admit_student(const std::string& name) const = 0; |
| 23 | +}; |
| 24 | + |
| 25 | +class computer_science_registrar_t : public registrar_t { |
| 26 | +public: |
| 27 | + void admit_student(const std::string& name) const override { std::cout << "CS registrar admitting " << name << "\n"; } |
| 28 | +}; |
| 29 | + |
| 30 | +class engineering_registrar_t : public registrar_t { |
| 31 | +public: |
| 32 | + void admit_student(const std::string& name) const override { |
| 33 | + std::cout << "Engineering registrar admitting " << name << "\n"; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +class math_registrar_t : public registrar_t { |
| 38 | +public: |
| 39 | + void admit_student(const std::string& name) const override { |
| 40 | + std::cout << "Math registrar admitting " << name << "\n"; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +class registrar_factory_t { |
| 45 | +public: |
| 46 | + virtual ~registrar_factory_t() = default; |
| 47 | + |
| 48 | + // factory method |
| 49 | + virtual std::unique_ptr<registrar_t> create() const = 0; |
| 50 | +}; |
| 51 | + |
| 52 | +class cs_factory_t : public registrar_factory_t { |
| 53 | +public: |
| 54 | + std::unique_ptr<registrar_t> create() const override { return std::make_unique<computer_science_registrar_t>(); } |
| 55 | +}; |
| 56 | + |
| 57 | +class eng_factory_t : public registrar_factory_t { |
| 58 | +public: |
| 59 | + std::unique_ptr<registrar_t> create() const override { return std::make_unique<engineering_registrar_t>(); } |
| 60 | +}; |
| 61 | + |
| 62 | +class math_factory_t : public registrar_factory_t { |
| 63 | +public: |
| 64 | + std::unique_ptr<registrar_t> create() const override { return std::make_unique<math_registrar_t>(); } |
| 65 | +}; |
| 66 | + |
| 67 | +void enroll_students( |
| 68 | + const std::map<std::string, std::unique_ptr<registrar_factory_t>>& factories, |
| 69 | + const std::map<std::string, std::string>& students_to_enroll |
| 70 | +) { |
| 71 | + for (const auto& [student, program] : students_to_enroll) { |
| 72 | + auto it = factories.find(program); |
| 73 | + if (it != factories.end()) { |
| 74 | + std::unique_ptr<registrar_t> registrar = it->second->create(); // factory method |
| 75 | + registrar->admit_student(student); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +using registrar_factory_fn_t = std::function<std::unique_ptr<registrar_t>()>; |
| 81 | + |
| 82 | +int main() { |
| 83 | + std::map<std::string, std::unique_ptr<registrar_factory_t>> factories; |
| 84 | + |
| 85 | + // std::map<std::string, registrar_factory_fn_t> factories; |
| 86 | + // factories["cs"] = [] { return std::make_unique<computer_science_registrar_t>(); }; |
| 87 | + // factories["eng"] = [] { return std::make_unique<engineering_registrar_t>(); }; |
| 88 | + |
| 89 | + factories["cs"] = std::make_unique<cs_factory_t>(); |
| 90 | + factories["eng"] = std::make_unique<eng_factory_t>(); |
| 91 | + factories["math"] = std::make_unique<math_factory_t>(); |
| 92 | + |
| 93 | + std::map<std::string, std::string> students_to_enroll{ |
| 94 | + {"jeff", "cs" }, |
| 95 | + {"bob", "eng" }, |
| 96 | + {"jane", "math"} |
| 97 | + }; |
| 98 | + |
| 99 | + enroll_students(factories, students_to_enroll); |
| 100 | +} |
0 commit comments