设计模式-FlyWeight(结构型模式) 针对 需要创建大量对象的情形,被共享的状态作为内部状态,不被共享的状态作为外部状态
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码
//Flyweight.h
#pragma once
#include<string>
class FlyWeight
{
public:
virtual ~FlyWeight();
virtual void Operation(const std::string & extrinsicState);
std::string GetIntrinsicState();
protected:
FlyWeight(std::string intrinsicState);
private:
std::string _intrinsicState;
}; class ConcreateFlyWeight :public FlyWeight
{
public:
ConcreateFlyWeight(std::string intrinsicState);
~ConcreateFlyWeight();
void Operation(const std::string& extrinsicState);
protected:
private:
};
//Flyweight.cpp
#include"Flyweight.h"
#include<iostream> FlyWeight::FlyWeight(std::string intrinsicState)
{
this->_intrinsicState = intrinsicState;
}
FlyWeight::~FlyWeight(){}
void FlyWeight::Operation(const std::string& extrinsicState)
{ }
std::string FlyWeight::GetIntrinsicState()
{
return this->_intrinsicState;
}
ConcreateFlyWeight::ConcreateFlyWeight(std::string intrinsicState) :FlyWeight(intrinsicState)
{
std::cout << "ConcreateFlyWeight Build..." << intrinsicState << std::endl;
}
ConcreateFlyWeight::~ConcreateFlyWeight(){}
void ConcreateFlyWeight::Operation(const std::string& extrinsicState)
{
std::cout << "ConcreateFlyWeight:内部[" << this->GetIntrinsicState() << "]外部[" << extrinsicState << "]" << std::endl;
}
//FlyWeightFactory.h
#pragma once #include"Flyweight.h"
#include<vector>
#include<string> class FlyWeightFactory
{
public:
FlyWeightFactory();
~FlyWeightFactory();
FlyWeight* GetFlyWeight(const std::string& key);
protected:
private:
std::vector<FlyWeight*>_fly;
};
//FlyWeightFactory.cpp
#include"FlyWeightFactory.h"
#include<iostream>
#include<string> FlyWeightFactory::FlyWeightFactory(){}
FlyWeightFactory::~FlyWeightFactory(){}
FlyWeight* FlyWeightFactory::GetFlyWeight(const std::string& key)
{
for (std::vector<FlyWeight*>::iterator it = _fly.begin(); it != _fly.end(); ++it)
{
if ((*it)->GetIntrinsicState() == key)
{
std::cout << "Already created by users..." << std::endl;
return *it;
}
}
FlyWeight* fn = new ConcreateFlyWeight(key);
_fly.push_back(fn);
return fn;
}
//main.cpp
#include"Flyweight.h"
#include"FlyWeightFactory.h"
#include<string>
int main(int args, char* argv)
{
FlyWeightFactory* flyfac = new FlyWeightFactory();
FlyWeight* fw1 = flyfac->GetFlyWeight("Hello");
FlyWeight* fw2 = flyfac->GetFlyWeight("World");
FlyWeight* fw3 = flyfac->GetFlyWeight("Hello");
getchar();
return ; }
设计模式-FlyWeight(结构型模式) 针对 需要创建大量对象的情形,被共享的状态作为内部状态,不被共享的状态作为外部状态的更多相关文章
- 设计模式-Facade(结构型模式) 针对 最终类的实现通过一系列类的相关操作,重点关注 起始与结尾的操作。
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Facade.h #pragma once class Subsystem1 { public: Subsystem1() ...
- GoF的23种设计模式之结构型模式的特点和分类
结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者釆用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关系耦合度低,满足 ...
- Go语言实现的23种设计模式之结构型模式
摘要:本文主要聚焦在结构型模式(Structural Pattern)上,其主要思想是将多个对象组装成较大的结构,并同时保持结构的灵活和高效,从程序的结构上解决模块之间的耦合问题. 本文分享自华为云社 ...
- Java设计模式之结构型模式
结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式. 一.适配器模式: 意图: 将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接 ...
- Java经典23种设计模式之结构型模式(一)
结构型模式包含7种:适配器模式.桥接模式.组合模式.装饰模式.外观模式.享元模式.代理模式. 本文主要介绍适配器模式和桥接模式. 一.适配器模式(Adapter) 适配器模式事实上非常easy.就像手 ...
- GoF23种设计模式之结构型模式之享元模式
一.概述 运用共享技术有效地支持大量细粒度的对象. 二.适用性 1.当一个应用程序使用了大量的对象的时候. 2.由于使用大量的独享而造成很大的存储开销的时候. 3.对象的大多数状态都可变为外部状态的 ...
- GoF23种设计模式之结构型模式之组合模式
一.概述 将对象组合成树型结构以表示“部分--整体”的层次关系.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.适用性 1.你想表示对象的部分--整体层次结构的时候. 2.你希望用户忽略组 ...
- GoF23种设计模式之结构型模式之桥接模式
一.概述 将类的抽象部分与实现分部分离开来,使它们都可以独立地变化. 二.适用性 1.你不希望在抽象和实现之间有一个固定的绑定关系的时候.例如:在程序运行时实现部分应可以被选择或切换. ...
- GoF23种设计模式之结构型模式之适配器模式
一.概述 将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 二.适用性 1.你想使用一个已经存在的类,但是它的接口不符合 ...
随机推荐
- spring的事物管理配置
基于注解的事务管理器配置(AOP) 首先要引入AOP和TX的名称控件 <!-- 使用annotation定义事务 --> <tx:annotation-driven transact ...
- DSP开发程序相关问题总结
1. 定义Class总是出错,原来是这样的class SCM_DRV_API CSERCOS{}:后来改为class CSERCOS{}:就可以了. 类的一般定义格式如下: class < ...
- python判断字典中key是否存在
例:#生成一个字典d = {'title':'abc','age':18} if 'title' in d.keys(): print('存在')else: print('不存在') if 'titl ...
- mongo shell 通过返回信息定位错误点
有时候我们会通过mongo shell 运行一些脚本,去执行更新或运维需求.mongo shell 可执行的代码可以实现比较复杂的功能,代码也可以比较丰富.当执行报错时,如果可以快速定位到错误点,对解 ...
- Nginx:反向代理
与众不同的生活方式很累人呢,因为找不到借口 在上一章节中,我们以及了解到正向.反向代理.负载均衡和动静分离的基本概念,安装教程,而在本节中将会了解到在 本文要点: 1.理清概念 2.Linux ...
- 解决Android Screen Monitor在android8.0及以上系统报错:"E/Screenshot: Unsupported protocol: 2"
1.打开命令窗口,切换到 asm.jar 所在目录,执行 java -jar asm.jar,正常情况下打开后连接上设备会显示出画面 2.但是在android8.0以上系统该asm.jar包就无法正常 ...
- Java中的BufferedImage类、Image类、Graphics类
https://www.cnblogs.com/jpfss/p/11731812.html
- [译]Vulkan教程(18)命令buffers
[译]Vulkan教程(18)命令buffers Command buffers 命令buffer Commands in Vulkan, like drawing operations and me ...
- [译]Vulkan教程(13)图形管道基础之Shader模块
[译]Vulkan教程(13)图形管道基础之Shader模块 Shader modules Unlike earlier APIs, shader code in Vulkan has to be s ...
- Tomcat9+JDK 13报错Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program
Tomcat使用的是https://tomcat.apache.org/download-90.cgi Tomcat9 之前安装的JDK 13,有JAVA_HOME环境变量地址(C:\Program ...