以下代码来源: 设计模式精解-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(结构型模式) 针对 需要创建大量对象的情形,被共享的状态作为内部状态,不被共享的状态作为外部状态的更多相关文章

  1. 设计模式-Facade(结构型模式) 针对 最终类的实现通过一系列类的相关操作,重点关注 起始与结尾的操作。

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Facade.h #pragma once class Subsystem1 { public: Subsystem1() ...

  2. GoF的23种设计模式之结构型模式的特点和分类

    结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者釆用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关系耦合度低,满足 ...

  3. Go语言实现的23种设计模式之结构型模式

    摘要:本文主要聚焦在结构型模式(Structural Pattern)上,其主要思想是将多个对象组装成较大的结构,并同时保持结构的灵活和高效,从程序的结构上解决模块之间的耦合问题. 本文分享自华为云社 ...

  4. Java设计模式之结构型模式

    结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式. 一.适配器模式: 意图: 将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接 ...

  5. Java经典23种设计模式之结构型模式(一)

    结构型模式包含7种:适配器模式.桥接模式.组合模式.装饰模式.外观模式.享元模式.代理模式. 本文主要介绍适配器模式和桥接模式. 一.适配器模式(Adapter) 适配器模式事实上非常easy.就像手 ...

  6. GoF23种设计模式之结构型模式之享元模式

    一.概述  运用共享技术有效地支持大量细粒度的对象. 二.适用性 1.当一个应用程序使用了大量的对象的时候. 2.由于使用大量的独享而造成很大的存储开销的时候. 3.对象的大多数状态都可变为外部状态的 ...

  7. GoF23种设计模式之结构型模式之组合模式

    一.概述 将对象组合成树型结构以表示“部分--整体”的层次关系.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.适用性 1.你想表示对象的部分--整体层次结构的时候. 2.你希望用户忽略组 ...

  8. GoF23种设计模式之结构型模式之桥接模式

    一.概述         将类的抽象部分与实现分部分离开来,使它们都可以独立地变化. 二.适用性 1.你不希望在抽象和实现之间有一个固定的绑定关系的时候.例如:在程序运行时实现部分应可以被选择或切换. ...

  9. GoF23种设计模式之结构型模式之适配器模式

    一.概述         将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 二.适用性 1.你想使用一个已经存在的类,但是它的接口不符合 ...

随机推荐

  1. EFCore某张表中获取某几个字段

    目录 EFCore某张表中获取某几个字段 1.背景 2.法一:linq 2.1 使用Select方法 2.2 使用ForEach方法 2.3 其他参考代码 3.法二:IQueryble 3.1 参考例 ...

  2. Attach Files to Objects 将文件附加到对象

    In this lesson, you will learn how to attach file collections to business objects. For this purpose, ...

  3. Java中"或"运算与"与"运算快慢的三三两两

    先上结论 模运算比与运算慢20%到30% 这是通过实验的方式得到的结论.因为没有大大可以进行明确指导,所以我以最终运行的结果为准.欢迎指正. 测试代码 @Test public void test10 ...

  4. vue常见问题处理 -- 页面刷新时,如何保持原有vuex中的state信息

    一.页面刷新时,如何保持原有vuex中的state信息 页面刷新后,原有的 vuex 中的 state 会发生改变,如果在页面刷新之前,可以将 state 信息保存,页面重新加载时,再将该值赋给 st ...

  5. nodejs环境使用Typeorm连接查询Oracle

    首先是typeorm的官方地址, 国内有人翻了中文版,不保证时效性 ·通过npm安装下列包: typeorm //typeorm连接数据库 @types/node //类型系统 typescript ...

  6. Owhat sign参数分析

    需求:Owath进行商品购买时,psot提交的参数,有个sign,分析生成的算法. 1)点击商品购买后,进行抓包. 2)搜索sign定位赋值函数,OWAPIParamsDict paramsDictW ...

  7. dedecmsV5.7 百度编辑器ueditor 多图上传 在线管理 排序问题

    问题:dedecms后台百度编辑器ueditor的多图上传-在线管理的图片排序有问题,想把这个顺序调成按照文件修改时间倒序来展示 解决方法: 1.打开/include/ueditor/php/acit ...

  8. 调试seanbell/intrinsic遇到的坑

    那些遗忘过去的人注定要重蹈覆辙.——乔治•桑塔亚纳  Authorized error 刚开始按作者 GitHub 上的指示,当运行环境配置好,并且 make 之后,因为生成的 decompose.p ...

  9. 线程池API总结

    1.Executor:线程池顶级接口,只有一个方法 2.ExecutorService:真正的线程池接口 1) void execute(Runnable command) :执行任务/命令,没有返回 ...

  10. JeeSite | 保存信息修改记录封装

    前面写过两篇关于“保存信息修改记录”的内容,分别如下: JeeSite | 保存信息修改记录 JeeSite | 保存信息修改记录续 回顾         第一篇文章通过类字段的比较返回一个有字段值不 ...