作者在 2011-11-29 20:40:39 发布以下内容
More programming lover like to study pattern. Especially for the newcomer who want to learn more about the basic knowledge.
Therefore, I write a simple demo to demostrate how to denote the singleton pattern.
At first, I need to explain why we apply this pattern. For instance, CEO. we all konw that one company just can have a only CEO. So,when we ask who is the CEO of the company, it must the specified guy.
singleton pattern code steps
1) private static data member (it dednotes current class object or pointer, They also ok)
2) private constuct function (limit the instance just can be one)
3) static member function to access the static variable.
Now I give a log file demo. Actually the log file is the same as the CEO demo.
Header file
// LogFile.h: interface for the CLogFile class.
//
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_LOGFILE_H__1F67BBEF_C14E_472B_AD97_49BE45674688__INCLUDED_)
#define AFX_LOGFILE_H__1F67BBEF_C14E_472B_AD97_49BE45674688__INCLUDED_
#define AFX_LOGFILE_H__1F67BBEF_C14E_472B_AD97_49BE45674688__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CLogFile
{
// private data member
static CLogFile* sm_pLogFile;
{
// private data member
static CLogFile* sm_pLogFile;
CString m_strFilePath;
public:
static CLogFile* GetInstance();
virtual ~CLogFile();
public:
static CLogFile* GetInstance();
virtual ~CLogFile();
void WriteLogInfo(CString strFilePath);
void ReadLogInfo(CString& strMsg);
private:
// pravite construct
CLogFile();
};
void ReadLogInfo(CString& strMsg);
private:
// pravite construct
CLogFile();
};
#endif // !defined(AFX_LOGFILE_H__1F67BBEF_C14E_472B_AD97_49BE45674688__INCLUDED_)
implement file
#include "stdafx.h"
#include "CLoggingFile.h"
#include "LogFile.h"
#include <io.h>
#include "stdafx.h"
#include "CLoggingFile.h"
#include "LogFile.h"
#include <io.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// initialization the sm_pLogFile;
CLogFile* CLogFile::sm_pLogFile = NULL;
CLogFile* CLogFile::sm_pLogFile = NULL;
CLogFile::CLogFile()
: m_strFilePath("")
{
}
: m_strFilePath("")
{
}
CLogFile::~CLogFile()
{
if (sm_pLogFile)
{
delete sm_pLogFile;
sm_pLogFile = NULL;
}
}
{
if (sm_pLogFile)
{
delete sm_pLogFile;
sm_pLogFile = NULL;
}
}
CLogFile* CLogFile::GetInstance()
{
if ( NULL == sm_pLogFile)
{
sm_pLogFile = new CLogFile();
}
return sm_pLogFile;
}
{
if ( NULL == sm_pLogFile)
{
sm_pLogFile = new CLogFile();
}
return sm_pLogFile;
}
void CLogFile::WriteLogInfo(CString strMsg)
{
m_strFilePath = "log.txt";
// check file exist?
int nLen = m_strFilePath.GetLength();
char* filePath = (char*)m_strFilePath.GetBuffer(nLen);
if (_access(filePath,0) != -1)
{
_unlink(filePath); // remove this log file.
}
CStdioFile fOriginal;
if(fOriginal.Open(m_strFilePath,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate))
{
// File the time information
CString strData;
CTime time = CTime::GetCurrentTime();
strData = time.Format(" %H:%M:%S");
strMsg += strData;
fOriginal.WriteString(strMsg);
fOriginal.Close();
}
else
{
AfxMessageBox("Cannot open the file: "+ m_strFilePath );
return;
}
}
{
m_strFilePath = "log.txt";
// check file exist?
int nLen = m_strFilePath.GetLength();
char* filePath = (char*)m_strFilePath.GetBuffer(nLen);
if (_access(filePath,0) != -1)
{
_unlink(filePath); // remove this log file.
}
CStdioFile fOriginal;
if(fOriginal.Open(m_strFilePath,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate))
{
// File the time information
CString strData;
CTime time = CTime::GetCurrentTime();
strData = time.Format(" %H:%M:%S");
strMsg += strData;
fOriginal.WriteString(strMsg);
fOriginal.Close();
}
else
{
AfxMessageBox("Cannot open the file: "+ m_strFilePath );
return;
}
}
void CLogFile::ReadLogInfo(CString& strMsg)
{
int nLen = m_strFilePath.GetLength();
char* filePath = (char*)m_strFilePath.GetBuffer(nLen);
if (_access(filePath,0) == -1)
{
AfxMessageBox(m_strFilePath + " is not existing");
return;
}
CStdioFile fDest;
if(fDest.Open(m_strFilePath,CFile::modeRead))
{
// File the time information
fDest.ReadString(strMsg);
fDest.Close();
}
else
{
AfxMessageBox("Cannot open the file: "+ m_strFilePath );
return;
}
}
{
int nLen = m_strFilePath.GetLength();
char* filePath = (char*)m_strFilePath.GetBuffer(nLen);
if (_access(filePath,0) == -1)
{
AfxMessageBox(m_strFilePath + " is not existing");
return;
}
CStdioFile fDest;
if(fDest.Open(m_strFilePath,CFile::modeRead))
{
// File the time information
fDest.ReadString(strMsg);
fDest.Close();
}
else
{
AfxMessageBox("Cannot open the file: "+ m_strFilePath );
return;
}
}
The Main Function
#include "LogFile.h"
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
CLogFile* pLogFile = CLogFile::GetInstance();
pLogFile->WriteLogInfo("ClogFle is runing");
CString strLogInfo;
pLogFile->ReadLogInfo(strLogInfo);
AfxMessageBox(strLogInfo);
return 0;
}
{
CLogFile* pLogFile = CLogFile::GetInstance();
pLogFile->WriteLogInfo("ClogFle is runing");
CString strLogInfo;
pLogFile->ReadLogInfo(strLogInfo);
AfxMessageBox(strLogInfo);
return 0;
}
I hope you like it. Thank your time.
Best regards
Pnfeng