Look at the application of Design Pattern (Six) from a ConnectionPool implementation

zhaozj2021-02-16  52

This kind of RESOURCESCOLLEctor also has a bit of a little in the United States, that is, we expose our details of Java.util.Collection in ResourceManImp to expose to ResourcesCollection. If a resourceman's implementation doesn't want to use Collection, it is not easy.

You can say, anyway, Collection is an interface, we can let the resourceman's implementation written an adapter, don't you?

Yup. It is theoretically. However, the dead Sun defines too many methods in Collection. And some ways are Optional. That is, if an implementation of a ResourcesCollector uses an OPTIONAL method, the compiler will not report an error. If the container used by a resourceman implemented does not support these optional methods, the compiler will not report an error. However, when you put it up, pass! OperationNotsupportedException!

Hey, to define a requirement to meet all possible ResourcesCollecor (such as order access, random access, etc.), but also to make all possible resourceman's interfaces of the container that you can implement is too difficult!

I think this requirement should be ineffective. Because, there is not enough coupling between Resourceman and ResourcesCollector. Conceptually, Resourceman must choose a container that meets the requirements of ResourcesCollector. This is the coupling of natural definitions between them. Therefore, there is no perfect solution that is unable to relieve their coupling is not worth surprising.

Ok, ResourcesCollector is just a small child module for the Resourceman function. How to organize them does not affect our entire Pooling framework system.

Below, let me sumize the structure of our Pooling framework system:

1. Reusable pooling logic:

Resourceman: Implementing pure pooling algorithm logic, maintains the maximum transparency for specific resources.

ResourceFactory: Used to encapsulate resource generation logic, need to combine resourceman.

ResourcesCollector: Used to encapsulate the resource recycling, need to combine resourceman.

ResourceCollector: Used to encapsulate individual resource recycling, need to combine the ResourcesCollector.

2.

PooledConnection: Package the Connection object, making it coordinated with pool.

3.

Resourceman2ConnectionPool: Similar to Connectionman2ConnectionPool, responsible for using PooledConnection to convert a resourceman to the user's opaque users to confine the user's secure transparent ConnectionPool.

To achieve a ConnectionPool,

1. We must first find a suitable Pooling logic, which is an implementation class of Resourceman.

2. Then, depending on the characteristics of the resource, decide which ResourcesCollector used to use, for ConnectionPool, use LinearResourcesCollector for Thread, using NopResourcesCollector.

3. Because the Connection resource needs to be released for a single resource, handle the ConnectionCollector to linearResourceCollector 4. Construct the object instance of Resourceman.

5. Use the Resourceman2ConnectionPool class to convert ResourceMan to ConnectionPool. Resourceman2ConnectionPool will be encapsulated using PooledConnection.

In the above steps, there are some commonality to extract. Although users buy ResourceMan and ResourceConnection , the implementationPool is fixed, and Resourceman2ConnectionPool is also fixed.

If we abstract this process, can it be more simple to have our user interface? Ideally, the interface should be like this:

Public Interface ResourcemanFactory {

Public ResourceMan getresourceman (ResourceFactory factory, resourcescollector collector);

}

Public class connectionpoolfactory {

Public Static ConnectionPool

GetConnectionPool (ResourceManFactory MF, ResourceFactory

Return Mf.GetResourceman (RF,

LinearResourceScollector .instance.

ConnectionCollector.Instance ()

)

}

}

In this way, constructing people of ConnectionPool, just choose the right resourcemanfactory and resourcefactory , there are anything else. Can't just simple.

People who implement the POOLING algorithm only need to study his algorithms, and others don't have to manage. Can't just simple.

People who implement ResourceFactory , just pay attention to how to connect to the database, don't have anything else. Can't just simple.

Some bridges and resource classes have been written. Do not take them.

People who implement encapsulated connection or other resource, just pay attention to the interface and semantics of that resource, make the adjustment of the Pool logic, and anything else. Can't just simple.

转载请注明原文地址:https://www.9cbs.com/read-26044.html

New Post(0)