Design Model learning-enjoy the Yuan Model

Source: Internet
Author: User

GOOD: Use the sharing technology to effectively support a large number of fine-grained objects (for C ++, it is to share a memory block, and the Object Pointer Points to the same place ).

If an application uses a large number of objects, and these objects cause a large storage overhead, you should consider using them.

In addition, most states of objects can be external States. If you delete an object's external state, you can replace multiple groups of objects with fewer shared objects. In this case, you can consider using the shared object.

 

[Html]
# Include <iostream>
# Include <string>
# Include <vector>
Using namespace std;
 
// Abstract website
Class WebSite
{
Public:
Virtual void Use () = 0;
};
 
// Specific shared website
Class ConcreteWebSite: public WebSite
{
Private:
String name;
Public:
ConcreteWebSite (string strName)
{
Name = strName;
}
Virtual void Use ()
{
Cout <"website category:" <name <endl;
}
};
 
// Non-shared website
Class untitled WebSite: public WebSite
{
Private:
String name;
Public:
Unencrypted website (string strName)
{
Name = strName;
}
Virtual void Use ()
{
Cout <"websites not shared:" <name <endl;
}
};
 
// WebSite factory class, used to store shared WebSite objects
Class WebFactory
{
Private:
Vector <WebSite *> websites;
Public:
WebSite * GetWeb ()
{
Vector <WebSite *>: iterator p = websites. begin ();
Return * p;
}
WebFactory ()
{
Websites. push_back (new ConcreteWebSite ("test "));
}
};
 
// Client
Int main ()
{
WebFactory * f = new WebFactory ();
WebSite * ws = f-> GetWeb ();
Ws-> Use ();
 
WebSite * ws2 = f-> GetWeb ();
Ws2-> Use ();
 
// Non-shared class
WebSite * ws3 = new untitled WebSite ("test ");
Ws3-> Use ();
Return 0;
}
 
 

 

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.