c++ 工厂模式简介和应用场景
生活随笔
收集整理的這篇文章主要介紹了
c++ 工厂模式简介和应用场景
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工廠模式簡介和應用場景
一、簡介
工廠模式主要是為創建對象提供了接口。工廠模式按照《Java與模式》中的提法分為三類:
1. 簡單工廠模式 (Simple Factory)
2. 工廠方法模式 (Factory Method)
3. 抽象工廠模式 (Abstract Factory)
一、簡單工廠模式
簡單工廠模式:一個工廠,多個產品。產品需要有一個虛基類。通過傳入參數,生成具體產品對象,并利用基類指針指向此對象。通過工廠獲取此虛基類指針,通過運行時多肽,調用子類實現。
simple_factory.h
#ifndef SIMPLE_FACTORY_H #define SIMPLE_FACTORY_H #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> //抽象一個產品基類 class Product { public:virtual void show() = 0; }; //簡單工廠類 class simple_factory { public:simple_factory();//create()方法通常是靜態的,所以也稱之為靜態工廠static Product * create(int type); };#endif // SIMPLE_FACTORY_H?simple_factory.cpp
#include "simple_factory.h" //具體的A產品類 class Product_A : public Product { public:void show(){printf( "Product_A" );} }; //具體的B產品類 class Product_B : public Product { public:void show(){printf ( "Product_B" );} };simple_factory::simple_factory() {} //生成具體產品對象,并利用基類指針指向此對象 Product * simple_factory::create(int type) {switch (type){case 1:return new Product_A;break;case 2:return new Product_B;break;default:break;} }main.cpp
#include <iostream> #include "simple_factory.h" using namespace std; // main.cpp : 定義控制臺應用程序的入口點。 int main() {simple_factory::create(1)->show();simple_factory::create(2)->show();pause();return 0; }二、簡單工廠模式
1.1 定義
工廠方法模式,又稱工廠模式、多態工廠模式和虛擬構造器模式,通過定義工廠父類負責定義創建對象的公共接口,而子類則負責生成具體的對象,工廠虛基類。
1.2 主要作用
將類的實例化(具體產品的創建)延遲到工廠類的子類(具體工廠)中完成,即由子類來決定應該實例化(創建)哪一個類。
1.3 解決的問題
工廠一旦需要生產新產品就需要修改工廠類的方法邏輯,違背了“開放 - 關閉原則
?
#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>using namespace std;class Product { public:virtual void show() = 0; };class Product_A : public Product { public:void show(){printf ( "Product_A\n" );} };class Product_B : public Product { public:void show(){printf ( "Product_B\n" );} };class Factory { public:virtual Product* create() = 0; };class Factory_A : public Factory { public:Product* create(){return new Product_A;} };class Factory_B : public Factory { public:Product* create(){return new Product_B;} };int main() {Factory_A* productA = new Factory_A();Factory_B* productB = new Factory_B();productA->create()->show();productB->create()->show();pause();return 0; }三、抽象工廠模式
多個工廠,多個產品,并且每個產品可以包含多個型號。此時工廠和產品都是通過虛基類的方式構建。每一個工廠類可以生產同一個產品的多個型號。
#include "abstract_mode_factory.h" #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <iostream> using namespace std;abstract_mode_factory::abstract_mode_factory() { }//定義抽象類 class product1 { public:virtual void show() = 0; };//定義具體類 class product_A1 :public product1 { public:void show(){ cout << "product A1" << endl; } };class product_B1 :public product1 { public:void show(){ cout << "product B1" << endl; } };//定義抽象類 class product2 { public:virtual void show() = 0; };//定義具體類 class product_A2 :public product2 { public:void show(){ cout << "product A2" << endl; } };class product_B2 :public product2 { public:void show(){ cout << "product B2" << endl; } };class Factory { public:virtual product1 *creat1() = 0;virtual product2 *creat2() = 0; };class abstract_mode_factoryA { public:product1 *creat1(){ return new product_A1(); }product2 *creat2(){ return new product_A2(); } };class abstract_mode_factoryB { public:product1 *creat1(){ return new product_B1(); }product2 *creat2(){ return new product_B2(); } };int main() {abstract_mode_factoryA *factoryA= new abstract_mode_factoryA();factoryA->creat1()->show();factoryA->creat2()->show();abstract_mode_factoryB *factoryB = new abstract_mode_factoryB();factoryB->creat1()->show();factoryB->creat2()->show();return 0; }?
總結
以上是生活随笔為你收集整理的c++ 工厂模式简介和应用场景的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 闲谈绩效考核——来自项目管理群的讨论
- 下一篇: 大学计算机基础技能论文,计算机基础论文,