O'Reilly logo

Design Patterns in C# by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required