Java Design Pattern Factory
This document summarizes the Factory Pattern implemented in Java.
1. Java Factory Pattern
Factory Pattern is a Pattern used when you want to hide the object creation process from the outside. Factory Pattern has three Patterns: Simple Factory Pattern, Factory Method Pattern, and Abstract Factory Pattern.
| |
[Code 1] shows the Product Class used as an example for introducing Factory Pattern. The Product Class performs the role of Abstract Class, and the Book and Phone Classes inherit and implement the Product Class.
1.1. Simple Factory Pattern
| |
Simple Factory Pattern means a Factory Pattern that can be implemented simply, as the name suggests. [Code 2] shows an example of Simple Factory Pattern through the SimpleProductFactory Class. You can see that the getProduct() Method of SimpleProductFactory creates different Product objects depending on the type. Simple implementation is the biggest advantage, but it has the disadvantage that the Code of the SimpleProductFactory Class must be changed each time a Product type is added.
1.2. Factory Method Pattern
| |
Factory Method Pattern is a Pattern that compensates for the disadvantages of Simple Factory. It is a Pattern that creates dedicated Factories that create objects of a single Type by inheriting Factory Classes. [Code 3] shows the Factory Method Pattern. You can see a BookFactory Factory Class that creates only Book objects and a PhoneFactory Factory Class that creates only Phone objects by inheriting the ProductFactory Class. It has the advantage that existing Factory-related Code does not need to be modified even if Product types are added.
1.3. Abstract Factory Pattern
| |
Abstract Factory Pattern is a Pattern that can create objects of various Types depending on the Factory object being injected. [Code 4] shows the Abstract Factory Pattern. You can see that various Types of Products can be created depending on the Factory Class injected into the AbstractProductFactory Class.
2. References
- https://medium.com/bitmountn/factory-vs-factory-method-vs-abstract-factory-c3adaeb5ac9a
- https://www.codeproject.com/Articles/716413/Factory-Method-Pattern-vs-Abstract-Factory-Pattern
- https://stackoverflow.com/questions/5739611/what-are-the-differences-between-abstract-factory-and-factory-design-patterns
- https://blog.seotory.com/post/2016/08/java-abstract-factory-pattern
- https://blog.seotory.com/post/2016/08/java-factory-pattern