mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +00:00
docs: improve factory
This commit is contained in:
+33
-28
@@ -3,32 +3,27 @@ title: Factory
|
||||
category: Creational
|
||||
language: en
|
||||
tag:
|
||||
- Gang of Four
|
||||
- Gang of Four
|
||||
- Instantiation
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Simple Factory
|
||||
* Static Factory Method
|
||||
* Factory Method
|
||||
|
||||
## Intent
|
||||
|
||||
Providing a static method encapsulated in a class called the factory, to hide the implementation
|
||||
logic and make client code focus on usage rather than initializing new objects.
|
||||
The Factory design pattern is intended to define an interface for creating an object, but allows subclasses to alter the type of objects that will be created. This pattern is particularly useful when the creation process involves complexity.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real-world example
|
||||
|
||||
> Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both
|
||||
> gold and copper coins and switching between them must be possible without modifying the existing
|
||||
> source code. The factory pattern makes it possible by providing a static construction method which
|
||||
> can be called with relevant parameters.
|
||||
> Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both gold and copper coins and switching between them must be possible without modifying the existing source code. The factory pattern makes it possible by providing a static construction method which can be called with relevant parameters.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> Factory is an object for creating other objects – formally a factory is a function or method that
|
||||
> returns objects of a varying prototype or class.
|
||||
> Factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
@@ -74,8 +69,7 @@ public enum CoinType {
|
||||
}
|
||||
```
|
||||
|
||||
Then we have the static method `getCoin` to create coin objects encapsulated in the factory class
|
||||
`CoinFactory`.
|
||||
Then we have the static method `getCoin` to create coin objects encapsulated in the factory class `CoinFactory`.
|
||||
|
||||
```java
|
||||
public class CoinFactory {
|
||||
@@ -110,17 +104,9 @@ This is a gold coin.
|
||||
|
||||
## Applicability
|
||||
|
||||
Use the factory pattern when you only care about the creation of an object, not how to create
|
||||
and manage it.
|
||||
|
||||
Pros
|
||||
|
||||
* Allows keeping all objects creation in one place and avoid of spreading 'new' keyword across codebase.
|
||||
* Allows to write loosely coupled code. Some of its main advantages include better testability, easy-to-understand code, swappable components, scalability and isolated features.
|
||||
|
||||
Cons
|
||||
|
||||
* The code becomes more complicated than it should be.
|
||||
* Use the Factory pattern in Java when the class doesn't know beforehand the exact types and dependencies of the objects it needs to create.
|
||||
* When a method returns one of several possible classes that share a common super class and wants to encapsulate the logic of which object to create.
|
||||
* The pattern is commonly used when designing frameworks or libraries to give the best flexibility and isolation from concrete class types.
|
||||
|
||||
## Known uses
|
||||
|
||||
@@ -131,9 +117,28 @@ Cons
|
||||
* [java.net.URLStreamHandlerFactory#createURLStreamHandler(String)](https://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html) (returns different singleton objects, depending on a protocol)
|
||||
* [java.util.EnumSet#of()](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of(E))
|
||||
* [javax.xml.bind.JAXBContext#createMarshaller()](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller--) and other similar methods.
|
||||
* JavaFX uses Factory patterns for creating various UI controls tailored to the specifics of the user's environment.
|
||||
|
||||
## Related patterns
|
||||
## Consequences
|
||||
|
||||
* [Factory Method](https://java-design-patterns.com/patterns/factory-method/)
|
||||
* [Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
|
||||
* [Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/)
|
||||
Benefits:
|
||||
|
||||
* Reduces coupling between the implementation of an application and the classes it uses.
|
||||
* Supports the [Open/Closed Principle](https://java-design-patterns.com/principles/#open-closed-principle), as the system can introduce new types without changing existing code.
|
||||
|
||||
Trade-offs:
|
||||
|
||||
* The code can become more complicated due to the introduction of multiple additional classes.
|
||||
* Overuse can make the code less readable if the underlying complexity of the object creation is low or unnecessary.
|
||||
|
||||
## Related Patterns
|
||||
|
||||
* [Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/): Can be considered a kind of Factory that works with groups of products.
|
||||
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Often used in conjunction with Factory to ensure that a class has only one instance.
|
||||
* [Builder](https://java-design-patterns.com/patterns/builder/): Separates the construction of a complex object from its representation, similar to how factories manage instantiation.
|
||||
* [Factory Kit](https://java-design-patterns.com/patterns/factory-kit/): Is a factory of immutable content with separated builder and factory interfaces.
|
||||
|
||||
## Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0Rk5y)
|
||||
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)
|
||||
|
||||
@@ -31,7 +31,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* create and return objects of varying classes, in order to hide the implementation logic
|
||||
* and makes client code focus on usage rather than objects initialization and management.
|
||||
*
|
||||
* <p>In this example an alchemist manufactures coins. CoinFactory is the factory class and it
|
||||
* <p>In this example an alchemist manufactures coins. CoinFactory is the factory class, and it
|
||||
* provides a static method to create different types of coins.
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user