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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.