C++设计模式-采用装饰模式用户和管理员加载不同的模块(Qt框架实现)
目錄
?
?
基本概念
例子與實例
?
基本概念
這里在本https://blog.csdn.net/qq78442761/article/details/91379724例子的基本上,進行擴充與修改。
同樣,結構圖如下
?
例子與實例
當加載如下信息時:
界面如下:
修改成如下:
界面運行如下:
程序結構如下:
這里只是簡單處理,所有的模塊都是formxxxxxxxxx.ui和相關類組成的,按道理來說,應該是寫到一個dll里面,調用不同的dll,進行加載,這樣的話,耦合性就低了,而且還方便后期的維護工作。
這里的concretedcoratorX.h和.cpp是decorator的子類,decorator是透明的,寫好了就不用改。通過concretedcoratorX在operation的地方寫對應模塊的代碼,在通過如下的代碼,進行加載即可
詳細代碼,請到下面這個鏈接里面查找。
component為最關鍵的接口。他有著承上啟下的作用,關聯了comcretecomponent這個具體對象,和decorator與裝飾相關的(這里我給他稱之為與裝飾相關的中間層)。
感覺主要就是套路!
component.h
#ifndef COMPONENT_H #define COMPONENT_H#include <QObject>QT_BEGIN_NAMESPACE class QWidget; QT_END_NAMESPACEclass Component { public:virtual void processOperation(QWidget *page = nullptr);protected:Component(); };#endif // COMPONENT_Hcomponent.cpp
#include "component.h"void Component::processOperation(QWidget *page) {Q_UNUSED(page) }Component::Component() {}decorator.h
#ifndef DECORATOR_H #define DECORATOR_H#include "component.h"class Decorator: public Component { public:Decorator();void setComponent(Component *component);void processOperation(QWidget *page = nullptr);private:Component *m_component; };#endif // DECORATOR_Hdecorator.cpp
?
源碼打包下載地址如下:
https://github.com/fengfanchen/Qt/tree/master/DecoratorWidget
總結
以上是生活随笔為你收集整理的C++设计模式-采用装饰模式用户和管理员加载不同的模块(Qt框架实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL工作笔记-解决导入外部sql中
- 下一篇: C++笔记-lambda表达式需要注意的