Search code examples
asp.netvb.netdesign-patternsfactory-pattern

When should the factory pattern be used?


Just as the title asks, when should a trigger in your head go off signifying "Aha! I should use the factory pattern here!"? I find these moments will occur with many other design patterns, but never do I stop myself and think about this pattern.


Solution

  • The factory pattern is best employed in situations where you want to encapsulate the instantiation of a group of objects inside a method.

    In other words, if you have a group of objects that all inherit from the same base class or all implement the same interface that would be an instance where you would want to use the factory pattern (that would be the "pattern" you would look for).