设计模式——桥接模式(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
原创粉丝点击