开源程序LAN_share 单例模式巧用

Posted COCO_PEAK_NOODLE

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开源程序LAN_share 单例模式巧用相关的知识,希望对你有一定的参考价值。

以前写单例模式时,考虑的单个类的单例,看看下面的代码如何实现单例类和主程序分离,便于复用
作者主要使用QLocalSocket和QLocalServer进行程序的注册和监听,所以再次有同名称的程序打看,就会给之前的程序发消息,令其主动弹出,而自身则悄然关闭,实现了单例运行程序。

singleinstance.h
/*
    LANShare - LAN file transfer.
    Copyright (C) 2016 Abdul Aris R. <abdularisrahmanudin10@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef SINGLEINSTANCE_H
#define SINGLEINSTANCE_H

#include <QObject>
#include <QtNetwork/QLocalServer>

/*
 * SingleInstance, digunakan agar aplikasi hanya memiliki satu instance
 * yang berjalan disistem operasi
 */
class SingleInstance : public QObject

    Q_OBJECT

public:
    SingleInstance(const QString& id, QObject* parent = 0);
    ~SingleInstance();

    QString getLastErrorString() const;
    bool start();
    bool hasPreviousInstance();

Q_SIGNALS:
    void newInstanceCreated();

private Q_SLOTS:
    void onNewConnection();

private:

    QLocalServer mServer;
    QString mName;
;

#endif // SINGLEINSTANCE_H

singleinstance.cpp
/*
    LANShare - LAN file transfer.
    Copyright (C) 2016 Abdul Aris R. <abdularisrahmanudin10@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtNetwork/QLocalSocket>

#include "singleinstance.h"

SingleInstance::SingleInstance(const QString& name, QObject* parent)
    : QObject(parent), mName(name)

    connect(&mServer, &QLocalServer::newConnection, this, &SingleInstance::onNewConnection);


SingleInstance::~SingleInstance()



QString SingleInstance::getLastErrorString() const

    return mServer.errorString();


bool SingleInstance::start()

    mServer.removeServer(mName);
    return mServer.listen(mName);


bool SingleInstance::hasPreviousInstance()

    QLocalSocket socket;
    socket.connectToServer(mName);

    return socket.waitForConnected();


void SingleInstance::onNewConnection()

    emit newInstanceCreated();


这样我们只要需要单例的地方加上这句话,就可以实现对象的单例

    QObject::connect(&si, &SingleInstance::newInstanceCreated, [&mainWindow]() 
        mainWindow.setMainWindowVisibility(true);
    );

最后要了解这个QLocalSocket和QLocalServer是个啥?他们是进程通信用到的类。

进程通信(IPC)的方法有很多,项目开发中,需要根据业务需求来选择适合的IPC方式。所谓LocalSocket,其实就是在socket的基础上衍生出来的一种IPC通信机制。其旨在解决同一台主机上不同进程间互相通信的问题,不能像网络通信使用的socket一样实现不同主机间通信。但正因为这一点,它不需要经过网络协议栈,不需要打包拆包、计算校验,所以执行效率要更高。
参考网址:https://blog.csdn.net/m0_46577050/article/details/123499117

以上是关于开源程序LAN_share 单例模式巧用的主要内容,如果未能解决你的问题,请参考以下文章

JAVA设计模式--单例模式

设计模式之单例模式——Singleton

C# 静态类 单例模式 对比

单例模式的 几种写法

什么是单例模式

什么是单例模式