Purpose: Use the prototype example to specify the object type, and copy the prototype to create a new object.
Applicability:
When the instantiated class is specified at runtime
To avoid creating a factory class parallel to the product class
When a class can only have one of several combinations of different states
Example:
ExampleCode:
# Pragma once
// Sun Wukong: one change, two changes, four changes...
Class cmonkeyking
{
Public:
Cmonkeyking (int
I );
Public:
~ Cmonkeyking (void );
Public:
Void
Display ()
{
If (m_ I = 0)
{
_ Tprintf (_ T ("stone monkey/N "));
}
If (m_ I
= 1)
{
_ Tprintf (_ T ("qitiandasheng/N "));
}
Else
{
_ Tprintf (_ T ("Other
Status/N "));
}
}
Public:
Cmonkeyking *
Clone ()
{
Return new cmonkeyking (* This );
}
PRIVATE:
Int m_ I;
};
Cmonkeyking ** kings = new cmonkeyking * [1, 100];
Cmonkeyking
Stonking (0 );
Cmonkeyking qitianking (1 );
Cmonkeyking
Otherkin (-1 );
For (INT I = 0; I <100; I ++)
{
If (I % 3 = 0)
{
Kings [I] = stonking. Clone ();
}
Else if (I % 3 = 1)
{
Kings [I] =
Qitianking. Clone ();
}
Else
{
Kings [I] = otherkin. Clone ();
}
}
// Cmonkeyking * ptemp =
* Kings;
For (Int J = 0; j <100; j ++)
{
Kings [J]-> display ();
}