设计模式——桥接模式(C++)
来源:互联网 发布:考试宝典怎么样知乎 编辑:程序博客网 时间:2024/06/07 09:41
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:Method.h@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#ifndef _METHOD_H#define _METHOD_H#include <string>// 抽象方法class Method {public: virtual void fun() = 0;};// 具体方法Aclass concreteMethodA : public Method {private: std::string name;public: concreteMethodA(std::string name); virtual void fun();};// 具体方法Bclass concreteMethodB : public Method {private: std::string name;public: concreteMethodB(std::string name); virtual void fun();};#endif // _METHOD_H
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:Method.cpp@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#include "Method.h"#include <iostream>using namespace std;void Method::fun() {}concreteMethodA::concreteMethodA(string name) { this->name = name;}void concreteMethodA::fun() { cout << name + ":fun of method A" << endl;}concreteMethodB::concreteMethodB(string name) { this->name = name;}void concreteMethodB::fun() { cout << name + ":fun of method B" << endl;}
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:Object.h@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#ifndef _OBJECT_H#define _OBJECT_H#include "Method.h"// 抽象对象class Object {public: Method* method;public: void set(Method* method); // 在抽象层进行依赖 virtual void run() = 0;};// 具体对象Aclass concreteObjectA : public Object {private: std::string name;public: concreteObjectA(std::string name); virtual void run();};// 具体对象Bclass concreteObjectB : public Object {private: std::string name;public: concreteObjectB(std::string name); virtual void run();};#endif // _OBJECT_H
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:Object.cpp@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#include "Object.h"#include <iostream>using namespace std;void Object::run() {}void Object::set(Method* method) { this->method = method;}// concreteObjectA::concreteObjectA(string name) { this->name = name;}void concreteObjectA::run() { cout << "object A" << endl; method->fun();}//concreteObjectB::concreteObjectB(string name) { this->name = name;}void concreteObjectB::run() { cout << "object B" << endl; method->fun();}
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:main.cpp@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#include <iostream>#include "Object.h"#include "Method.h"using namespace std;// 桥接模式:将抽象和实现分开int main() { Method* method1 = new concreteMethodA("A"); Method* method2 = new concreteMethodB("B"); Object* object1 = new concreteObjectA("A"); Object* object2 = new concreteObjectB("B"); object1->set(method1); object2->set(method2); object1->run(); object2->run(); system("pause"); return 0;}
1 0
- 设计模式——桥接模式(C++)
- 设计模式_桥接模式(C++)
- C#--设计模式之桥接模式
- C语言和设计模式(桥接模式)
- C语言和设计模式(桥接模式)
- C语言和设计模式(桥接模式)
- C语言和设计模式(桥接模式)
- 08.C语言和设计模式(桥接模式)
- 19、C语言和设计模式(桥接模式)
- 设计模式——桥接模式
- 设计模式——桥接模式
- 设计模式——桥接模式
- 设计模式——桥接模式
- 设计模式——桥接模式
- 设计模式——桥接模式
- 设计模式——桥接模式
- 设计模式——桥接模式
- 设计模式——桥接模式
- HDU 1757 矩阵快速幂模版
- Linux分区
- Python生成随机数
- C#之WinForm基础 设置文件对话框只能看到所有类型的文件
- WSAAsyncSelect模型
- 设计模式——桥接模式(C++)
- BNU20410 UVA11992 线段树区间更新
- Base64编码出现换行符
- C#之WinForm基础 设置文件对话框只能看到一种指定类型的文件
- 伸展树(splay树)
- MAC之Sublime Text 2插件安装与步骤
- Linux(二)服务器运行环境配置
- 奇虎360_2017校园招聘笔试编程题第一题
- elasticsearch系列之排序