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

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/211396.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

快手自动评论助手:开发流程与所需技术的深度解析

先来看实操成果&#xff0c;↑↑需要的同学可看我名字↖↖↖↖↖&#xff0c;或评论888无偿分享 一、引言 随着互联网的发展&#xff0c;越来越多的人开始使用快手这款短视频平台。在这个平台上&#xff0c;用户可以分享自己的生活点滴&#xff0c;观看他人的精彩瞬间。然而&am…

Ext4文件系统解析(一)

1、前言 熟悉Linux操作系统的都应该或多或少的了解或者使用过Ext4文件系统。 接下来&#xff0c;会简单介绍Ext4文件系统的一些特性和工作原理。 2、常用概念 在介绍Ext文件系统之前&#xff0c;先简单描述一些相关概念。 块(Block)&#xff1a;Ext文件系统存储分配的基本单…

软件工程 - 第8章 面向对象建模 - 4 - 物理体系结构建模

构件图 构件图概述 构件图描述了软件的各种构件和它们之间的依赖关系。 构件图的作用 在构件图中&#xff0c;系统中的每个物理构件都使用构件符号来表示&#xff0c;通常&#xff0c;构件图看起来像是构件图标的集合&#xff0c;这些图标代表系统中的物理部件&#xff0c;…

java学习part30callabel和线程池方式

140-多线程-线程的创建方式3、4&#xff1a;实现Callable与线程池_哔哩哔哩_bilibili 1.Callable 实现类 使用方式 返回值 2.线程池

Linux expect命令详解

在Linux系统中&#xff0c;expect 是一款非常有用的工具&#xff0c;它允许用户自动化与需要用户输入进行交互的程序。本文将深入探讨expect命令的基本语法、使用方法以及一些最佳实践。 什么是Expect命令&#xff1f; expect 是一个用于自动化交互式进程的工具。它的主要功能…

【PyTorch】线性回归

文章目录 1. 代码实现1.1 一元线性回归模型的训练 2. 代码解读2.1. tensorboardX2.1.1. tensorboardX的安装2.1.2. tensorboardX的使用 1. 代码实现 波士顿房价数据集下载 1.1 一元线性回归模型的训练 import numpy as np import torch import torch.nn as nn from torch.ut…

Ext4文件系统解析(二)

1、前言 想要了解EXT文件系统的工作原理&#xff0c;那了解文件系统在磁盘上的分布就是必不可少的。这一节主要介绍EXT文件系统硬盘存储的物理结构。 由于当前主流的CPU架构均采用小端模式&#xff0c;因此下文介绍均已小端模式为准。 2、超级块 2.1 属性 下表列举出超级块…

Java 8 中 ReentrantLock 与 Synchronized 的区别

&#x1f680; 作者主页&#xff1a; 有来技术 &#x1f525; 开源项目&#xff1a; youlai-mall &#x1f343; vue3-element-admin &#x1f343; youlai-boot &#x1f33a; 仓库主页&#xff1a; Gitee &#x1f4ab; Github &#x1f4ab; GitCode &#x1f496; 欢迎点赞…

分布式ID生成框架Leaf升级踩坑

背景&#xff1a; 在项目中需要一个统一的拿单号等唯一ID的服务&#xff0c;就想起了之前用到的leaf&#xff0c;但是因为项目要求&#xff0c;leaf的版本不符合&#xff0c;需要做一些升级 项目地址&#xff1a;https://github.com/Meituan-Dianping/Leaf 升级点&#xff1…

231202 刷题日报

周四周五&#xff0c;边值班边扯皮&#xff0c;没有刷题。。 今天主要是做了: 1. 稀疏矩阵压缩&#xff0c;十字链表法 2. 快速排序 3.349. 两个数组的交集​​​​​ 4. 174. 地下城游戏 要注意溢出问题&#xff01;

Motion 5 for Mac,释放创意,打造精彩视频特效!

Motion 5 for Mac是一款强大的视频后期特效处理软件&#xff0c;为Mac用户提供了无限的创意可能性。无论你是专业的影视制作人&#xff0c;还是想为个人视频添加独特特效的爱好者&#xff0c;Motion 5都能满足你的需求&#xff0c;让你的视频脱颖而出。 Motion 5提供了丰富多样…

跳表的基础

跳表的作用 无需数组查找目标元素-----从头遍历---O(n); 有序数组查找目标元素-----二分查找---O(logn); 链表查找目标元素----------只能从头遍历---O(n); 那么链表要如何实现O(logn)的查找时间复杂度呢-----跳表。 跳表的定义 有序链表多级索引跳表 就是一个多级链表 …

TA-Lib学习研究笔记(八)——Momentum Indicators 中

TA-Lib学习研究笔记&#xff08;八&#xff09;——Momentum Indicators 中 Momentum Indicators 动量指标&#xff0c;是最重要的股票分析指标&#xff0c;能够通过数据量化分析价格、成交量&#xff0c;预测股票走势和强度&#xff0c;大部分指标都在股票软件中提供。 11. …

力扣题:字符串的反转-11.22

力扣题-11.22 [力扣刷题攻略] Re&#xff1a;从零开始的力扣刷题生活 力扣题1&#xff1a;541. 反转字符串 II 解题思想&#xff1a;进行遍历翻转即可 class Solution(object):def reverseStr(self, s, k):""":type s: str:type k: int:rtype: str"&quo…

[计算机网络] 高手常用的几个抓包工具(下)

文章目录 高手常用的抓包工具一览什么是抓包工具优秀抓包工具HTTP Debugger ProFree Network AnalyzerKismetEtherApeNetworkMiner 结尾 高手常用的抓包工具一览 什么是抓包工具 抓包工具是一种可以捕获、分析和修改网络流量的软件。它可以帮助您进行网络调试、性能测试、安全…

JavaScript 延迟加载的艺术:按需加载的最佳实践

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

cyclictest 交叉编译与使用

目录 使用版本问题编译 numactl编译 cyclictest使用参考 cyclictest 主要是用于测试系统延时&#xff0c;进而判断系统的实时性 使用版本 rt-tests-2.6.tar.gz numactl v2.0.16 问题 编译时&#xff0c;需要先编译 numactl &#xff0c;不然会有以下报错&#xff1a; arm-…

Android 中的权限

关于作者&#xff1a;CSDN内容合伙人、技术专家&#xff0c; 从零开始做日活千万级APP。 专注于分享各领域原创系列文章 &#xff0c;擅长java后端、移动开发、商业变现、人工智能等&#xff0c;希望大家多多支持。 目录 一、导读二、概览三、权限分类3.1 安装时权限3.2 运行时…

Java面试题(每天10题)-------连载(41)

目录 Spring篇 1、什么是Spring框架&#xff1f;Spring框架主要有哪些模块&#xff1f; 2、使用Spring框架能带来哪些好处&#xff1f; 3、什么是控制反转&#xff08;IOC&#xff09;&#xff1f;什么是依赖注入&#xff1f; 4、解释下Spring中的IoC? 5、BeanFactory和…

docker-compose Foxmic dt版

Foxmic dt 版前言 实现企业对资产的基本管理,包含对资产的登记、维修、调拨、转移等基本功能的支持,并提供对资产的耗材、库存进行管理,有完善的组织架构,非常适合中小企业的需求系统整体覆盖了基本的资产管理、合同管理、运维服务、运维服务、数据中心设备管理等多个模块。…
最新文章