设计模式-结构型-享元
来源:互联网 发布:淘宝网官网下载2017 编辑:程序博客网 时间:2024/06/01 23:46
#pragma once#ifndef FLYWEIGHT_H #define FLYWEIGHT_H #include <string> #include <list> typedef std::string STATE; class Flyweight { public: virtual ~Flyweight(){} STATE GetIntrinsicState(); virtual void Operation(STATE& ExtrinsicState) = 0; protected: Flyweight(const STATE& state) :m_State(state) { } private: STATE m_State; }; class FlyweightFactory { public: FlyweightFactory(){} ~FlyweightFactory(); Flyweight* GetFlyweight(const STATE& key); private: std::list<Flyweight*> m_listFlyweight; }; class ConcreateFlyweight : public Flyweight { public: ConcreateFlyweight(const STATE& state) : Flyweight(state) { } virtual ~ConcreateFlyweight(){} virtual void Operation(STATE& ExtrinsicState); }; #endif
#include "StdAfx.h"#include "flyweight_impl.h"#include <iostream> inline STATE Flyweight::GetIntrinsicState() { return m_State; } FlyweightFactory::~FlyweightFactory() { for (std::list<Flyweight*>::iterator iter = m_listFlyweight.begin();iter != m_listFlyweight.end(); ++iter) { delete (*iter);} m_listFlyweight.clear(); } Flyweight* FlyweightFactory::GetFlyweight(const STATE& key) {for (std::list<Flyweight*>::iterator iter = m_listFlyweight.begin();iter != m_listFlyweight.end(); ++iter) { if ((*iter)->GetIntrinsicState() == key) { std::cout << "The Flyweight:" << key << " already exits"<< std::endl; return (*iter); } } std::cout << "Creating a new Flyweight:" << key << std::endl; Flyweight* flyweight = new ConcreateFlyweight(key); m_listFlyweight.push_back(flyweight); return flyweight;} void ConcreateFlyweight::Operation(STATE& ExtrinsicState) { std::cout<<ExtrinsicState<<std::endl;}
// FlyWeight.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "flyweight_impl.h"#include <stdlib.h>//运用共享技术有效地支持大量细粒度的对象。int _tmain(int argc, _TCHAR* argv[]){FlyweightFactory flyweightfactory; flyweightfactory.GetFlyweight("hello"); flyweightfactory.GetFlyweight("world"); flyweightfactory.GetFlyweight("hello"); system("pause"); return 0;}
阅读全文
0 0
- 设计模式-结构型模式-享元
- 设计模式--结构型--享元模式
- 结构型设计模式-享元模式
- 结构型设计模式-享元模式
- 设计模式-结构型-享元
- (C++设计模式) ------享元模式-- 结构型模式
- 设计模式 - 结构型模式 - 享元模式
- 设计模式-结构型模式:享元模式
- 设计模式->结构型模式->享元模式
- 设计模式--结构型模式之六-Flyweight享元
- 设计模式笔记(12)---享元模式(结构型)
- 设计模式(11)-结构型-享元模式(Flyweight)
- [设计模式-结构型]享元模式(Flyweight )
- 【设计模式基础】结构型模式 - 6 - 享元(Flyweight)
- java设计模式(结构型)之享元模式
- 设计模式--享元模式FlyWeight(结构型)
- 结构型设计模式---享元模式(Flyweight)
- 设计模式(结构型)之享元模式(Flyweight Pattern)
- OPenCV3.2 调整图像对比度与亮度
- 点云地图投影-2
- (转)Cannot merge new index 65553 into a non-jumbo instruction
- Warning: fopen() [function.fopen]: Unable to find the wrapper “https” – did you forget to enable it
- 软件工程第三次实验
- 设计模式-结构型-享元
- 阿里云服务器linux环境部署node及npm(cnpm)
- ESRI_ArcGIS+InfoCAD_v6.5地理信息系统软件
- 搭建Docker Swarm集群实战(二)(服务发现使用consul)
- Markdown中插入数学公式的方法
- Java解析xml文件
- 第十章 定位 层级关系
- linux关闭死循环的fork子进程
- PostgreSQL小记