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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s