Qt|设计模式工作笔记-对单例模式进一步的理解(静态加单例实现专门收发UDP对象)
生活随笔
收集整理的這篇文章主要介紹了
Qt|设计模式工作笔记-对单例模式进一步的理解(静态加单例实现专门收发UDP对象)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
?
理論
源碼
理論
這里只說明一點,使用單例模式,私有成員里放一個成員,這個成員是一個靜態(tài)成員,把構(gòu)造函數(shù)都屏蔽掉(放到protected或者private中即可)使用一個flag,讓其只能生成一次,因為是靜態(tài)的成員,所以在各個.cpp文件里面都可以用,這樣就實現(xiàn)了單例模式的內(nèi)容,今天剛剛體會到這種功能,仿寫了出這樣的一套代碼!
源碼
程序結(jié)構(gòu)如下圖所示:
Qt運行截圖如下:
串口調(diào)試工具運行截圖如下:
源碼如下:
singleudp.h
#ifndef SINGLEUDP_H #define SINGLEUDP_H#include <QObject> #include <QHostAddress>#define NoInstance 0 #define HaveInstance 1QT_BEGIN_NAMESPACE class QUdpSocket; QT_END_NAMESPACEclass SingleUDP: public QObject {Q_OBJECT public:static int nFlag;static SingleUDP *createObject(const int &port);static void clearTheObject();static void sendMsg(char *msg, QHostAddress address, int peerPort);QUdpSocket *getUdpSocket();~SingleUDP();protected:SingleUDP(const int &port);SingleUDP(){}SingleUDP(const SingleUDP &singleUdp){ Q_UNUSED(singleUdp) }SingleUDP & operator = (SingleUDP &singleUdp){ Q_UNUSED(singleUdp) return *this; }protected slots:void readPendingDatagrams();private:static SingleUDP *m_instance;QUdpSocket *m_udpSocket; };#endif // SINGLEUDP_Hwidget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE class QTimer; QT_END_NAMESPACEnamespace Ui { class Widget; }class SingleUDP;class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();protected slots:void timeOut();private:Ui::Widget *ui;SingleUDP *m_singleUDP;QTimer *m_timer; };#endif // WIDGET_Hmain.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }singleudp.cpp
#include "singleudp.h" #include <QUdpSocket> #include <QMessageBox> #include <QHostAddress> #include <QDebug> #include <QByteArray>int SingleUDP::nFlag = NoInstance; SingleUDP* SingleUDP::m_instance = NULL;SingleUDP *SingleUDP::createObject(const int &port) {if(NoInstance == nFlag){nFlag = HaveInstance;m_instance = new SingleUDP(port);return m_instance;}else{return m_instance;} }void SingleUDP::clearTheObject() {if(HaveInstance == nFlag){nFlag = NoInstance;delete m_instance;delete m_instance->getUdpSocket();} }void SingleUDP::sendMsg(char *msg, QHostAddress address, int peerPort) {if(HaveInstance == nFlag){m_instance->getUdpSocket()->writeDatagram(msg, 128, address, peerPort);return;}qDebug() << "沒有實例化socket對象!";return; }QUdpSocket *SingleUDP::getUdpSocket() {if(nFlag == NoInstance)return NULL;return m_udpSocket; }SingleUDP::SingleUDP(const int &port) {m_udpSocket = new QUdpSocket;if(!m_udpSocket->bind(port)){QMessageBox::information(NULL, "提示", "端口綁定失敗:" + m_udpSocket->errorString());}connect(m_udpSocket, SIGNAL(readyRead()), SLOT(readPendingDatagrams())); }void SingleUDP::readPendingDatagrams() {while(m_udpSocket->hasPendingDatagrams()){QHostAddress srcAddr;quint16 SrcPort;QByteArray datagram;datagram.resize(m_udpSocket->pendingDatagramSize());m_udpSocket->readDatagram(datagram.data(), datagram.size(), &srcAddr, &SrcPort);qDebug() << "IP:" << srcAddr.toString() << " port:"<< QString::number(SrcPort) << "數(shù)據(jù):" << datagram;} }SingleUDP::~SingleUDP() {}widget.cpp
#include "widget.h" #include "ui_widget.h" #include "singleudp.h"#include <QTimer>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);m_singleUDP = SingleUDP::createObject(7755);m_timer = new QTimer;connect(m_timer, SIGNAL(timeout()), this, SLOT(timeOut()));m_timer->start(500); }Widget::~Widget() {delete ui; }void Widget::timeOut() {SingleUDP::sendMsg("1234567ABCDEFG", QHostAddress("127.0.0.1"), 10000); }?
總結(jié)
以上是生活随笔為你收集整理的Qt|设计模式工作笔记-对单例模式进一步的理解(静态加单例实现专门收发UDP对象)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-Qt生成dll或so,并且
- 下一篇: C++ STL list输出和增加