Chapter 16. Factory Method

When you develop a class, you usually provide class constructors to let clients of your class instantiate it. There are times, though, when a client that needs an object does not or should not know which of several possible classes to instantiate. The FACTORY METHOD pattern lets a class developer define the interface for creating an object while retaining control of which class to instantiate.

A Classic Example—Enumerators

Enumerators in the .NET FCL provide a good example of FACTORY METHOD. The overall design for supporting enumeration is the subject of the ITERATOR pattern (see Chapter 28, “Iterator”). But, GetEnumerator() methods themselves are good examples of the FACTORY METHOD pattern. These methods isolate the caller ...

Get Design Patterns in C# now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.