设计模式-创建型-抽象工厂
生活随笔
收集整理的這篇文章主要介紹了
设计模式-创建型-抽象工厂
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#pragma once#ifndef ABSTRACTFACTORY_H #define ABSTRACTFACTORY_H // 抽象基類AbstractProductA,代表產品A 的抽象
class AbstractProductA
{
public: AbstractProductA() {} virtual ~AbstractProductA(){};
}; // 派生類ConcreateProductA1,繼承自AbstractProductA,代表產品A 的第一種實現
class ConcreateProductA1 : public AbstractProductA
{
public: ConcreateProductA1(); virtual ~ConcreateProductA1();
}; // 派生類ConcreateProductA2,繼承自AbstractProductA,代表產品A 的第二種實現
class ConcreateProductA2 : public AbstractProductA
{
public: ConcreateProductA2(); virtual ~ConcreateProductA2();
}; // 抽象基類AbstractProductB,代表產品B 的抽象
class AbstractProductB
{
public: AbstractProductB() {} virtual ~AbstractProductB(){};
}; // 派生類ConcreateProductB1,繼承自AbstractProductB,代表產品B 的第一種實現
class ConcreateProductB1 : public AbstractProductB
{
public: ConcreateProductB1(); virtual ~ConcreateProductB1();
}; // 派生類ConcreateProductB2,繼承自AbstractProductB,代表產品B 的第二種實現
class ConcreateProductB2 : public AbstractProductB
{
public: ConcreateProductB2(); virtual ~ConcreateProductB2();
}; // 抽象基類AbstractFactory,工廠的抽象類,生產產品A 和產品B
class AbstractFactory
{
public: AbstractFactory(){} virtual ~AbstractFactory(){} virtual AbstractProductA* CreateProductA() = 0; virtual AbstractProductB* CreateProductB() = 0;
}; // 派生類ConcreateFactory1,繼承自AbstractFactory // 生產產品A 和產品B 的第一種實現
class ConcreateFactory1 : public AbstractFactory
{
public: ConcreateFactory1(); virtual ~ConcreateFactory1(); virtual AbstractProductA* CreateProductA(); virtual AbstractProductB* CreateProductB();
}; // 派生類ConcreateFactory2,繼承自AbstractFactory // 生產產品A 和產品B 的第二種實現
class ConcreateFactory2 : public AbstractFactory
{
public: ConcreateFactory2(); virtual ~ConcreateFactory2(); virtual AbstractProductA* CreateProductA(); virtual AbstractProductB* CreateProductB();
}; #endif
#include "StdAfx.h" #include "abstractfactory_impl.h"#include <iostream> ConcreateProductA1::ConcreateProductA1() { std::cout << "construction of ConcreateProductA1\n"; } ConcreateProductA1::~ConcreateProductA1() { std::cout << "destruction of ConcreateProductA1\n"; } ConcreateProductA2::ConcreateProductA2() { std::cout << "construction of ConcreateProductA2\n"; } ConcreateProductA2::~ConcreateProductA2() { std::cout << "destruction of ConcreateProductA2\n"; } ConcreateProductB1::ConcreateProductB1() { std::cout << "construction of ConcreateProductB1\n"; } ConcreateProductB1::~ConcreateProductB1() { std::cout << "destruction of ConcreateProductB1\n"; } ConcreateProductB2::ConcreateProductB2() { std::cout << "construction of ConcreateProductB2\n"; } ConcreateProductB2::~ConcreateProductB2() { std::cout << "destruction of ConcreateProductB2\n"; } ConcreateFactory1::ConcreateFactory1() { std::cout << "construction of ConcreateFactory1\n"; } ConcreateFactory1::~ConcreateFactory1() { std::cout << "destruction of ConcreateFactory1\n"; } AbstractProductA* ConcreateFactory1::CreateProductA() { return new ConcreateProductA1(); } AbstractProductB* ConcreateFactory1::CreateProductB() { return new ConcreateProductB1(); } ConcreateFactory2::ConcreateFactory2() { std::cout << "construction of ConcreateFactory2\n"; } ConcreateFactory2::~ConcreateFactory2() { std::cout << "destruction of ConcreateFactory2\n"; } AbstractProductA* ConcreateFactory2::CreateProductA() { return new ConcreateProductA2(); } AbstractProductB* ConcreateFactory2::CreateProductB() { return new ConcreateProductB2(); }
// AbstractFactory.cpp : 定義控制臺應用程序的入口點。 //#include "stdafx.h"#include "abstractfactory_impl.h" #include <stdlib.h>//提供一個創建一系列相關或相互依賴對象的接口,而無需指定它們具體的類。 int _tmain(int argc, _TCHAR* argv[]) {// 生產產品A 的第一種實現 AbstractFactory *pFactory1 = new ConcreateFactory1; AbstractProductA *pProductA = pFactory1->CreateProductA(); // 生產產品B 的第二種實現 AbstractFactory *pFactory2 = new ConcreateFactory2; AbstractProductB *pProductB = pFactory2->CreateProductB(); delete pFactory1; delete pProductA; delete pFactory2; delete pProductB; system("pause"); return 0; }
#include "StdAfx.h" #include "abstractfactory_impl.h"#include <iostream> ConcreateProductA1::ConcreateProductA1() { std::cout << "construction of ConcreateProductA1\n"; } ConcreateProductA1::~ConcreateProductA1() { std::cout << "destruction of ConcreateProductA1\n"; } ConcreateProductA2::ConcreateProductA2() { std::cout << "construction of ConcreateProductA2\n"; } ConcreateProductA2::~ConcreateProductA2() { std::cout << "destruction of ConcreateProductA2\n"; } ConcreateProductB1::ConcreateProductB1() { std::cout << "construction of ConcreateProductB1\n"; } ConcreateProductB1::~ConcreateProductB1() { std::cout << "destruction of ConcreateProductB1\n"; } ConcreateProductB2::ConcreateProductB2() { std::cout << "construction of ConcreateProductB2\n"; } ConcreateProductB2::~ConcreateProductB2() { std::cout << "destruction of ConcreateProductB2\n"; } ConcreateFactory1::ConcreateFactory1() { std::cout << "construction of ConcreateFactory1\n"; } ConcreateFactory1::~ConcreateFactory1() { std::cout << "destruction of ConcreateFactory1\n"; } AbstractProductA* ConcreateFactory1::CreateProductA() { return new ConcreateProductA1(); } AbstractProductB* ConcreateFactory1::CreateProductB() { return new ConcreateProductB1(); } ConcreateFactory2::ConcreateFactory2() { std::cout << "construction of ConcreateFactory2\n"; } ConcreateFactory2::~ConcreateFactory2() { std::cout << "destruction of ConcreateFactory2\n"; } AbstractProductA* ConcreateFactory2::CreateProductA() { return new ConcreateProductA2(); } AbstractProductB* ConcreateFactory2::CreateProductB() { return new ConcreateProductB2(); }
// AbstractFactory.cpp : 定義控制臺應用程序的入口點。 //#include "stdafx.h"#include "abstractfactory_impl.h" #include <stdlib.h>//提供一個創建一系列相關或相互依賴對象的接口,而無需指定它們具體的類。 int _tmain(int argc, _TCHAR* argv[]) {// 生產產品A 的第一種實現 AbstractFactory *pFactory1 = new ConcreateFactory1; AbstractProductA *pProductA = pFactory1->CreateProductA(); // 生產產品B 的第二種實現 AbstractFactory *pFactory2 = new ConcreateFactory2; AbstractProductB *pProductB = pFactory2->CreateProductB(); delete pFactory1; delete pProductA; delete pFactory2; delete pProductB; system("pause"); return 0; }
總結
以上是生活随笔為你收集整理的设计模式-创建型-抽象工厂的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows下live555+rtsp
- 下一篇: 设计模式-创建型-生成器