title: "modern C++ DesignPattern-Part3"
date: 2018-04-12T19:08:49+08:00
lastmod: 2018-04-12T19:08:49+08:00
keywords: [设计模式, C++]
tags: [设计模式]
categories: []

结构式设计模式的最后两个,享元模式:实现对象共享,减少内存开销;代理模式,提供相同接口的代理

flyweight

享元模式的主要目的是实现对象的共享,即共享池,当系统中对象多的时候可以减少内存的开销,通常与工厂模式一起使用。boost::flyweight使用:

struct User2
{
flyweight<string> first_name, last_name; //类似一个缓存 User2(const string &first_name, const string &last_name)
: first_name(first_name),
last_name(last_name) {}
}; void boost_flyweight()
{
User2 user1{"John", "Smith"};
User2 user2{"Jane", "Smith"};
cout << user1.first_name << endl;
cout << boolalpha //std::boolalpha可以把bool变成true/false字符串
<< (&user1.first_name.get() == &user2.first_name.get()) << endl;
cout << boolalpha
<< (&user1.last_name.get() == &user2.last_name.get()) << endl;
}

示例:

class FormattedText {  //用来记录是否被复用的单元
string plain_text;
bool *caps;
public:
explicit FormattedText(const string &plainText)
: plain_text{plainText} {
caps = new bool[plainText.length()];
memset(caps, 0, plain_text.length());
} ~FormattedText() {
delete[] caps;
} void capitalize(int start, int end) {
for (int i = start; i <= end; ++i)
caps[i] = true;
} friend std::ostream &operator<<(std::ostream &os, const FormattedText &obj) {
string s;
for (int i = 0; i < obj.plain_text.length(); ++i) {
char c = obj.plain_text[i];
s += (obj.caps[i] ? toupper(c) : c);
}
return os << s;
}
}; class BetterFormattedText {
public:
struct TextRange {
int start, end;
bool capitalize, bold, italic; bool covers(int position) const {
return position >= start && position <= end;
}
}; TextRange &get_range(int start, int end) { //每当设置一个被复用的单元,push到vector里面,返回最后一个
formatting.emplace_back(TextRange{start, end});
return *formatting.rbegin();
} explicit BetterFormattedText(const string &plainText)
: plain_text{plainText} {
} friend std::ostream &operator<<(std::ostream &os, const BetterFormattedText &obj) {
string s;
for (size_t i = 0; i < obj.plain_text.length(); i++) {
auto c = obj.plain_text[i];
for (const auto &rng : obj.formatting) {
if (rng.covers(i) && rng.capitalize)
c = toupper(c);
}
s += c; // fixed!
}
return os << s;
} private:
string plain_text;
vector<TextRange> formatting;
};

Proxy

代理模式最经典的是shared_ptr实现,增加了引用计数的同时保持了原有的普通指针接口

Property Proxy

template <typename T> struct Property {
T value;
Property(const T initialValue) {
*this = initialValue;
}
operator T() { //getter
return value;
}
T operator =(T newValue) { //setter
return value = newValue;
}
};
//usage
struct Creature
{
Property<int> strength{ 10 };
Property<int> agility{ 5 };
};
void property_proxy() {
Creature creature;
creature.agility = 20;
cout << creature.agility << endl;
}

主要目的是可以允许 Property<int>与int的随时转换。

Virtual Proxy

这种非常常见,使用时分两种,eager模式和lazy模式,区别就是lazy模式是在使用的时候才会初始化

struct Image{
virtual ~Image() = default;
virtual void draw() = 0;
};
struct Bitmap : Image{
Bitmap(const string& filename) {
cout << "Loading image from " << filename << endl;
}
void draw() override {
cout << "Drawing image" << endl;
}
};
struct LazyBitmap : Image {
LazyBitmap(const string& filename): filename(filename) {}
~LazyBitmap() { delete bmp; }
void draw() override {
if (!bmp)
bmp = new Bitmap(filename);
bmp->draw();
}
private:
Bitmap* bmp{nullptr};
string filename;
}; void draw_image(Image& img) {
img.draw();
}
void virtual_proxy() {
LazyBitmap img{ "pokemon.png" };
draw_image(img); // loaded whether the bitmap is loaded or not
draw_image(img);
}

DesignPattern-part3的更多相关文章

  1. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 5.安装Database软件 5. ...

  2. Linux平台 Oracle 11gR2 RAC安装Part3:DB安装

    四.DB(Database)安装 4.1 解压DB的安装包 4.2 DB软件安装 4.3 ASMCA创建磁盘组 4.4 DBCA建库 4.5 验证crsctl的状态 Linux平台 Oracle 11 ...

  3. Hadoop入门学习笔记---part3

    2015年元旦,好好学习,天天向上.良好的开端是成功的一半,任何学习都不能中断,只有坚持才会出结果.继续学习Hadoop.冰冻三尺,非一日之寒! 经过Hadoop的伪分布集群环境的搭建,基本对Hado ...

  4. 小课堂Week10 例外处理设计的逆袭Part3

    小课堂Week10 例外处理设计的逆袭Part3 今天是<例外处理设计的逆袭>这本书阅读的第三天,也是最后一天,我们会主要通过实例,对Part2中提出的例外处理等级进行解读. Level1 ...

  5. Linux平台 Oracle 12cR2 RAC安装Part3:DB安装

    Linux平台 Oracle 12cR2 RAC安装Part3:DB安装 四.DB(Database)安装 4.1 解压DB的安装包 4.2 DB软件安装 4.3 ASMCA创建磁盘组 4.4 DBC ...

  6. Python学习 Part3:数据结构

    Python学习 Part3:数据结构 1. 深入列表: 所有的列表对象方法 list.append(x): 在列表的末尾添加一个元素 list.extend(L): 在列表的末尾添加一个指定列表的所 ...

  7. Linux平台 Oracle 18c RAC安装Part3:DB配置

    四.DB(Database)配置 4.1 解压DB的安装包 4.2 DB软件配置 4.3 ASMCA创建磁盘组 4.4 DBCA建库 4.5 验证crsctl的状态 Linux平台 Oracle 18 ...

  8. java 设计模式http://www.runoob.com/design-pattern/design-pattern-tutorial.html

    参考:http://www.runoob.com/design-pattern/design-pattern-tutorial.html

  9. 运放参数的详细解释和分析-part3,输入失调电压Vos及温漂

    运放参数的详细解释和分析-part3,输入失调电压Vos及温漂 在运放的应用中,不可避免的会碰到运放的输入失调电压Vos问题,尤其对直流信号进行放大时,由于输入失调电压Vos的存在,放大电路的输出端总 ...

  10. 结对作业——四则运算 Part3. 对于结对编程的总结与思考

    结对作业——四则运算 Part3. 对于结对编程的总结与思考 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 Part 1. Core代码编写部分 Part ...

随机推荐

  1. 1 msql的安装和配置

    1.检测系统是否已经安装过mysql或其依赖,若已装过要先将其删除,否则第4步使用yum安装时会报错: yum list installed | grep mysql mysql-libs.i686 ...

  2. 一次 HPC 病毒感染与解决经历

    周一的时候,有同事反馈说,HPC 的项目报告路径正在不断产生 *.exe 和 *.pif 文件,怀疑是不是被病毒感染! 收到信息,第一时间进去目录,的确发现该目录每个几秒钟就自动生成一个 *.exe ...

  3. Nginx SSL 双向认证,key 生成和配置

    一.安装Nginx和OpenSSL yum install nginx openssl -y 二.SSL 服务器 / 客户端双向验证证书的生成 创建一个新的 CA 根证书,在 nginx 安装目录下新 ...

  4. 【python基础】类-模块

    随着不断给类添加功能,文件可能变得很长,即便妥善地使用了继承亦是如此,为遵循Python的总体理念,应让文件尽可能简洁.为在这方面提供帮助,Python允许将类存储在模块中,然后在主程序中导入所需的模 ...

  5. Apple、AWS 这些科技巨头,已悄然入局隐私计算

    随着数字化时代的到来,数据已经成为企业竞争的重要资源.然而,与此同时,数据隐私泄露的风险也在不断增加,这已经成为了公共安全和个人权利保护的重要问题.为了解决这个问题,科技巨头谷歌.苹果.亚马逊纷纷入局 ...

  6. .NET Core 允许跨域的两种方式实现(IIS 配置、C# 代码实现)

    〇.前言 当把开发好的 WebApi 接口,部署到 Windows 服务器 IIS 后,postman 可以直接访问到接口并正确返回,这并不意味着任务完成,毕竟接口嘛是要有交互的,最常见的问题莫过于跨 ...

  7. 了解O2OA(翱途)开发平台中的VIP应用

    使用O2OA(翱途)开发平台可以非常方便地进行项目的业务需求开发与实施,O2OA(翱途)开发平台并不限制实现的系统类型,所以能实现的系统很多,最终呈现的项目成果也是多样性的,可能是OA系统,可能是人力 ...

  8. opensbi入门

    OpenSBI 入门 声明 本文为本人原创,未经允许,严禁转载. FW_JUMP FW_PAYLOAD FW_DYNAMIC FW_JUMP OpenSBI 带跳转地址的固件(FW_JUMP)是一种仅 ...

  9. 【ElasticSearch】大数据量情况下的前缀、中缀实时搜索方案

    简述 业务开发中经常会遇到这样一种情况,用户在搜索框输入时要实时展示搜索相关的结果.要实现这个场景常用的方案有Completion Suggester.search_as_you_type.那么这两种 ...

  10. snmptt解析中文trap消息

    项目中使用了中国电信系统集成公司的虚拟化平台,为通过zabbix监控,接收HyperCenter发送的告警,需要将trap消息中的汉语编码转译.网络上snmptt资料不多,官网文档也不甚友好,通过参考 ...