Design Patterns
Resource factory v1.0
Resource Factory - Object creation type
1. Name Resource Factory
2. Version 1.0
3. Intent to create different objects with a unified approach, not to care about how this object is created, where is it.
4. In the motive application, too many resource objects are used, and these resource objects are not the same, such as creating an EJB remote interface object in the Java Bean, create database connections, using these resource objects, without having to know they are How to create it. Some resource objects can be reserved after using it, so that they can return this resource directly when you create the same type of resources.
5. Applicability In the following, the implementation of this mode is implemented independent of its user. An application system uses a variety of different resources.
6. Structure This mode is shown in the figure below:
7. Participants ResourceManager Create (ResourceDriver ConcreteDriver Defines an Explorer, all resource objects are created by this manager.
ResourcePool resource pool.
ResourceDriver createFactory () Defines a driver interface that creates a resource object.
ResourceFactory CreateResource () Defines a factory interface that creates a resource object.
ConcreteDriver createFactory () implements the driver for creating a specific resource object. Implement the ResourceDriver interface.
ConcreteFactory Createresource () implements factory objects for creating specific resources. Implement the ResourceFactory interface.
ConcreteResource is created specific resource objects.
Client Application System Client, Create a resource object only using the resourceManager's CREATE method.
8. When you work together, you first create an instance of a ResourceManager class, an instance of a ConcreteDriver class, create a specific resource object through the ResourceManager's CREATE method, and the Concretedriveer instance will use the parameters of the CREATE method, the following is the entire process of creating a resource object. Create a resource object directly through the resource pool
The client program creates a resource object via the ResourceManager's CREATE method. If the resource pool object management is used, after each object is used, you must return this object to the ResourceManager with the RETURN method.
9. Effects Resource Factory mode There are some advantages and disadvantages: 1) It separates the specific classes. It has the characteristics of the Abstract Factory mode. The resource factory package creates resource object responsibilities and processes, it separates customers with resource objects, customers can Manipulate examples through the abstract interface of the resource object. 2) Support to create different types of resources only need to implement ResourceDriver for each type of resource, and the ResourceFactory interface can create a new resource object. 3) A type of resource implementation of multiple factories by the resourceDriver implementation to decide to use the factory to create resources. Customers don't have to care for using that plant to create resources. 4) Supporting pool management mode Some resource objects can be reused, such as database connections, which are more consumed, and resource pool management can be used to multiplex objects. 5) Returns No resources If you use resource pool management mode, you must return it to the resource pool after using this resource. 10. Realization Some techniques for implementing a Resource Factory mode: 1) Designing ResourceFactory as a single type of resource only requires a ConcreteFactory instance, so the factory's implementation is best used. Singleton mode in standard design mode. 2) Creating the creation of a factory factory object through ResourceDriver is resourceDriver to complete, so you can design multiple factories for the same type of resource, which can create resources more flexible. 3) The ResourcePool resource pool is determined by a specific factory for a certain type of resource using a resource pool. You can use global resource pools, which is all resources sharing a resource pool, or you can create a resource pool for each type of resource, so you must create a universal resource pool interface. The ResourcePool is generally designed as a single piece.
11. Code Example The following is an example of a simple Java version using Resource Factory to create an object: fto.Pattern.ResourceFactory.zip
12. Applications in the application system, all resources used can be created, such as establishing a database connection, and the Lookup EJB remote interface. Use this mode in the development of game development.
13. Related Mode Abstract Factory (Abstract Factory), Singleton.
14. Download 1) PDF Format Document 2) Java version code example
Copyright (c) 1995-2004, The Free Think Organizing
Author: wjian, email:
Wjian@free-think.org
http://www.free-think.org