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

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

#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
#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();
}

在这里插入图片描述

相关推荐

  1. 设计模式--桥接模式Bridge Pattern)

    2023-12-06 10:28:01       30 阅读
  2. 设计模式】6、bridge 桥接模式

    2023-12-06 10:28:01       14 阅读
  3. 设计模式——桥接模式Bridge

    2023-12-06 10:28:01       10 阅读
  4. 设计模式-11 - Bridge Method 桥接模式

    2023-12-06 10:28:01       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 10:28:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 10:28:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 10:28:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 10:28:01       18 阅读

热门阅读

  1. 腾讯面试笔试题2023.11.30

    2023-12-06 10:28:01       38 阅读
  2. gsap实现文字动画效果02

    2023-12-06 10:28:01       38 阅读
  3. Oracle初始化参数文件pfile和spfile

    2023-12-06 10:28:01       34 阅读
  4. AFPN:用于目标检测的渐近特征金字塔网络

    2023-12-06 10:28:01       38 阅读
  5. AFPN:用于目标检测的渐近特征金字塔网络

    2023-12-06 10:28:01       37 阅读
  6. 【SpringCloud】设计原则之前后端分离与版本控制

    2023-12-06 10:28:01       28 阅读
  7. ES6基础语法

    2023-12-06 10:28:01       40 阅读
  8. ES6迭代器

    2023-12-06 10:28:01       36 阅读
  9. 查看php进程占用内存

    2023-12-06 10:28:01       39 阅读
  10. 【RabbitMQ基础编程模型】

    2023-12-06 10:28:01       35 阅读