Simple Factory Pattern, Factory Method,and Abstract Factory Pattern | Hello World
This is a simple factory method, but it can get more complex. The above example does serve the function alright but it does not satisfy the open-close principle.
So there is another method called Class Registration that will satisfy the open-close principle – we do not need to change the factory class when we want to add new product class. The idea is have a register method that can register the class (using reflection or creator method enforced in product interface). But usually the factory method is a more preferable solution:
Factory Method:
The following is quoted from Gang of four:
"Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses"
Read full article from Simple Factory Pattern, Factory Method,and Abstract Factory Pattern | Hello World
No comments:
Post a Comment