自动回收资源的单例模式

本文介绍一种很简单的可以自动回收资源的单例模式,平时也经常看别个写的技术文章,CSDN里面有很多牛人,他们写的文章通俗易懂,言简意赅,能给看文章的人带来美的享受。但也有很多写技术文章的童鞋文章冗长,屁话一大篇,浪费时间。好了我废话少说,希望本文对有需要的人能有所帮助:

  • 测试环境:Linux CentOS7.2

直接上代码,小伙伴们可以复制到自己的环境里面编译运行。C++这东西,还是自己咀嚼才有味道。

代码块[SingletonLogManager.h]
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   SingletonLogManager.h
 * Author: Jerry
 *
 * Created on 2017年8月8日, 下午2:25
 */

#ifndef SINGLETONLOGMANAGER_H
#define SINGLETONLOGMANAGER_H

#include <iostream>
using namespace std;

class SingletonLogManager 
{
public:
    static SingletonLogManager* GetInstance();    // 获取一个实例

public:
    void print();

protected:
    SingletonLogManager();
    ~SingletonLogManager();


    // 资源回收
    class SingletonRecovery   
    {  
    public:  
        SingletonRecovery()  
        {  

        }  
        ~SingletonRecovery()  
        {  
        cout << " begin to run destruction !" << endl;
            if (m_pInstance != NULL)  
            {  
                delete m_pInstance;  
                m_pInstance = NULL;                  
            }  
        }  
    };  

    // 资源回收类
    static SingletonRecovery m_SR;  

private:
    static SingletonLogManager*                            m_pInstance;                  // 静态指针

};

#endif /* LOGTOOL_H */

代码块[SingletonLogManager.cpp]
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   LogTool.cpp
 * Author: Jerry
 * 
 * Created on 201788日, 下午2:25
 */
#include <stdio.h>
#include <stdlib.h>
#include "SingletonLogManager.h"

// new一个实例
SingletonLogManager* SingletonLogManager::m_pInstance = new SingletonLogManager;
SingletonLogManager::SingletonRecovery SingletonLogManager::m_SR;


SingletonLogManager::SingletonLogManager() 
{

}

SingletonLogManager::~SingletonLogManager()
{

}

// 获取实例
SingletonLogManager* SingletonLogManager::GetInstance()
{
    return m_pInstance;
}

void SingletonLogManager::print()
{
  printf("SingletonLogManager::print()\n\n");
}

代码块[main.cpp]
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   LogTool.cpp
 * Author: Jerry
 * 
 * Created on 201788日, 下午2:25
 */

#include <iostream>
#include "SingletonLogManager.h"

void testRecovery()
{
  SingletonLogManager* pSingletonLogManager = SingletonLogManager::GetInstance();
  pSingletonLogManager->print();
}

int main()
{  
  cout << "testRecovery begin !" << endl;
  testRecovery();
  cout << "testRecovery end !" << endl;
  return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值