工厂模式都是在那种有着大量类的程序里面使用的,比如我突然要产生许多的类,这时候就可以用工厂模式,工厂模式有如下三种类型。
简单工厂
用户输入实例化的信息(比如产品名称),向工厂申请对象,工厂返回相应的对象,用户完全不需要知道,这个对象的具体构造方式,封装了对象的创建。
cpp
#include<iostream>
using namespace std;
class Car
{
public:
Car() {}
~Car() {}
virtual void show() = 0;
};
class Audi:public Car
{
public:
Audi() {}
~Audi() {}
void show()
{
cout << "this is Audi" << endl;
}
};
class Baoma :public Car
{
public:
Baoma() {}
~Baoma() {}
void show()
{
cout << "this is Baoma" << endl;
}
};
enum CarType
{
BAOMA,AUDI
};
class SimpleFactory
{
public:
SimpleFactory() {}
~SimpleFactory(){}
Car* createCar(CarType type)
{
switch (type)
{
case BAOMA:
return new Baoma();
case AUDI:
return new Audi();
}
}
};
int main()
{
SimpleFactory factory;
unique_ptr< Car>myaudi(factory.createCar(AUDI));
unique_ptr< Car>mybaoma(factory.createCar(BAOMA));
myaudi->show();
mybaoma->show();
return 0;
}
简单工厂存在明显的缺点,那就是如果要产生新的产品,就需要改变源头的工厂类,因为简单工厂只有一个工厂,这不符合软件设计的开闭原则。
工厂方法
实现能够产生不同产品的工厂,用这些工厂产生它们对应的产品,如果有新的产品,就添加工厂类就可以了,完全不用改变之前实现的工厂类。
cpp
#include<iostream>
using namespace std;
class Car
{
public:
Car() {}
~Car() {}
virtual void show() = 0;
};
class Audi:public Car
{
public:
Audi() {}
~Audi() {}
void show()
{
cout << "this is Audi" << endl;
}
};
class Baoma :public Car
{
public:
Baoma() {}
~Baoma() {}
void show()
{
cout << "this is Baoma" << endl;
}
};
class FactoryMethod
{
public:
virtual Car* createCar() = 0;
};
class AudiFactory:public FactoryMethod
{
public:
Car* createCar()
{
return new Audi();
}
};
class BaoMaFactory:public FactoryMethod
{
public:
Car* createCar()
{
return new Baoma();
}
};
int main()
{
unique_ptr<FactoryMethod>audifactory(new AudiFactory());
unique_ptr<FactoryMethod>baomafactory(new BaoMaFactory());
unique_ptr< Car>myaudi(audifactory->createCar());
unique_ptr< Car>mybaoma(baomafactory->createCar());
myaudi->show();
mybaoma->show();
return 0;
}
抽象工厂
把有关联关系的,属于一个产品簇的所有产品创建的接口函数,放在一个抽象工厂里面,派生类应该负责创建该产品簇里面的所有产品,比如华为和小米都是派生类,抽象工厂要生产手机和耳机以及充电头,那么小米和华为这两个派生类工厂,就要产生这些产品。
显然,抽象工厂也有不好的地方,假如我华为要产生手机芯片这一新的产品,那么抽象工厂就要跟着改变,由于抽象工厂类是抽象的,所以所有派生类都要实现那个产生芯片的函数,不然派生类也会变成抽象类,但是小米不一定要自己生产芯片。
cpp
#include<iostream>
using namespace std;
class Car
{
public:
Car() {}
~Car() {}
virtual void show() = 0;
};
class Audi:public Car
{
public:
Audi() {}
~Audi() {}
void show()
{
cout << "this is Audi" << endl;
}
};
class Baoma :public Car
{
public:
Baoma() {}
~Baoma() {}
void show()
{
cout << "this is Baoma" << endl;
}
};
class Light
{
public:
Light() {}
~Light() {}
virtual void show() = 0;
};
class AudiLight:public Light
{
public:
AudiLight() {}
~AudiLight() {}
void show()
{
cout << "this is AudiLight" << endl;
}
};
class BaoMaLight :public Light
{
public:
BaoMaLight() {}
~BaoMaLight() {}
void show()
{
cout << "this is BaoMaLight" << endl;
}
};
class AbstractFactory
{
public:
virtual Car* createCar() = 0;
virtual Light* createLight() = 0;
};
class AudiFactory:public AbstractFactory
{
public:
Car* createCar()
{
return new Audi();
}
Light* createLight()
{
return new AudiLight();
}
};
class BaoMaFactory:public AbstractFactory
{
public:
Car* createCar()
{
return new Baoma();
}
Light* createLight()
{
return new BaoMaLight();
}
};
int main()
{
unique_ptr<AbstractFactory>audifactory(new AudiFactory());
unique_ptr<AbstractFactory>baomafactory(new BaoMaFactory());
unique_ptr< Car>myaudi(audifactory->createCar());
unique_ptr< Car>mybaoma(baomafactory->createCar());
unique_ptr<Light>myaudilight(audifactory->createLight());
unique_ptr<Light>mybaomalight(baomafactory->createLight());
myaudi->show();
myaudilight->show();
mybaoma->show();
mybaomalight->show();
return 0;
}
显然,抽象工厂也有不好的地方,假如我华为要产生手机芯片这一新的产品,那么抽象工厂就要跟着改变,由于抽象工厂类是抽象的,所以所有派生类都要实现那个产生芯片的函数,不然派生类也会变成抽象类,但是小米不一定要自己生产芯片。