-
July 21st, 2010, 10:53 PM
#1
how CWinApp realize the singleton pattern?
Generally,singleton pattern is applied by the way that the ctor is private and gives a member funtion named getInstance, but CWinApp's instance is defined as a global object,its ctor is not private.So how MFC realizes the singleton pattern for CWinApp?
-
July 22nd, 2010, 12:38 AM
#2
Re: how CWinApp realize the singleton pattern?
It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:
Code:
CWinApp::CWinApp(LPCTSTR lpszAppName)
{
if (lpszAppName != NULL)
m_pszAppName = _tcsdup(lpszAppName);
else
m_pszAppName = NULL;
// initialize CWinThread state
AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
ENSURE(pModuleState);
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ENSURE(pThreadState);
ASSERT(AfxGetThread() == NULL); // <- this assertion fails
...
}
So, the first CWinApp instance constructor makes some initialization, setting global variables. The second instance asserts that these variables are not initialized. Access to the only CWinApp-derived instance is done using AFX_MODULE_STATE global variable.
Last edited by Alex F; July 22nd, 2010 at 12:41 AM.
-
July 22nd, 2010, 12:43 AM
#3
Re: how CWinApp realize the singleton pattern?
MFC was around long before design patterns such as the Singleton became mainstream.
-
July 22nd, 2010, 01:03 AM
#4
Re: how CWinApp realize the singleton pattern?
Originally Posted by Alex F
It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:
Code:
CWinApp::CWinApp(LPCTSTR lpszAppName)
{
if (lpszAppName != NULL)
m_pszAppName = _tcsdup(lpszAppName);
else
m_pszAppName = NULL;
// initialize CWinThread state
AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
ENSURE(pModuleState);
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ENSURE(pThreadState);
ASSERT(AfxGetThread() == NULL); // <- this assertion fails
...
}
So, the first CWinApp instance constructor makes some initialization, setting global variables. The second instance asserts that these variables are not initialized. Access to the only CWinApp-derived instance is done using AFX_MODULE_STATE global variable.
thanks for your reply!
In debug mode, a assert will be throwed.Ignoring the assert,the application would continue to execute well. If creating second CWinApp-derived object in release mode, there is no assert.Does the second instance overwrite the first instance?Is it that CWinApp does not strictly apply singleton pattern?
Last edited by zxjun84; July 22nd, 2010 at 07:35 AM.
-
July 22nd, 2010, 07:07 AM
#5
Re: how CWinApp realize the singleton pattern?
Originally Posted by zxjun84
Strictly speaking, a singleton is a class of which there can be at most one instance. However, sometimes the name is also used for classes of which there should be at most one instance.
Note that even if there can be at most one instance, it still isn't specified how that is enforced. It may be at compile time (preferably), or it may be at run time.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 22nd, 2010, 08:03 AM
#6
Re: how CWinApp realize the singleton pattern?
I have no idea, what happens in Release mode, or in Debug mode, if assertion is ignored. Undefined behavior or exception. It is not difficult to test.
-
July 23rd, 2010, 01:50 AM
#7
Re: how CWinApp realize the singleton pattern?
Is it that CWinApp does not strictly apply singleton pattern?
As Alex and Arjay said, CWinApp does not implement the singleton pattern. It bears the requirement that you don't create a second instance. Period.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|