31.2 Object Pool Mode
31.2.1 definitions and class diagrams
(1) Definition: An object pool is a design pattern that can be reused to share certain scarce or costly resources by managing a finite object.
(2) class diagram:
31.2.2 extended interface for object pooling
(1) Level two object pooling method: Free object pool and used object pool
(2) Use wrapper mode to wrap reusable objects so that some properties (such as idle time) are attached
class wrapper{private: int// start using time int// Start idle time // Other attached properties // Reusable object Pointer };
(3) Free object pool: Storage of idle reusable objects that are not currently allocated
①wrapper* get (string model, string strategy);//Get a Free object (the policy here refers to the policy that allocates the idle object, such as maximum idle time allocation or random allocation, etc.)
②void Add (String model, wrapper* object);//Join a free object
(4) Self-used object pool: Holds the connection that is being used by the client.
①void Close (wrapper* object); To remove an object from its own pool of objects
②void put (wrapper* object); Add a Reusable Object
31.2.3 establishment and extinction of reusable objects
(1) Reuse object creation: wrapper* Create (string model);//Note is the private property. Model is the type of the reusable object. Since the program does not know what reuse object to establish, the actual object created by bridge is forwarded to objectbridge->create (model);
wrapper* Create (string model) { wrapper* ret =NULL; if (Objectnumber < maxobjectnumber) { Objectnumber++ ; // Create a new wrapped object New // transferred } return ret;}
(2) The extinction of the reused object: void Remove (wrapper*);//delete from the object pool, that is, physical deletion. Private
void Remove (wrapper* o) { objectnumber--; Objectbridge->remove (o.object);
(3) Gets the reused object: When a reusable object is required, the client calls the Acquirereusable method. If the pool is empty, then the Acquirereusable method creates a reusable object (if it can), otherwise waits to know that a reusable object is returned to the collection.
31.2.4 Manager Object the management policy
(1) The generation strategy of the object
①void setinitpoolsize ();//defines the default number of objects when the object pool is initialized
②void createbystrategy ();//What policies are used to create idle objects
(2) Object Recycling strategy
① client obtains a reusable object through acquiredreusable, and then proactively returns the reusable object after use.
② but it is not assumed that after the client has finished using the object, the object is definitely returned, and it is sometimes possible that the client program cannot return the object because of a program exception or deadlock. Set the Reuse object maximum usage time: void Setmaxusetime (Long), which is forced to recycle when time-out occurs. When the maximum rating is exceeded (recycle is checked by time thread timing).
(3) The extinction strategy of the object
① when the number of reusable objects exceeds the rated value, the system returns the resources of the current idle reuse object to reduce the burden on the system.
② policy: If the idle objects are all recycled or partially reclaimed at once.
"Programming Experiment" simulates a database connection pool for implementation
//new design mode--Object pool mode//Scenario: Database connection Pool#include <iostream>#include<string>#include<vector>using namespaceStd;typedefvoidObject;//The actual things that the user needs to implement this interfaceclassiconnection{ Public: Virtualobject* getconnection () =0; Virtual voidSetConnection (object* obj) =0;};//implementation class: What the user really needs, such as a database connectionclassConnection: Publiciconnection{Object*Conn; Public: Object* Getconnection () {returnConn;} voidSetConnection (object*obj) {Conn=obj; }};//what is placed in the pool (with the state and what the user actually needs)classpoolitem{Private: BOOLM_isuse; Iconnection*Conn; Public: Poolitem (iconnection*conn) { This->conn =Conn; M_isuse=false; } BOOLIsuse () {returnM_isuse;} voidSetisuse (BOOLvalue) {M_isuse=value; } iconnection*getconnection () {returnConn; }};//Pool Management Objectsclasspoolmanager{Private: Vector<PoolItem*>items; Public: //put things in the pool (you can consider syncing when you have multiple threads) voidAdd (iconnection*conn) {Items.push_back (NewPoolitem (conn)); } //Releasing objects (synchronization is considered when multi-threading) voidRelease (iconnection*conn) {Vector<poolitem*>::iterator iter =Items.begin (); while(ITER! =Items.end ()) {Poolitem& item = * (*ITER); if(item.getconnection () = =conn) {Item.setisuse (false); Break; } ++ITER; } } //get the objects in the pool (synchronization is considered when multi-threading)iconnection*Get() {iconnection* ret =NULL; Vector<poolitem*>::iterator iter =Items.begin (); while(ITER! =Items.end ()) {Poolitem& item = * (*ITER); if(!Item.isuse ()) {Item.setisuse (true); RET=item.getconnection (); Break; } ++ITER; } returnret; }};//Connection Pool objects (pools that users really need to care about)classconnectionpool{Private: StaticPoolmanager Manager; Public: //Bulk Add Connection Objects voidAddconnections (intcount) { for(intI=0; i<count; i++) {Manager.add (NewConnection ()); } } //Get the Connection object Staticiconnection*getconnection () {returnManager.Get(); } //Release Connection Static voidRelease (iconnection*conn) {manager.release (conn); } //Clear All Connections Static voidclearconnection () {}}; Poolmanager Connectionpool::manager;intMain () {return 0;};
31.2.5 Summary
(1) The pattern is defined by defining 6 interfaces and two abstract extension objects (Realobjectcreator and Poolmanager). By implementing Realobjectcreator abstract Objects , you can extend the entire object pool pattern to new application areas .
(2) by implementing Poolmnanger abstract Objects , different management strategies can be implemented for different objects to accomplish the establishment, extinction and management of reusable objects.
31st. New design Pattern (2)