Design Pattern: Factory Pattern

How to create a factory pattern?

Define an interface for creating an object, but let subclasses decide which class to instantiate.

When to use factory pattern?

  • Use it when a class cannot predict which class of objects that it must create.
  • Use it when you want a subclass to specify the objects it must create
  • Use it when you want a class to delegate responsibility to a helper subclasses

Conclusion

You need to understand that classes are chosen at runtime

Conclusion

When a method returns one of many possible classes that share a common super class.

Leave a comment

Design a site like this with WordPress.com
Get started