唐山建设集团下岗职工网站,做网站的软件 知乎,公司搜索seo,网站建设高级教程享元模式#xff0c;个人理解#xff0c;就是应用共享技术来减少类的对象创建#xff0c;节省计算机资源消耗#xff0c;而且能够减少维护成本
#include iostream
#include string
#include vectorusing namespace std;class Flyweight {
public:…享元模式个人理解就是应用共享技术来减少类的对象创建节省计算机资源消耗而且能够减少维护成本
#include iostream
#include string
#include vectorusing namespace std;class Flyweight {
public:Flyweight(string state):_state(state) {}virtual void Operation(const stringstate) { }string GetState()const { return _state; }virtual ~Flyweight() { }
private:string _state;
};class ConcreteFlyweight :public Flyweight {
public:ConcreteFlyweight(string state):Flyweight(state) {cout ConcreteFlyweight Build... state endl;}void Operation(const string state) {cout ConcreteFlyweight GetState() \\ state endl;}
};class FlyweightFactory {
public:Flyweight *GetFlyweight(std::string key) {for (auto fly : _flys) {if (fly-GetState() key) {cout already created by users... endl;return fly;}}Flyweight *fn new ConcreteFlyweight(key);_flys.push_back(fn);return fn;}
private:std::vectorFlyweight* _flys;
};int main() {FlyweightFactory *fc new FlyweightFactory();Flyweight *fw1 fc-GetFlyweight(hello);Flyweight *fw2 fc-GetFlyweight(world);Flyweight *fw3 fc-GetFlyweight(hello);delete fw1;delete fw2;//delete fw3;delete fc;return 0;
}