Bridge设计模式
来源:互联网 发布:手机淘宝看退款率 编辑:程序博客网 时间:2024/06/10 20:04
一篇不错的文章http://blog.csdn.net/hguisu/article/details/7529194
文章中以将毛笔大小和颜色分开实现为实例阐述了Bridge模式的优点。作者给出的是PHP实现代码,这里给出C++实现代码!
BrushPenAbstraction.h
#ifndef _BRUSHPENABSTRACTION_H_#define _BRUSHPENABSTRACTION_H_class ImplementorColor;class BrushPenAbstraction{public:BrushPenAbstraction();virtual ~BrushPenAbstraction();virtual void OperationDraw() = 0;};//SmallBrushPenAbstractionclass SmallBrushPenAbstraction :public BrushPenAbstraction{public:SmallBrushPenAbstraction(ImplementorColor * color = 0){ m_color = color; }~SmallBrushPenAbstraction(){}void OperationDraw();private:ImplementorColor * m_color;};//MidBrushPenAbstractionclass MidBrushPenAbstraction :public BrushPenAbstraction{public:MidBrushPenAbstraction(ImplementorColor * color = 0){ m_color = color; }~MidBrushPenAbstraction(){}void OperationDraw();private:ImplementorColor * m_color;};//BigBrushPenAbstractionclass BigBrushPenAbstraction :public BrushPenAbstraction{public:BigBrushPenAbstraction(ImplementorColor * color = 0);~BigBrushPenAbstraction();void OperationDraw();private:ImplementorColor * m_color;};#endif
BrushPenAbstraction.cpp
#include "BrushPenAbstraction.h"#include "ImplementorColor.h"#include <iostream>//BrushPenAbstractionBrushPenAbstraction::BrushPenAbstraction(){}BrushPenAbstraction::~BrushPenAbstraction(){}//SmallBrushPenAbstractionvoid SmallBrushPenAbstraction::OperationDraw(){std::cout << "This is a small and ";m_color->OperationDraw();std::cout << " brush!\n";}//MidBrushPenAbstractionvoid MidBrushPenAbstraction::OperationDraw(){std::cout << "This is a middle and ";m_color->OperationDraw();std::cout << " brush!\n";}//BigBrushPenAbstractionBigBrushPenAbstraction::BigBrushPenAbstraction(ImplementorColor * color){m_color = color; }BigBrushPenAbstraction::~BigBrushPenAbstraction(){}void BigBrushPenAbstraction::OperationDraw(){std::cout << "This is a big and ";m_color->OperationDraw();std::cout << " brush!\n";}
ImplementorColor.h
#ifndef _IMPLEMENTORCOLOR_H_#define _IMPLEMENTORCOLOR_H_class ImplementorColor{public:ImplementorColor();virtual ~ImplementorColor();virtual void OperationDraw()=0;};//ImplementorRedclass ImplementorRed : public ImplementorColor{public:ImplementorRed();~ImplementorRed();void OperationDraw();};//ImplementorGreenclass ImplementorGreen : public ImplementorColor{public:ImplementorGreen();~ImplementorGreen();void OperationDraw();};//ImplementorBlueclass ImplementorBlue : public ImplementorColor{public:ImplementorBlue();~ImplementorBlue();void OperationDraw();};#endif
ImplementorColor
#include <iostream>#include "BrushPenAbstraction.h"#include "ImplementorColor.h"int main(){ImplementorColor * red = new ImplementorRed();ImplementorColor * blue = new ImplementorBlue();BrushPenAbstraction * bigBrush = new BigBrushPenAbstraction(red);bigBrush->OperationDraw();BrushPenAbstraction * midBrush = new MidBrushPenAbstraction(red);midBrush->OperationDraw();BrushPenAbstraction * smallBrush = new SmallBrushPenAbstraction(blue);smallBrush->OperationDraw();getchar();return 0;}
结果
C++ primer plus将继承描述为为一种 is-a 关系;而有元类是一种 has-a 关系,感觉Factory,Builder,包括这里的Bridge,可能还有更多我暂时没了解的设计模式,实现的也是一种has-a关系!
0 0
- 设计模式----Bridge模式
- 设计模式:Bridge模式
- 设计模式----Bridge模式
- 设计模式 -- Bridge模式
- 设计模式-Bridge模式
- 【设计模式】bridge模式
- 设计模式----Bridge模式
- 设计模式--------Bridge模式
- 设计模式-Bridge模式
- 设计模式 - Bridge模式
- 设计模式--Bridge 模式
- 设计模式之Bridge
- 设计模式之Bridge
- 设计模式之Bridge
- 设计模式二:bridge
- 设计模式之Bridge
- 设计模式之Bridge
- 设计模式之Bridge
- minicom配置USB串口遇到到问题
- hdu - 1037 ACM
- 搜索表单____
- 面试专题1----------链表(单链表)
- SCIENCE公布125个科学前沿问题
- Bridge设计模式
- 自己实现printf函数,用...
- 常用scanf和printf格式码
- 华为手机调试时看不到错误log打印解决方案
- LeetCode(172) Factorial Trailing Zeroes
- cheap ugg boots vtfei sfmto pkkll
- [LeetCode] Combination Sum (复习merge sort)
- 前段小菜博客记录之路——起点
- adoop入门学习笔记