Some of people recommend 开发者_如何学编程the factory pattern in java. I am not aware of that. What is the main objective to use the factory pattern in java and Give me your suggestion which type of pattern is useful?
The objective depends on the type of factory pattern, e.g. Abstract Factory: "Provide an interface for creating families of related or dependent objects without specifying their concrete classes." from Design Patterns
Most factory patterns allow you to loosen the coupling between the creation request and the class of the object created. This allows you to reserve the right to change your mind.
This approach is commonly used to enable unit testing with stub or mock objects.
There are 2 well know factory patterns:
- Factory Method Pattern.
- Abstract Factory Pattern.
Factory method pattern basically "deals with the problem of creating objects (products) without specifying the exact class of object that will be created." while Abstract Factory pattern "provides a way to encapsulate a group of individual factories that have a common theme."
Factory method is used for Object creation, basically creating a factory interface of methods which subclasses can derive and implement the method to create an object. This allows that encapsulation of not worrying who made the object but you have the object done once the method is excuted.
Abstract Factory pattern encapsulates Factories together who have the same
objective functions to fulfill a task. E.g. you may have a Button GUI and have factory such as WindowFactory
, LinuxFactory
, AppleFactory
that can create those buttons. Wrap these factory into an Abstract Factory such that, providing the OS, it will return a OS specific factory to create the Button
.
Hope this is clear. Sorry for not using proper english constructive sentencing.
The factory pattern allows you to decouple (reduce dependence) between a class and it's consumer. Rather than having a class create it's dependent objects using a constructor, you supply it with a factory that knows how to create the object and it uses the factory to create the dependent objects. Using interfaces in conjunction with the factory provides even greater decoupling because it allows the factory to be changed to produce different concrete objects that implement the interface without having to change the consuming class. It's frequently used in unit testing in this way where you provide a mock implementation of the dependency instead of a real implementation for use in a test. Generally, though, factories are useful whenever you have potentially multiple variants of a class implementing an interface that could be provided depending on the state or configuration of the program.
The major benefit is that you don't need to rewrite/rebuild/redeploy your code whenever you'd like to change the concrete implementation used in the code. With a Factory you only need to change the external configuration setting (properties file?) which implementation class to use and/or to change the external JAR file containing the implementation.
You see this good back in several Java API's which offers possibities to create vendor-specific implementations, such as JDBC, JAXP, etc.
You'd use the factory pattern (in one of its many forms) whenever:
- Object creation is too complex for letting the object be responsible for itself.
- Object creation requires interaction/knowledge of other objects and their creation
- Object creation is dependent on global state/environment variables
- The requester of such an object should not be bound to a specific implementation but rather to an interface (the case for Abstract Factory)
精彩评论