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