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


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

    #ifndef SHAPE_H
    #define SHAPE_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    #include "board.h"
    
    #include 
    #include "shape.h"
    #include 
    
    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();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

  • 相关阅读:
    《深度学习进阶:自然语言处理》读书笔记:第3章 word2vec
    维也纳酒店抚州市政中心店以品牌之力创造非凡价值
    前端常见的设计模式
    52 https
    DALL E2【论文阅读】
    Windows版Ros环境的搭建以及Rviz显示激光点云信息
    R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、设置可视化图像的主题为theme_gray
    化妆品展示网页设计作业 静态HTML化妆品网站 DW美妆网站模板下载 大学生简单网页作品代码 个人网页制作 学生个人网页设计作业
    Springboot项目的多数据源配置
    MyBatis-Plus中如何使用ResultMap
  • 原文地址:https://blog.csdn.net/weixin_39568531/article/details/134692185