公众号 手机网站开发,网站后台登陆密码忘记了,北京建设网坡屋顶改造工程指标,免费建设小说网站链接#xff1a;C 设计模式 链接#xff1a;C 设计模式 - 单例模式
享元模式#xff08;Flyweight Pattern#xff09;是一种结构型设计模式#xff0c;它通过共享尽可能多的相同对象来减少内存使用和提高性能。享元模式适用于大量细粒度对象的场景#xff0c;这些对象之…链接C 设计模式 链接C 设计模式 - 单例模式
享元模式Flyweight Pattern是一种结构型设计模式它通过共享尽可能多的相同对象来减少内存使用和提高性能。享元模式适用于大量细粒度对象的场景这些对象之间有很多相同的部分可以通过共享来减少内存消耗。
1.问题分析
在开发中尤其是在需要大量细粒度对象的场景中内存使用和性能优化是两个重要的考虑因素。例如在图形界面、文本编辑器、游戏开发等领域可能会有大量相似或相同的对象需要频繁创建和使用。如果每次都创建新的对象不仅会占用大量内存还会导致性能下降。
为了解决这些问题我们可以结合工厂方法模式和延迟初始化来实现享元模式。具体来说我们可以定义一个工厂类通过延迟初始化和独立工厂函数来动态创建和缓存具体的对象。
2.实现步骤
定义享元接口定义一个抽象的接口包含一个执行方法。实现具体享元类实现享元接口定义具体的类。定义独立工厂函数定义一个独立的工厂函数用于根据类型动态创建具体的对象。实现享元工厂实现一个工厂类通过延迟初始化和独立工厂函数来动态创建和缓存具体的对象。客户端代码使用享元工厂获取具体对象并执行。
3.代码示例
以机器人舞蹈动作作为示例。
3.1.定义享元接口
// 享元接口
class DanceMove {public:virtual ~DanceMove() default;virtual void execute() 0;
};3.2.实现具体享元类
// 具体享元类1抬手动作
class RaiseHand : public DanceMove {public:void execute() override { std::cout Executing Raise Hand Move std::endl; }
};// 具体享元类2转身动作
class TurnAround : public DanceMove {public:void execute() override { std::cout Executing Turn Around Move std::endl; }
};// 具体享元类3踢腿动作
class KickLeg : public DanceMove {public:void execute() override { std::cout Executing Kick Leg Move std::endl; }
};3.3.定义独立工厂函数
// 独立工厂函数
std::shared_ptrDanceMove createDanceMove(const std::string type) {if (type RaiseHand) {return std::make_sharedRaiseHand();} else if (type TurnAround) {return std::make_sharedTurnAround();} else if (type KickLeg) {return std::make_sharedKickLeg();} else {return nullptr;}
}3.4.实现享元工厂类
// 享元工厂
class DanceMoveFactory {public:// 获取舞蹈动作std::shared_ptrDanceMove getDanceMove(const std::string type) {auto it danceMoveMap_.find(type);if (it ! danceMoveMap_.end()) {return it-second;} else {// 创建动作std::shared_ptrDanceMove danceMove createDanceMove(type);if (danceMove) {danceMoveMap_[type] danceMove;return danceMove;} else {throw std::runtime_error(Dance move type not recognized: type);}}}private:std::unordered_mapstd::string, std::shared_ptrDanceMove danceMoveMap_;
};3.5.客户端代码
// 客户端代码
int main() {DanceMoveFactory factory;try {// 获取并执行舞蹈动作auto raiseHand factory.getDanceMove(RaiseHand);raiseHand-execute();auto turnAround factory.getDanceMove(TurnAround);turnAround-execute();auto kickLeg factory.getDanceMove(KickLeg);kickLeg-execute();// 再次获取舞蹈动作验证是否共享了相同类型的动作对象auto raiseHand2 factory.getDanceMove(RaiseHand);raiseHand2-execute();std::cout raiseHand address: raiseHand.get() std::endl;std::cout raiseHand2 address: raiseHand2.get() std::endl;// 尝试获取未注册的舞蹈动作auto unknownMove factory.getDanceMove(UnknownMove);unknownMove-execute();} catch (const std::exception e) {std::cerr Error: e.what() std::endl;}return 0;
}