结构型模式-享元(flyweight)
来源:互联网 发布:中国当下的网络环境 编辑:程序博客网 时间:2024/04/27 14:20
享元
运用共享技术有效地支持大量细粒度的对象.
实例
main.cc:
#include "clothes_factory.h"#include "leading_runner_clothes.h"#include <stdio.h>using namespace std;/*design_pattern:"flyweight"Each runner club has its own clothes. Each piece of clothing for an object, then when the group ran to 100 thousand people, there are 100 thousand objects,the flyweight pattern can only save a lot of memory resources.The leading runner of the clothes have their own names, ordinary members of the clothes only the size difference.*/int main(){ ClothesFactory *clothes_factory = new ClothesFactory(); clothes_factory->GetClothes('S'); clothes_factory->GetClothes('M'); clothes_factory->GetClothes('L'); Clothes *clothes_first = clothes_factory->GetClothes('X'); Clothes *clothes_second = clothes_factory->GetClothes('X'); Clothes *leading_clothes_first = new LeadingRunnerClothes(); Clothes *leading_clothes_second = new LeadingRunnerClothes(); //check printf("clothes_first=%p,clothes_second=%p\n", clothes_first, clothes_second); printf("leading_clothes_first=%p,leading_clothes_second=%p\n",leading_clothes_first,leading_clothes_second); //clear delete clothes_factory; delete leading_clothes_first; delete leading_clothes_second; system("Pause"); return 0;}
ClothesFactory:
//clothes_factory.h#ifndef HELENDP_SOURCE_CLOTHES_FACTORY_H_#define HELENDP_SOURCE_CLOTHES_FACTORY_H_#include "clothes.h"#include <map>using namespace std;class ClothesFactory{public: ClothesFactory(); ~ClothesFactory(); Clothes *GetClothes(int key);private: map<int,Clothes*> map_clothes_;};#endif//clothes_factory.cc#include "clothes_factory.h"#include "runner_clothes.h"#include <iostream>using namespace std;ClothesFactory::ClothesFactory(){}ClothesFactory::~ClothesFactory(){ map<int,Clothes*>::iterator iterator; iterator = map_clothes_.begin(); while(iterator != map_clothes_.end()){ delete iterator->second; printf("%s:[%c] delete!\n",__func__,iterator->first); iterator++; }}Clothes *ClothesFactory::GetClothes(int key){ map<int,Clothes*>::iterator iterator = map_clothes_.find(key); if(iterator == map_clothes_.end()){ Clothes *clothes = new RunnerClothes(); map_clothes_.insert(make_pair(key,clothes)); cout << "key (" << key << ")" << " is not exist,insert." << endl; return clothes; } else{ cout << "key (" << key << ")" << " is exist,return object." << endl; return iterator->second; }}
Clothes:
//clothes.h#ifndef HELENDP_SOURCE_CLOTHES_H_#define HELENDP_SOURCE_CLOTHES_H_class Clothes{public: Clothes(); virtual ~Clothes();};#endif//clothes.cc#include "clothes.h"Clothes::Clothes(){}Clothes::~Clothes(){}
RunnerClothes:
//runner_clothes.h#ifndef HELENDP_SOURCE_RUNNER_CLOTHES_H_#define HELENDP_SOURCE_RUNNER_CLOTHES_H_#include "clothes.h"class RunnerClothes : public Clothes{public: RunnerClothes(); ~RunnerClothes();};#endif//runner_clothes.cc#include "runner_clothes.h"RunnerClothes::RunnerClothes(){}RunnerClothes::~RunnerClothes(){}
LeadingRunnerClothes:
//leading_runner_clothes.h#ifndef HELENDP_SOURCE_LEADING_RUNNER_CLOTHES_H_#define HELENDP_SOURCE_LEADING_RUNNER_CLOTHES_H_#include "clothes.h"class LeadingRunnerClothes : public Clothes{public: LeadingRunnerClothes(); ~LeadingRunnerClothes();};#endif//leading_runner_clothes.cc#include "leading_runner_clothes.h"LeadingRunnerClothes::LeadingRunnerClothes(){}LeadingRunnerClothes::~LeadingRunnerClothes(){}
代码和UML图(EA)工程文件,最后会整理打包上传.
UML类图
结构
- FlyweightFactory(ClothesFactory):创建并管理flyweight对象.
- Flyweight(Clothes):抽象享元类.
- ConcreteFlyweight(RunnerClothes):具体享元类.
- UnsharedConcreteFlyweight(LeadingRunnerClothes):非共享具体享元类.
优点
- 享元模式的优点在于它可以极大减少内存中对象的数量,使得相同对象或相似对象在内存中只保存一份.
- 享元模式的外部状态相对独立,而且不会影响其内部状态,从而使得享元对象可以在不同的环境中被共享.
缺点
- 享元模式使得系统更加复杂,需要分离出内部状态和外部状态,这使得程序的逻辑复杂化.
- 为了使对象可以共享,享元模式需要将享元对象的状态外部化,而读取外部状态使得运行时间变长.
0 0
- 结构型模式-Flyweight ( 享元模式 )
- 结构型模式:Flyweight(享元)
- 【结构型模式】flyweight(享元)
- 结构型模式之享元FLYWEIGHT
- 结构型模式-享元(flyweight)
- 享元模式Flyweight(结构型)
- 结构型-享元模式(Flyweight)
- 设计模式--结构型模式之六-Flyweight享元
- 设计模式(11)-结构型-享元模式(Flyweight)
- 结构型模式之享元模式(Flyweight)
- [设计模式-结构型]享元模式(Flyweight )
- 结构型模式-----享元模式(Flyweight)
- 【设计模式基础】结构型模式 - 6 - 享元(Flyweight)
- 设计模式--享元模式FlyWeight(结构型)
- 结构型设计模式---享元模式(Flyweight)
- 结构型模式之享元模式(FlyWeight)
- 设计模式(结构型)之享元模式(Flyweight Pattern)
- 享元模式Flyweight——结构型模式
- ASN1之DER编码规则
- 练习系统 实验四 八皇后(枚举法)
- Java8 函数式编程
- java利用url实现网页内容的抓取
- 【Codeforces Gym 100187F 】+ 贪心
- 结构型模式-享元(flyweight)
- magento创建后台用户sql 1.9.2.4+
- 网游实时对战同步技术三篇
- python操作mysql数据库
- ZOJ 3768 Continuous Login
- zoj1128 Atlantis(离散)
- 工作略微充实
- 日常小记:以checkbox实现类似Radiobutton的互斥选项
- dubbo使用教程(可直接应用于企业开发)