C++设计模式-享元模式
生活随笔
收集整理的這篇文章主要介紹了
C++设计模式-享元模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
基本概念
代碼與實例
?
基本概念
享元模式(Flyweight):運用共享技術有效的支持大量細粒度的對象。
UML圖如下(此圖來源于大話設計模式)
享元模式可以避免大量非常相似類的開銷。在程序設計中,有時需要生成大量細粒度的類實例來表示數據。如果能發現這些實例除了幾個參數外基本上都是相同的,有時就能受大幅度的減少需要實例化的數量。如果能把哪些參數移動到類實例外面,在方法調用是將他們傳遞進來,就可以通過共享大幅度的減少單個實例的數量。
如果一個應用程序使用了大量的對象,而大量的這些對象造成了很大的存儲開銷時就可以考慮使用;對象的大多數狀態可以外部狀態,如果刪除對象的外部狀態,那么可以用相對較少的共享對象取代很多組對象。
?
?
代碼與實例
程序運行截圖如下:
源碼如下:
Head.h
#ifndef HEAD_H #define HEAD_H#include <iostream> #include <cstring> #include <map> #include <algorithm> using namespace std;class Flyweight;typedef pair<char, Flyweight*> in_pair; typedef pair<map<char, Flyweight*>::iterator, bool> in_pair_bool;//Flyweight類,它是所有具體享元類的超類或接口,通過這個接口,Flyweight可以接收并作用于外部狀態 class Flyweight{public:virtual void operation(const int &extrinsicstate);virtual ~Flyweight(); };//ConcreteFlyweight是繼承Flyweight超類或實現Flyweight接口,并為內部狀態增加存儲空間 class ConcreteFlyweight: public Flyweight{public:ConcreteFlyweight();void operation(const int &extrinsicstate);~ConcreteFlyweight(); };//UnsharedConcreteFlyweight是指那些不需要共享的Flyweight子類。因為Flyweight接口共享成為可能,但它并不強制共享 class UnsharedConcreteFlyweight: public Flyweight{public:void operation(const int &extrinsicstate);~UnsharedConcreteFlyweight(); };//FlyweightFactory是一個享元工廠,用來創建并且管理Flyweight對象。用來確保合理的共享Flyweight //當用戶請求一個Flyweight時,FlyweightFacotry對象提供一個已創建的實例或者創建一個(如果不存在的話) class FlyweightFacory{public:FlyweightFacory();~FlyweightFacory();Flyweight* getFlyweight(char c);protected:void insertOk(in_pair_bool pr);friend void deleteMapNode(in_pair pr);private:map<char, Flyweight*> m_flyweights; };#endif //HEAD_HHead.cpp
#include "Head.h"void Flyweight::operation(const int &extrinsicstate) {}Flyweight::~Flyweight() {}ConcreteFlyweight::ConcreteFlyweight() {}void ConcreteFlyweight::operation(const int &extrinsicstate) {cout << "具體Flyweight:" << extrinsicstate << endl; }ConcreteFlyweight::~ConcreteFlyweight() {cout << "ConcreteFlyweight::~ConcreteFlyweight()" << endl; }void UnsharedConcreteFlyweight::operation(const int &extrinsicstate) {cout << "不共享的具體類Flyweight:" << extrinsicstate << endl; }UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight() {cout << "UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()" << endl; }FlyweightFacory::FlyweightFacory() {insertOk(m_flyweights.insert(in_pair('x', new ConcreteFlyweight)));insertOk(m_flyweights.insert(in_pair('y', new ConcreteFlyweight)));insertOk(m_flyweights.insert(in_pair('z', new ConcreteFlyweight))); }FlyweightFacory::~FlyweightFacory() {cout << "FlyweightFacory::~FlyweightFacory()" << endl;for_each(m_flyweights.begin(), m_flyweights.end(), deleteMapNode);m_flyweights.clear(); }Flyweight* FlyweightFacory::getFlyweight(char c) {map<char, Flyweight*>::iterator i = m_flyweights.find(c);return i->second; }void FlyweightFacory::insertOk(in_pair_bool pr) {if(pr.second){cout << "insert ok!" << endl;}else{cout << "insert failed!" << endl;} }void deleteMapNode(in_pair pr) {delete pr.second; }main.cpp
#include "Head.h"int main(int *argc, int *argv[]){int extrinsicstate = 22;FlyweightFacory *f = new FlyweightFacory;Flyweight *fx = f->getFlyweight('x');fx->operation(--extrinsicstate);Flyweight *fy = f->getFlyweight('y');fy->operation(--extrinsicstate);Flyweight *fz = f->getFlyweight('z');fz->operation(--extrinsicstate);Flyweight *uf = new UnsharedConcreteFlyweight;uf->operation(--extrinsicstate);delete f;getchar();return 0; }實際上FlyweightFactory不用在構造函數里面,可以根據需要在進行添加等!
總結
以上是生活随笔為你收集整理的C++设计模式-享元模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt实践| HTTP知识点-Qt填充re
- 下一篇: Arduino笔记-温度传感器的使用