class AppServiceMgr {
public:
static AppServiceMgr& getInstance() {
static AppServiceMgr instance;
return instance;
}
virtual ~AppServiceMgr() {}
AppServiceMgr(AppServiceMgr&) = delete;
AppServiceMgr& operator=(AppServiceMgr&) = delete;
public:
// 业务
void DeviceServiceBusiness();
void InputServiceBusiness();
void OutputServiceBusiness();
private:
AppServiceMgr() {}
};
#include "devicemanager.h"
//
// 饿汉
class AppServiceMgr {
public:
static AppServiceMgr& getInstance() {
//返回在程序加载时就实例化出的对象
return _instance;
}
virtual ~AppServiceMgr() {}
// 禁用赋值拷贝
AppServiceMgr(AppServiceMgr&) = delete;
AppServiceMgr& operator=(AppServiceMgr&) = delete;
public:
// 业务
void DeviceServiceBusiness();
void InputServiceBusiness();
void OutputServiceBusiness();
private:
static AppServiceMgr _instance; //定义一个静态成员变量
private:
//将其构造,拷贝构造,赋值运算符重载私有化
AppServiceMgr() {}
};
//在类里面进行定义的静态成员变量需要在类外面进行实例化
AppServiceMgr AppServiceMgr::_instance;
class DeviceManager {
public:
static DeviceManager& getInstance() {
//返回在程序加载时就实例化出的对象
return _instance;
}
virtual ~DeviceManager() {}
// 禁用赋值拷贝
DeviceManager(DeviceManager&) = delete;
DeviceManager& operator=(DeviceManager&) = delete;
public:
// 业务
void addDevice();
void delDevice();
void searchDevice();
private:
static DeviceManager _instance; //定义一个静态成员变量
private:
//将其构造,拷贝构造,赋值运算符重载私有化
DeviceManager() {}
};
//在类里面进行定义的静态成员变量需要在类外面进行实例化
DeviceManager DeviceManager::_instance;
int main()
{
AppServiceMgr::getInstance().DeviceServiceBusiness();
AppServiceMgr::getInstance().InputServiceBusiness();
AppServiceMgr::getInstance().OutputServiceBusiness();
DeviceManager::getInstance().addDevice();
DeviceManager::getInstance().delDevice();
DeviceManager::getInstance().searchDevice();
return 0;
}
#include "devicemanager.h"
#include <mutex>
#include <iostream>
//
// 饿汉
class AppServiceMgr {
public:
static AppServiceMgr& getInstance() {
std::call_once(_initInstanceFlag, &AppServiceMgr::initAppServiceMgr);
return* _instance;
}
virtual ~AppServiceMgr() {}
// 禁用赋值拷贝
AppServiceMgr(AppServiceMgr&) = delete;
AppServiceMgr& operator=(AppServiceMgr&) = delete;
public:
// 业务
void DeviceServiceBusiness();
void InputServiceBusiness();
void OutputServiceBusiness();
private:
class CGarbo {
~CGarbo() {
if (AppServiceMgr::_instance != nullptr)
{
std::cout << "cg";
delete AppServiceMgr::_instance;
AppServiceMgr::_instance = nullptr;
}
}
};
private:
static AppServiceMgr* _instance; //定义一个静态成员变量
static std::once_flag _initInstanceFlag;
static CGarbo _cg;
private:
AppServiceMgr() {}
static void initAppServiceMgr() {
_instance = new AppServiceMgr;
}
};
//在类里面进行定义的静态成员变量需要在类外面进行实例化
AppServiceMgr* AppServiceMgr::_instance = nullptr;
std::once_flag AppServiceMgr::_initInstanceFlag;
AppServiceMgr::CGarbo AppServiceMgr::_cg;
int main()
{
AppServiceMgr::getInstance().DeviceServiceBusiness();
AppServiceMgr::getInstance().InputServiceBusiness();
AppServiceMgr::getInstance().OutputServiceBusiness();
return 0;
}
#include <mutex>
class AppServiceMgr {
public:
static AppServiceMgr* getInstance() {
if (_instance == nullptr) {
_instance = new AppServiceMgr();
}
return _instance;
}
virtual ~AppServiceMgr() {}
// 禁用赋值拷贝
AppServiceMgr(AppServiceMgr&) = delete;
AppServiceMgr& operator=(AppServiceMgr&) = delete;
public:
// 业务
void DeviceServiceBusiness();
void InputServiceBusiness();
void OutputServiceBusiness();
private:
static AppServiceMgr* _instance; //定义单例对象指针
private:
// 将其构造函数私有化
AppServiceMgr() {}
};
//类外对其静态成员变量进行初始化
AppServiceMgr* AppServiceMgr::_instance = nullptr;
// brief: a singleton base class offering an easy way to create singleton
#include <iostream>
template<typename T>
class Singleton {
public:
static T& getInstance() {
static T instance;
return instance;
}
virtual ~Singleton() {}
Singleton(const Singleton&) = delete;
Singleton& operator =(const Singleton&) = delete;
private:
Singleton() {}
};
// Example:
class AppServiceMgr :public Singleton<AppServiceMgr> {
// 需要通过友元访问私有构造函数
//friend class Singleton<AppServiceMgr>;
public:
AppServiceMgr(const AppServiceMgr&) = delete;
AppServiceMgr& operator =(const AppServiceMgr&) = delete;
public:
void DeviceServiceBusiness();
void InputServiceBusiness();
void OutputServiceBusiness();
private:
AppServiceMgr() = default;
};