Design Pattern learning-prototype (prototype)

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.