工厂模式:利用C++多态的特性,对对象的创建进行封装。
定义一个创建对象的接口,让其子类自己决定实力化哪一个工厂类,工厂模式使其创建过程延迟到子类进行
参考链接
- 简单工厂
cpp
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
Car(string name) : m_name(name) {}
~Car() {}
virtual void show() = 0;
protected:
string m_name;
};
class Bmw : public Car
{
public:
Bmw(string name) : Car(name){}
void show()
{
cout << "获取了一辆宝马汽车: " << m_name << endl;
}
};
class Audi : public Car
{
public:
Audi(string name) : Car(name) {}
void show()
{
cout << "获取了一辆奥迪汽车: " << m_name << endl;
}
};
// 简单工厂
enum carType
{
BMW,
AUDI,
};
class simpleFactory
{
public:
Car* createCar(carType ct)
{
switch (ct)
{
case BMW:
return new Bmw("x6");
case AUDI:
return new Audi("a8");
default:
cerr << "传入的参数错误" << ct << endl;
}
return nullptr;
}
};
int main()
{
// 智能指针 在使用了new以后不需要delete
/*unique_ptr<simpleFactory> fac(new simpleFactory());
unique_ptr<Car> p1(fac->createCar(BMW));
unique_ptr<Car> p2(fac->createCar(AUDI));*/
simpleFactory* fac = new simpleFactory();
Car* p1 = fac->createCar(BMW);
Car* p2 = fac->createCar(AUDI);
p1->show();
p2->show();
return 0;
}
- 工厂方法
cpp
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
Car(string name) : m_name(name) {}
~Car() {}
virtual void show() = 0;
protected:
string m_name;
};
class Bmw : public Car
{
public:
Bmw(string name) : Car(name){}
void show()
{
cout << "获取了一辆宝马汽车: " << m_name << endl;
}
};
class Audi : public Car
{
public:
Audi(string name) : Car(name) {}
void show()
{
cout << "获取了一辆奥迪汽车: " << m_name << endl;
}
};
// 工厂方法
// 基类
class factory
{
public:
virtual Car* createCar(string name) = 0;
};
// 子类1
class BmwFac : public factory
{
public:
Car* createCar(string name)
{
return new Bmw(name);
}
};
// 子类2
class AudiFac : public factory
{
public:
Car* createCar(string name)
{
return new Audi(name);
}
};
int main()
{
unique_ptr<factory> bmwFty(new BmwFac());
unique_ptr<factory> audiFty(new AudiFac());
unique_ptr<Car> p3(bmwFty->createCar("x7"));
unique_ptr<Car> p4(audiFty->createCar("a9"));
p3->show();
p4->show();
return 0;
}
- 抽象工厂
cpp
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
Car(string name) : m_name(name) {}
~Car() {}
virtual void show() = 0;
protected:
string m_name;
};
class Bmw : public Car
{
public:
Bmw(string name) : Car(name){}
void show()
{
cout << "获取了一辆宝马汽车: " << m_name << endl;
}
};
class Audi : public Car
{
public:
Audi(string name) : Car(name) {}
void show()
{
cout << "获取了一辆奥迪汽车: " << m_name << endl;
}
};
// 抽象工厂
// 增加另外一个类
class Light
{
public:
virtual void show() = 0;
};
class BmwLight : public Light
{
public:
void show()
{
cout << "BMW Light" << endl;
}
};
class AudiLight : public Light
{
public:
void show()
{
cout << "AUDI Light" << endl;
}
};
class AbstractFactory
{
public:
virtual Car* createCar(string name) = 0;
virtual Light* createLight() = 0;
};
class BMWFactory : public AbstractFactory
{
public:
Car* createCar(string name)
{
return new Bmw(name);
}
Light* createLight()
{
return new BmwLight();
}
};
class AUDIFactory : public AbstractFactory
{
public:
Car* createCar(string name)
{
return new Audi(name);
}
Light* createLight()
{
return new AudiLight();
}
};
int main()
{
unique_ptr<AbstractFactory> bmwfty(new BMWFactory());
unique_ptr<AbstractFactory> audifty(new AUDIFactory());
unique_ptr<Car> p5(bmwfty->createCar("x8"));
unique_ptr<Car> p6(audifty->createCar("a10"));
unique_ptr<Light> p7(bmwfty->createLight());
unique_ptr<Light> p8(audifty->createLight());
p5->show();
p6->show();
p7->show();
p8->show();
return 0;
}
其实感觉对于工厂模式的实现,看到第二种方式就差不多了,第三种只是在第二种基础上再增加一个类而已。之前在前司的时候项目中的代码有看到过这种设计模式的,当时还不了解设计模式,当时调代码的时候一步一步的走到了父类,父类还是一个模版,所以印象深刻。