简单工厂模式
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <iostream> |
| #include <string> |
| using namespace std; |
| |
| |
| class Operation{ |
| public: |
| double a; |
| double b; |
| |
| virtual double calculate_a_b() = 0; |
| |
| }; |
| |
| class AddOperation:public Operation{ |
| double calculate_a_b(){ |
| return a + b; |
| } |
| }; |
| |
| class SubOperation:public Operation{ |
| double calculate_a_b(){ |
| return a - b; |
| } |
| }; |
| |
| class DivOperation:public Operation{ |
| double calculate_a_b(){ |
| return a / b; |
| } |
| }; |
| |
| class MulOperation:public Operation{ |
| double calculate_a_b(){ |
| return a * b; |
| } |
| }; |
| |
| |
| class OperationFactory{ |
| public: |
| static Operation* createOperation(char c){ |
| switch (c) { |
| case '+': |
| return new AddOperation; |
| case '-': |
| return new SubOperation; |
| case '/': |
| return new DivOperation; |
| case '*': |
| return new MulOperation; |
| default: |
| return nullptr; |
| } |
| } |
| }; |
| |
| int main(){ |
| Operation* operation = OperationFactory::createOperation('+'); |
| |
| if(operation != nullptr){ |
| operation->a = 1.5; |
| operation->b = 2.5; |
| |
| double result = operation->calculate_a_b(); |
| |
| cout << result << endl; |
| } |
| else{ |
| cout << "nullptr" << endl; |
| } |
| |
| |
| } |
| |
| |
抽象工厂模式
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <iostream> |
| #include <string> |
| using namespace std; |
| |
| |
| class Product{ |
| public: |
| virtual void show() = 0; |
| }; |
| |
| |
| class A_Product:public Product{}; |
| |
| class B_Product:public Product{}; |
| |
| |
| class M_A_Product: public A_Product{ |
| void show(){ |
| cout << "M Factory A Product" << endl; |
| } |
| }; |
| |
| class M_B_Product:public B_Product{ |
| void show(){ |
| cout << "M Factory B Product" << endl; |
| } |
| }; |
| |
| |
| class N_A_Product:public A_Product{ |
| void show(){ |
| cout << "N Factory A Product" << endl; |
| } |
| }; |
| |
| class N_B_Product:public B_Product{ |
| void show(){ |
| cout << "N Factory B Product" << endl; |
| } |
| }; |
| |
| |
| class Factory{ |
| public: |
| virtual A_Product* create_A() = 0; |
| virtual B_Product* create_B() = 0; |
| }; |
| |
| |
| class M_Factory:public Factory{ |
| A_Product* create_A(){ |
| return new M_A_Product; |
| } |
| |
| B_Product* create_B(){ |
| return new M_B_Product; |
| } |
| }; |
| |
| |
| |
| class N_Factory:public Factory{ |
| A_Product* create_A(){ |
| return new N_A_Product; |
| } |
| |
| B_Product* create_B(){ |
| return new N_B_Product; |
| } |
| }; |
| |
| int main(){ |
| Factory* factory_M = new M_Factory; |
| |
| A_Product* m_a_product = factory_M->create_A(); |
| B_Product* m_b_product = factory_M->create_B(); |
| |
| m_a_product->show(); |
| m_b_product->show(); |
| |
| delete factory_M; |
| delete m_a_product; |
| delete m_b_product; |
| |
| |
| |
| Factory* factory_N = new N_Factory; |
| |
| A_Product* n_a_product = factory_N->create_A(); |
| B_Product* n_b_product = factory_N->create_B(); |
| |
| n_a_product->show(); |
| n_b_product->show(); |
| |
| delete factory_N; |
| delete n_a_product; |
| delete n_b_product; |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!