类写的不规范(应该屏蔽类的拷贝构造函数和运算符=)。少写点代码,缩短篇幅,重在理解。 实际中可不要这样做。

类比生活中的手机,pad等电源适配器。

简单来讲: 将原本  不匹配  的两者  变的匹配  并且  不影响  原有的逻辑结构

1、分类

  A、类适配器

  B、对象适配器

  C、接口适配器

2、具体

  A、类适配器 是通过继承(泛化)实现的。比如, 交流电220v转为5v

// 交流电
class ac220v
{
public:
int get_output()
{
return 220;
}
}; // 直流5V
class dc5v
{
public:
virtual int get_dc_5v() = 0;
}; class power_ada : public ac220v, public dc5v
{
public:
int get_dc_5v()
{
int aaa = ac220v::get_output();
return 5;
}
};
// 调用
void call_power_ada()
{
std::unique_ptr<dc5v> p5v(new power_ada);
if (!p5v)
cout << "\n\n 类适配器创建失败\n\n";
else
cout << p5v->get_dc_5v();
}

  B、 对象适配器     器是通过组合来实现适配器功能的,即适配器拥有源角色的实例

class data_operation
{
public:
void set_password(const string str) { this->str_password = str; }
string get_password() { return this->str_password; } virtual string do_encrypt(const int key, const string str) = 0; private:
string str_password;
}; class Caesar
{
public:
string do_encrypt(const int key, const string str)
{
return string("12345678");
}
}; // 加密适配器类
class CipherAdapter : public Caesar, public data_operation
{
public:
string do_encrypt(const int key, const string str)
{
return Caesar::do_encrypt(key, str);
}
}; void call_object_ada()
{
std::unique_ptr<data_operation> pdp(new CipherAdapter);
if (!pdp)
{
cout << "\n\n 创建密文适配器对象失败\n\n";
return;
} pdp->set_password("ABC");
string str = pdp->do_encrypt(1, string("123"));
cout << "密文 = " << str.c_str();
}

  C、接口适配器        , 类似pad的充电适配器一样,输出的总是恒定值, 比如,iPhone5s的手机充电线,原版充电线输出位5V。下面演示的是 输出5V,输入可以有多种的电压值的电源适配器。

// 5V接口类
class idc5v
{
public:
virtual int output() = 0;
}; // 电源接口类
class ipower
{
public:
virtual int output() = 0;
}; // 220V 电源
class ac220v : public ipower
{
public:
int output() { return 220; }
}; // 110v 电源
class ac110v : public ipower
{
public:
int output() { return 110; }
}; // 完整的电源适配器
class power_adapter : public idc5v
{
public:
power_adapter( ac220v* pinstance) { this->_ipower = pinstance; }
power_adapter( ac110v* pinstance) { this->_ipower = pinstance; } virtual ~power_adapter()
{
if (nullptr != _ipower)
{
delete _ipower;
_ipower = nullptr;
}
} // 重写基类的函数
int output()
{
int ret_val = 0; if (nullptr == _ipower)
cout << "\n\n\n 创建5v对象失败\n\n\n";
else
{
ret_val = _ipower->output();
// 省略达到5V的操作
ret_val = 5;
} return ret_val;
} private:
ipower *_ipower = nullptr;
}; // 调用电源适配器类
void call_interface_ada_demo()
{
ac220v* pac220 = nullptr;
pac220 = new(std::nothrow) ac220v;
if (nullptr == pac220)
{
cout << "\n\n\n 调用电源适配器: 创建220v对象失败\n\n\n";
return;
} idc5v *pdc5v = new(std::nothrow) power_adapter(pac220); if (nullptr == pdc5v)
{
cout << "\n\n\n 调用电源适配器: 创建220v的电源适配器出错 \n\n\n";
}
else
{
cout << "\n\n\n 调用电源适配器: 输出220v对应的适配结果:" << pdc5v->output();
} // 释放资源
if (nullptr != pdc5v)
{
delete pdc5v;
pdc5v = nullptr;
}
}

c++设计模式概述之适配器的更多相关文章

  1. java 28 - 1 设计模式 之 面向对象思想设计原则和模版设计模式概述

    在之前的java 23 中,了解过设计模式的单例模式和工厂模式.在这里,介绍下设计模式 面向对象思想设计原则 在实际的开发中,我们要想更深入的了解面向对象思想,就必须熟悉前人总结过的面向对象的思想的设 ...

  2. java设计模式概述

    java的设计模式大体上分为三大类: 创建型模式(5种):工厂方法模式,抽象工厂模式,单例模式,建造者模式,原型模式. 结构型模式(7种):适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模 ...

  3. 23种GoF设计模式概述

    23种GoF设计模式概述 在前面,我们对 GoF 的 23 种设计模式进行了分类,这里先对各个设计模式的功能进行简要介绍,以便有个大概了解.后面的章节再进行详细介绍. 创建型模式 关注于怎么创建对象的 ...

  4. Java设计模式(一):设计模式概述、UML图、设计原则

    1 设计模式概述 1.1 软件设计模式的产生背景 "设计模式"最初并不是出现在软件设计中,而是被用于建筑领域的设计中. 1977年美国著名建筑大师.加利福尼亚大学伯克利分校环境结构 ...

  5. 设计模式(一)----设计模式概述及UML图解析

    1.设计模式概述 1.1 软件设计模式的产生背景 "设计模式"最初并不是出现在软件设计中,而是被用于建筑领域的设计中. 1977年美国著名建筑大师.加利福尼亚大学伯克利分校环境结构 ...

  6. OOAD-设计模式(二)之GRASP模式与GOF设计模式概述

    一.GRASP模式(通用责任分配软件模式)概述 1.1.理解责任 1)什么是责任 责任是类间的一种合约或义务,也可以理解成一个业务功能,包括行为.数据.对象的创建等 知道责任——表示知道什么 行为责任 ...

  7. PHP设计模式概述

    PHP设计模式概述 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. ...

  8. 设计模式学习心得<适配器 Adapter>

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接 ...

  9. 001-java 设计模式概述

    一.概述 思维导图 GoF(“四人帮”,又称Gang of Four,即Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides) 1 ...

随机推荐

  1. 【samtools】运行报错: error while loading shared libraries:libcrypto.so.1.0.0或libncurses.so.5或libtinfow.so.5

    samtools用conda安装后,总是出现共享库缺失的报错.即便你刚安装samtools时可以用,但后面在同一环境中安装其他相关软件,有可能产生了冲突,导致库替换,因而报错. 避免这种情况,可能最好 ...

  2. R语言与医学统计图形-【20】ggplot2图例

    ggplot2绘图系统--图例:guide函数.标度函数.overrides.aes参数 图例调整函数guide_legend也属于标度函数,但不能单独作为对象使用,即不能如p+guide_legen ...

  3. 【php安全】 register_argc_argv 造成的漏洞分析

    对register_argc_argv的分析 简介 使用 cli模式下,不论是否开始register_argc_argv,都可以获取命令行或者说外部参数 web模式下,只有开启了register_ar ...

  4. 容器之分类与各种测试(四)——unordered-multiset

    unordered-multiset是不定序关联式容器,其底部是通过哈希表实现功能. (ps:黑色框就是bucket,白色框即为bucket上挂载的元素) 为了提高查找效率,bucket(篮子)的数量 ...

  5. 容器之分类与各种测试(四)——multimap

    multiset和multimap的具体区别在于,前者的key值就是自己存储的value,后者的key与value是分开的不相关的. 例程 #include<stdexcept> #inc ...

  6. 集合类——集合输出、栈和队列及Collections集合

    1.集合输出 在之前我们利用了toString()及get()方法对集合进行了输出,其实那都不是集合的标准输出,集合输出有四种方式:Iterator.ListIterator.Enumeration. ...

  7. Maven pom.xml报错解决

    用Maven建了一个web工程,总是在pom.xml头的地方报错: 大概是: Original error: Could not transfer artifact org.hamcrest:hamc ...

  8. go channel 概述 - 管道

    概述 unix/linux OS 的一个进程的输出可以是另一个进程的输入,这些进程使用stdin与stdout设备作为通道,在进程之间传递数据. 同样的,GO中有io.Reader与io.Writer ...

  9. clickhouse客户端使用

    测试初始化 clickhouse-client -m create database if not exists test; use test; drop table test; create tab ...

  10. 【Java 基础】Java 根据Class获取对象实例

    Spring在代码中获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法 ...