You can refer to wikipedia , but the basic idea of most design patterns is to introduce some abstraction to achieve better maintainability and / or reuse. Factory method template is no exception, what it does is to abstract the complexity of creating from code.
For a simple case, it makes no sense to use the Factory template, just new enough. But when you need more flexibility or functionality, this template can help.
For example, if a new instance is not required, a static Factory valueOf(boolean) usually better than new Bealean(boolean) because it avoids creating unnecessary objects. Factory method template is also known as Virtual Constructor . As you know, polymorphism is one of the key features of OOP, but the constructor cannot be polymorphic, this drawback can be overcome using the Factory method template.
In essence, instantiating an object directly (usually via new ) is hardly a concrete implementation, while the Factory method template escapes a mutable implementation using a stable interface > (not limited to interface in Java), pushing the object creation logic behind some abstraction so that provide more convenient and reusable code.
As a final word, to fully understand the benefits of the Factory method template and other design patterns, you need to understand the essence of OOP, including data abstraction , polymorphic abstraction, and SOLID .
Hui Zheng Jan 29 '13 at 4:10 2013-01-29 04:10
source share