Design Patterns: Factory

Please see my other Design Pattern articles.

Providing flexible object-instantiation.

0-factorypattern

In this example, I’ll illustrate a method by which a SalesAssociate object may be instantiated that is more flexible than the traditional way – using a constructor.
Instead I’ll implement a factory design pattern to create the instance. There are two advantages to this approach:

  • You can return a null object, which is impossible to do with a constructor.
  • If there are a lot of different ways to create an object, it gives you the chance to provide more meaningful function names.
    o For example, to create an instance, call CreateSalesAssociate(). Also, it’s easy to provide other methods by which objects may be created.

Factory Pattern code

1-factorypattern

Leave a comment