设计模式之工厂模式
-
当一个类不知道它所必须创建的对象的类的时候。
-
当一个类希望由它的子类来指定它所创建的对象的时候。
-
当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪个帮助子类是代理者这一信息局部化的时候。
简单工厂模式
- 定义:提供一个创建对象的静态方法,根据传入的参数,返回具体的对象实例。
- 优点:减少了客户端与具体产品的耦合。
- 缺点:不符合开闭原则,增加新产品时需要修改工厂类代码。
假如需要根据情况创建三个类中的某一个,我们可能会写出这样的代码:
cpp
#include <iostream>
using namespace std;
class AbstractSmile
{
public:
virtual void transform() {}
virtual void ability() {}
virtual ~AbstractSmile() {}
};
// 人造恶魔果实· 绵羊形态
class SheepSmile : public AbstractSmile
{
public:
void transform() override
{
cout << "变成人兽 -- 山羊人形态..." << endl;
}
void ability() override
{
cout << "将手臂变成绵羊角的招式 -- 巨羊角" << endl;
}
};
// 人造恶魔果实· 狮子形态
class LionSmile : public AbstractSmile
{
public:
void transform() override
{
cout << "变成人兽 -- 狮子人形态..." << endl;
}
void ability() override
{
cout << "火遁· 豪火球之术..." << endl;
}
};
class BatSmile : public AbstractSmile
{
public:
void transform() override
{
cout << "变成人兽 -- 蝙蝠人形态..." << endl;
}
void ability() override
{
cout << "声纳引箭之万剑归宗..." << endl;
}
};
// 恶魔果实工厂类
enum class Type:char{SHEEP, LION, BAT};
class SmileFactory
{
public:
SmileFactory() {}
~SmileFactory() {}
AbstractSmile* createSmile(Type type)
{
AbstractSmile* ptr = nullptr;
switch (type)
{
case Type::SHEEP:
ptr = new SheepSmile;
break;
case Type::LION:
ptr = new LionSmile;
break;
case Type::BAT:
ptr = new BatSmile;
break;
default:
break;
}
return ptr;
}
};
int main()
{
SmileFactory* factory = new SmileFactory;
AbstractSmile* obj = factory->createSmile(Type::BAT);
obj->transform();
obj->ability();
return 0;
}
工厂方法模式
- 定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
- 优点:符合开闭原则,新增产品时只需要增加相应的工厂子类。
- 缺点:每增加一个产品都需要增加一个对应的工厂类,类的个数成对增加。
cpp
#include <iostream>
using namespace std;
class AbstractSmile
{
public:
virtual void transform() = 0;
virtual void ability() = 0;
virtual ~AbstractSmile() {}
};
// 人造恶魔果实· 绵羊形态
class SheepSmile : public AbstractSmile
{
public:
void transform() override
{
cout << "变成人兽 -- 山羊人形态..." << endl;
}
void ability() override
{
cout << "将手臂变成绵羊角的招式 -- 巨羊角" << endl;
}
};
// 人造恶魔果实· 狮子形态
class LionSmile : public AbstractSmile
{
public:
void transform() override
{
cout << "变成人兽 -- 狮子人形态..." << endl;
}
void ability() override
{
cout << "火遁· 豪火球之术..." << endl;
}
};
class BatSmile : public AbstractSmile
{
public:
void transform() override
{
cout << "变成人兽 -- 蝙蝠人形态..." << endl;
}
void ability() override
{
cout << "声纳引箭之万剑归宗..." << endl;
}
};
// 恶魔果实工厂类
class AbstractFactory
{
public:
virtual AbstractSmile* createSmile() = 0;
virtual ~AbstractFactory() {}
};
class SheepFactory : public AbstractFactory
{
public:
AbstractSmile* createSmile() override
{
return new SheepSmile;
}
~SheepFactory()
{
cout << "释放 SheepFactory 类相关的内存资源" << endl;
}
};
class LionFactory : public AbstractFactory
{
public:
// 工厂函数
AbstractSmile* createSmile() override
{
return new LionSmile;
}
~LionFactory()
{
cout << "释放 LionFactory 类相关的内存资源" << endl;
}
};
class BatFactory : public AbstractFactory
{
public:
// 工厂函数
AbstractSmile* createSmile() override
{
return new BatSmile;
}
~BatFactory()
{
cout << "释放 BatFactory 类相关的内存资源" << endl;
}
};
int main()
{
AbstractFactory* factory = new BatFactory;
AbstractSmile* obj = factory->createSmile();
obj->transform();
obj->ability();
return 0;
}
作者: 苏丙榅
链接: https://subingwen.cn/design-patterns/factory/
来源: 爱编程的大丙
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
抽象工厂模式
- 定义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
- 优点:可以创建一系列相关或者相互依赖的对象,使得系统更加符合高内聚低耦合的原则。
- 缺点:产品族扩展困难,增加新的产品等级结构需要修改抽象工厂类。
cpp
#include <iostream>
#include <string>
using namespace std;
// 船体
class ShipBody
{
public:
virtual string getShipBody() = 0;
virtual ~ShipBody() {}
};
class WoodBody : public ShipBody
{
public:
string getShipBody() override
{
return string("用<木材>制作轮船船体...");
}
};
class IronBody : public ShipBody
{
public:
string getShipBody() override
{
return string("用<钢铁>制作轮船船体...");
}
};
class MetalBody : public ShipBody
{
public:
string getShipBody() override
{
return string("用<合金>制作轮船船体...");
}
};
// 武器
class Weapon
{
public:
virtual string getWeapon() = 0;
virtual ~Weapon() {}
};
class Gun : public Weapon
{
public:
string getWeapon() override
{
return string("配备的武器是<枪>...");
}
};
class Cannon : public Weapon
{
public:
string getWeapon() override
{
return string("配备的武器是<自动机关炮>...");
}
};
class Laser : public Weapon
{
public:
string getWeapon() override
{
return string("配备的武器是<激光>...");
}
};
// 动力
class Engine
{
public:
virtual string getEngine() = 0;
virtual ~Engine() {}
};
class Human : public Engine
{
public:
string getEngine() override
{
return string("使用<人力驱动>...");
}
};
class Diesel : public Engine
{
public:
string getEngine() override
{
return string("使用<内燃机驱动>...");
}
};
class Nuclear : public Engine
{
public:
string getEngine() override
{
return string("使用<核能驱动>...");
}
};
// 轮船类
class Ship
{
public:
Ship(ShipBody* body, Weapon* weapon, Engine* engine) :
m_body(body), m_weapon(weapon), m_engine(engine)
{
}
string getProperty()
{
string info = m_body->getShipBody() + m_weapon->getWeapon() + m_engine->getEngine();
return info;
}
~Ship()
{
delete m_body;
delete m_engine;
delete m_weapon;
}
private:
ShipBody* m_body = nullptr;
Weapon* m_weapon = nullptr;
Engine* m_engine = nullptr;
};
// 工厂类
class AbstractFactory
{
public:
virtual Ship* createShip() = 0;
virtual ~AbstractFactory() {}
};
class BasicFactory : public AbstractFactory
{
public:
Ship* createShip() override
{
Ship* ship = new Ship(new WoodBody, new Gun, new Human);
cout << "<基础型>战船生产完毕, 可以下水啦..." << endl;
return ship;
}
};
class StandardFactory : public AbstractFactory
{
public:
Ship* createShip() override
{
Ship* ship = new Ship(new IronBody, new Cannon, new Diesel);
cout << "<标准型>战船生产完毕, 可以下水啦..." << endl;
return ship;
}
};
class UltimateFactory : public AbstractFactory
{
public:
Ship* createShip() override
{
Ship* ship = new Ship(new MetalBody, new Laser, new Nuclear);
cout << "<旗舰型>战船生产完毕, 可以下水啦..." << endl;
return ship;
}
};
int main()
{
AbstractFactory* factroy = new StandardFactory;
Ship* ship = factroy->createShip();
cout << ship->getProperty();
delete ship;
delete factroy;
return 0;
}