淘宝网站模板是什么做的,竞价广告推广,网站开发分哪几个步骤,百度h5制作软件下载一#xff0c;享元模式简介 享元模式是一种结构型设计模式#xff0c;它将每个对象中各自保存一份数据的方式改为多个对象共享同一份数据#xff0c;该模式可以有效减少应用程序的内存占用。 享元模式的核心思想是共享和复用#xff0c;通过设置共享资源来避免创建过多的实…一享元模式简介 享元模式是一种结构型设计模式它将每个对象中各自保存一份数据的方式改为多个对象共享同一份数据该模式可以有效减少应用程序的内存占用。 享元模式的核心思想是共享和复用通过设置共享资源来避免创建过多的实例。 当应用程序的内部包含大量的对象且对象之间包含相似的数据或状态时可以使用享元模式来共享这些数据或状态。 享元模式的内部涉及到工厂模式的使用因为它需要创建一个享元工厂来管理共享资源池。这个共享资源池又称为享元池(Flyweight Pool)这里面包含多个访问共享数据的享元对象。当客户端需要使用一个享元对象时享元工厂会从池中获取一个已有的享元对象如果对象不存在则创建一个新的享元对象。 二享元模式的结构 1.内部状态(Intrinsic State)对象之间容易重复的、可以共享的、且变动很少的成员变量该变量在享元模式中被共享。 2.外部状态(Extrinsic State)对象之间各不相同的、不能共享的、且随着不同场景而变化的成员变量该变量被调用的客户端所设置和更改。 3.享元工厂类(Flyweight Factory)替外部客户端管理共享资源的类。 4.抽象享元类(Flyweight)享元模式的核心由享元工厂进行创建和管理里面包含了内部状态但不包含外部状态。 5.共享的具体享元类(Concrete Flyweight)实现了Flyweight声明的接口并访问和存储了内部状态。 对应UML类图 代码实现
#include iostream
#include memory
#includeunordered_mapusing namespace std;class Flyweight {
protected:int id; //内部状态public:Flyweight(int id) : id(id) {}virtual void operation() const 0;
};class ConcreteFlyweightA : public Flyweight {
public:ConcreteFlyweightA() : Flyweight(1) {}void operation() const override {cout Concrete Flyweight A, id: id endl;}
};class ConcreteFlyweightB : public Flyweight {
public:ConcreteFlyweightB() : Flyweight(2) {}void operation() const override {cout Concrete Flyweight B, id: id endl;}
};// 定义享元工厂
class FlyweightFactory {
private:std::unordered_mapint, shared_ptrFlyweight flyweights;public:FlyweightFactory() {}//返回享元对象std::shared_ptrFlyweight getConcreteFlyweight(int id) {if (flyweights.find(id) flyweights.end()) {if (id % 2 0) {flyweights[id] make_sharedConcreteFlyweightA();}else {flyweights[id] make_sharedConcreteFlyweightB();}}return flyweights[id];}
};int main() {FlyweightFactory factory;shared_ptrFlyweight f1 factory.getConcreteFlyweight(1);shared_ptrFlyweight f2 factory.getConcreteFlyweight(2);shared_ptrFlyweight f3 factory.getConcreteFlyweight(3);f1-operation();f2-operation();f3-operation();return 0;
}
运行结果
Concrete Flyweight B, id: 2
Concrete Flyweight A, id: 1
Concrete Flyweight B, id: 2
三享元模式的工作步骤 1.拆分类的成员变量将成员变量拆分成以下两种不变的、可能在对象之间重复使用的。变化的、随着应用场景而改动的。 2.将不变的可重复的成员变量的属性设置为不可修改并在构造函数中赋初始值。 3.创建享元类并将共享的成员变量集成到享元类。 4.创建享元工厂类来管理共享的资源池客户端与享元对象的交互借助享元工厂来实现。 5.优化共享资源池的代码实现这可能涉及到事件驱动、回调函数或者策略模式的应用。 四享元模式的应用场景 图形或图像处理在大型游戏或图形编辑器开发中同一个形状如矩形或颜色等状态会重复出现很多次基于享元模式可以降低内存开销。 数据库处理优化数据库被频繁地连接和请求时享元模式可以管理这些连接并复用它们提高处理的性能。 UI组件开发在用户界面中当创建多个界面窗口时像按钮、图标等小部件会在创建界面窗口时有大量重复使用享元模式可以减少界面之间重复组件的数量提高性能。 五享元模式的优缺点 享元模式的优点 1.增加了系统资源的可重用性节省了系统资源。 2.基于共享的结构降低了内存消耗。 3.系统可扩展性强新增对象时可直接复用共享资源。 4.降低了对象内部的结构复杂性。 享元模式的缺点 1.使代码结构更加复杂。 2.当需要被共享的资源量级很小时该模式的性能提升并不显著。 3.将共享变量放在构造函数中进行赋值额外增加了初始化的时间。 4.引入了共享这种结构会导致潜在的线程安全问题。 5.编写代码需要考虑保证状态的同步和一致性问题否则会导致bug的产生。 六代码实战 Demo模拟字符编辑器 #include iostream
#include mapusing namespace std;//Flyweight
class Character {
public:Character(char symbol) : symbol_(symbol) {}Character() default;void print() const {cout Character: symbol_ endl;}private:char symbol_;
};//Flyweight factory
class CharacterFactory {
public:static const Character getCharacter(char symbol) {if (characters_.find(symbol) characters_.end()) {characters_[symbol] Character(symbol);cout Create new char. endl;}return characters_[symbol];}private:static mapchar, Character characters_;
};mapchar, Character CharacterFactory::characters_;int main() {const Character A CharacterFactory::getCharacter(A);const Character B CharacterFactory::getCharacter(B);//Reusing Aconst Character A2 CharacterFactory::getCharacter(A);A.print();B.print();A2.print();return 0;
}
运行结果
Create new char.
Create new char.
Character: A
Character: B
Character: A
七参考阅读 https://softwarepatterns.com/cpp/flyweight-software-pattern-cpp-example https://www.pentalog.com/blog/design-patterns/flyweight-design-patterns/ https://design-patterns.readthedocs.io/zh-cn/latest/structural_patterns/flyweight.html