C++设计模式——Bridge模式(下)

在上篇 《C++设计模式------Bridge模式(上)》中我们对于桥接模式做了一些介绍。介于桥接模式在实际项目开发中使用广泛,而且也是面试中常问常新的话题。在本篇,我们专注bridge模式在具体的项目开发中的应用,举几个例子来说明。

cpp 复制代码
#ifndef SHAPE_H
#define SHAPE_H

#include <QObject>
#include <QWidget>
#include <QColor>
#include <QPointF>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QDebug>

class ShapeImp;

class Shape
{

public:
    virtual void draw() = 0;

    virtual void setLineColor(const QColor& color) = 0;
    virtual void setLineWidth(const int width) = 0;
    virtual ~Shape() {}
protected:
    Shape(ShapeImp* imp) : imp_(imp) {}
    ShapeImp* imp_;
};


class ShapeImp : public QWidget
{
public:
    virtual void draw() = 0;
    virtual void setLineColor(const QColor& color) = 0;
    virtual void setLineWidth(const int width) = 0;
    virtual ~ShapeImp() {}
protected:
    ShapeImp(QWidget* parent = nullptr):QWidget(parent) {}
    friend class Shape;
};


class ShapeImpWin : public Shape
{
public:
    ShapeImpWin(ShapeImp* pImpl) : Shape(pImpl) {}
public: // operation
    void draw() override{ imp_->draw(); }
    void setLineColor (const QColor& color) override { imp_->setLineColor(color); }
    void setLineWidth(const int width) override { imp_->setLineWidth(width);}
};



class Circle : public ShapeImp
{
    Q_OBJECT
public:
    Circle(const QPointF& center,qreal raduis,QWidget* parent = nullptr)
        : m_center(center),m_raduis(raduis),ShapeImp(parent){}

    void draw(){ update(); }

    void setLineColor(const QColor& color) override {m_color = color;}
    void setLineWidth(const int width) override{ m_lineWidth = width; }

    virtual void paintEvent(QPaintEvent* e) override
    {
        QPainter painter(this);
        QPen pen;
        pen.setColor( m_color /*QColor(Qt::red)*/);
        pen.setWidth(m_lineWidth);

        QBrush brush;
        brush.setColor(QColor(Qt::lightGray));
        painter.setPen(pen);
        painter.setBrush(brush);

        // draw circle
        painter.drawEllipse(m_center,m_raduis,m_raduis);

        e->accept();
    }

private:
    QPointF m_center;
    qreal m_raduis = 50;
    QColor m_color = QColor(Qt::black);
    int m_lineWidth = 1;
};

class Rectange : public ShapeImp
{
    Q_OBJECT
public:
    Rectange(const QPointF& topleft,qreal width,qreal height,QWidget* parent = nullptr)
        : m_topleft(topleft),m_width(width),m_height(height),ShapeImp(parent){}

    void draw(){ update(); }

    void setLineColor(const QColor& color) override {m_color = color;}
    void setLineWidth(const int width) override{ m_lineWidth = width; }

    virtual void paintEvent(QPaintEvent* e) override
    {
        QPainter painter(this);
        QPen pen;
        pen.setColor( m_color /*QColor(Qt::red)*/);
        pen.setWidth(m_lineWidth);

        QBrush brush;
        brush.setColor(QColor(Qt::lightGray));
        painter.setPen(pen);
        painter.setBrush(brush);

        // draw circle
        QRectF r(m_topleft.x(),m_topleft.y(),m_width,m_height);
        painter.drawRect(r);

        qDebug() << r;

        e->accept();
    }

private:
    QPointF m_topleft;
    qreal m_width = 100;
    qreal m_height = 100;
    QColor m_color = QColor(Qt::black);
    int m_lineWidth = 1;
};
#endif // SHAPE_H
cpp 复制代码
#include "board.h"

#include <QApplication>
#include "shape.h"
#include <QHBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    ShapeImp* pImpl_circle = new Circle(QPointF(100,100),80);
    pImpl_circle->setFixedSize(300,300);
    Shape* pShape = new ShapeImpWin(pImpl_circle);
    pShape->setLineWidth(1);
    pShape->setLineColor(QColor(Qt::red));
    pShape->draw();
    pImpl_circle->show();

    ShapeImp* pImpl_rect = new Rectange(QPointF(10,10),80,80);
    pImpl_rect->setFixedSize(300,300);
    Shape* pShap1 = new ShapeImpWin(pImpl_rect);
    pShap1->setLineWidth(1);
    pShap1->setLineColor(QColor(Qt::red));
    pShap1->draw();
    pImpl_rect->show();

    return a.exec();
}
相关推荐
郭老二几秒前
【C++】RPC:远程程序调用
c++·rpc
承渊政道几秒前
【贪心算法】(经典实战应用解析(六):整数替换、俄罗斯套娃信封问题、可被三整除的最⼤和、距离相等的条形码、重构字符串)
c++·算法·leetcode·贪心算法·排序算法·动态规划·哈希算法
宠..1 分钟前
VS Code SSH 远程连接 Ubuntu 并实现快速运行(C/C++示例)
java·运维·c语言·开发语言·c++·ubuntu·ssh
闻缺陷则喜何志丹9 分钟前
【图论 树 启发式合并】P7165 [COCI2020-2021#1] Papričice|普及+
c++·算法·启发式算法·图论··洛谷
alexwang21110 分钟前
AT_abc458_d [ABC458D] Chalkboard Median题解
c++·算法·题解·atcoder
故事和你9111 分钟前
洛谷-【图论2-4】连通性问题1
开发语言·数据结构·c++·算法·动态规划·图论
我先去打把游戏先19 分钟前
Ubuntu虚拟机(服务器版本)Git安装教程(附常用命令)——从零开始掌握版本控制
服务器·c语言·c++·git·嵌入式硬件·物联网·ubuntu
艾莉丝努力练剑24 分钟前
【Linux网络】Linux 网络编程:HTTP(四)从手写服务器到生产级 Nginx 与 cpp-httplib 实战
linux·运维·服务器·网络·c++·nginx·http
咩咦32 分钟前
C++学习笔记21:日期类加减天数
c++·学习笔记·运算符重载·日期类·operator+·operator+=
努力努力再努力wz33 分钟前
【QT入门系列】QWidget 六大常用属性详解:windowOpacity、cursor、font、focus、toolTip 与 styleSheet
android·开发语言·数据结构·c++·qt·mysql·算法