Simple factory pattern

The factory pattern is used to encapsulate the logic to instantiate objects referred through a common interface. New classes can be added with minimal changes.

The implementation of a simple factory is described in the following class diagram:

The SimpleFactory class implements the code to instantiate ConcreteProduct1 and ConcreteProduct2. When the client needs an object, it calls the createProduct() method of the SimpleFactory with the parameter indicating the type of object it requires. SimpleFactory instantiates the corresponding concrete product and returns it. The returned product is cast to the base class type ...

Get Design Patterns and Best Practices in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.