工厂模式简介和应用场景
一、简介
工厂模式主要是为创建对象提供了接口。工厂模式按照《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;
}