设计模式入门,适配器模式,c++代码实现
// test07.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//设计模式第7章 适配器模式
class Duck
{
public:
virtual void quack() = 0;
virtual void fly() = 0;
};
class MallardDuck : public Duck
{
public:
void quack()
{
printf("Quack\n");
}
void fly()
{
printf("I'm flying\n");
}
};
class Turkey
{
public:
virtual void gobble() = 0;
virtual void fly() = 0;
};
class WildTurkey : public Turkey
{
public:
void gobble()
{
printf("Gobble gobble\n");
}
void fly()
{
printf("I'm flying a short distance\n");
}
};
class TurkeyAdapter : public Duck
{
Turkey* turkey;
public:
TurkeyAdapter(Turkey* in_turkey)
{
turkey = in_turkey;
}
void quack()
{
turkey->gobble();
}
void fly()
{
for (int i = 0; i<5; i++)
{
turkey->fly();
}
}
};
void testDuck(Duck* duck)
{
duck->quack();
duck->fly();
};
int _tmain(int argc, _TCHAR* argv[])
{
MallardDuck* duck = new MallardDuck();
WildTurkey* turkey = new WildTurkey();
Duck* turkeyAdapter = new TurkeyAdapter(turkey);
printf("the turkey says...\n");
turkey->gobble();
turkey->fly();
printf("\nthe duck says...\n");
testDuck(duck);
printf("\nthe turkeyAdapter says...\n");
testDuck(turkeyAdapter);
return 0;
}
设计模式入门,适配器模式,c++代码实现的更多相关文章
- 设计模式入门,观察者模式,c++代码实现
// test02.cpp : Defines the entry point for the console application.////设计模式第2章 观察者模式#include " ...
- php设计模式之适配器模式实例代码
<?php header("Content-type:text/html;charset=utf-8"); // 适配器模式 /** * 查看天气接口 */ class Ti ...
- 设计模式入门,策略模式,c++代码实现
// test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...
- 每天一个设计模式-3 适配器模式(Adapteer)
每天一个设计模式-3 适配器模式(Adapteer) 1.现实中的情况 旧式电脑的硬盘是串口的,直接与硬盘连接,新硬盘是并口的,显然新硬盘不能直接连在电脑上,于是就有了转接线.好了,今天的学习主题出来 ...
- Head First 设计模式之适配器模式与外观模式
Head First设计模式之适配器模式与外观模式 前言: 之前讲过装饰者模式,将对象包装起来并赋予新的职责,这一章我们也会将对象进行包装,只不过是让它们看起来不像自己而像是别的东西.这样就可以在设计 ...
- 基于java的设计模式入门(1)——为什么要学习设计模式
大年初一,楼主在这里给大家拜年,祝大家码上升职加薪,码上有对象结婚,码上有车有房,幸福安康. 过完年,回学校注册报道之后,大概就要回深圳到公司开始实习了.提高自己,无非就有两种方式,一是看书学习,二是 ...
- C#设计模式(7)——适配器模式(Adapter Pattern)
一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...
- Java(Android)编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)
1.组合和继承之间的选择 组合和继承都允许在新的类中放置子对象,组合是显式的这样做,而继承则是隐式的做. 组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形.即在新类中嵌入某个对象,让其实 ...
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...
- C#设计模式之七适配器模式(Adapter)【结构型】
一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题, ...
随机推荐
- [JS] 瀑布流加载
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- 微信小程序ofo小黄车+thinkphp5.0打造全栈应用
原文地址:https://www.imooc.com/article/20310 ofo至今还没有微信小程序(很费解),每次用ofo都得去支付宝,很不方便,我用微信用的比较多,无意间在简书上面看到某人 ...
- linux上搭建nginx+php+mysql环境详细讲解
1.mysql安装 #安装编译环境 yum install -y gcc gcc-c++ gcc-devel g++ g++-devel; yum install -y wget yum instal ...
- 2016级算法第六次上机-C.AlvinZH的学霸养成记II
1032 AlvinZH的学霸养成记II 思路 中等题,贪心. 所有课程按照DDL的大小来排序. 维护一个当前时间curTime,初始为0. 遍历课程,curTime加上此课程持续时间d,如果这时cu ...
- Centos7下安装CUDA
https://developer.nvidia.com/cuda-80-ga2-download-archive https://developer.nvidia.com/rdp/cudnn-dow ...
- jquery scrollTop()与scrollLeft()
1.scrollLeft() scrollLeft() 方法设置或返回被选元素的水平滚动条位置. 提示:当滚动条位于最左侧时,位置是 0. 当用于返回位置时:该方法返回第一个匹配元素的滚动条的水平位置 ...
- P2486 [SDOI2011]染色(树剖)区间覆盖+区间的连续段
https://www.luogu.org/problemnew/show/P2486 值的一看https://www.cnblogs.com/Tony-Double-Sky/p/9283262.ht ...
- 一些很好用但不常用的css属性总结 (持续中......)
在各种框架横行的9012,感觉我们学前端的都变得浮躁了很多,放一张最近流行的神图: 唉,扯远了, 还是整理我的东西吧,路漫漫其修远兮,吾将上下而求索! 1, position:sticky; 粘性定位 ...
- 05-树9 Huffman Codes (30 分)
In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...
- javac之Method Invocation Expressions
15.12.1. Compile-Time Step 1: Determine Class or Interface to Search 15.12.2. Compile-Time Step 2: D ...