Factory Design PatternUse the factory design pattern. Understand how a factory returns objects for use in a program.
This page was last reviewed on Mar 15, 2022.
Factory pattern. A factory creates objects. A factory pattern is a reusable block of code that creates objects in a predictable way. We implement the factory design pattern in a C# program.
Pattern details. With this pattern, we develop an abstraction that isolates the logic for determining which type of class to create. This helps us control what types we create.
Protection Proxy
Example. The factory design pattern relies on a type hierarchy. The classes must all implement an interface or derive from a base class. We use an abstract class as the base.
Here The Manager, Clerk and Programmer classes derive from Position. The base class is shared.
Detail This method takes a value and instantiate a class based on that value. It translates integers to objects with a switch statement.
Detail Because Manager, Clerk, and Programmer all derive from the same abstract class, the return type Position can be used.
Detail An implicit cast automatically casts the Manager, Clerk and Programmer to Position references.
Detail We use the Get method with the values 0, 1, 2, and 3. We show that the appropriate type of class was instantiated for each integer.
using System; class Program { abstract class Position { public abstract string Title { get; } } class Manager : Position { public override string Title { get { return "Manager"; } } } class Clerk : Position { public override string Title { get { return "Clerk"; } } } class Programmer : Position { public override string Title { get { return "Programmer"; } } } static class Factory { /// <summary> /// Decides which class to instantiate. /// </summary> public static Position Get(int id) { switch (id) { case 0: return new Manager(); case 1: case 2: return new Clerk(); case 3: default: return new Programmer(); } } } static void Main() { for (int i = 0; i <= 3; i++) { var position = Factory.Get(i); Console.WriteLine("Where id = {0}, position = {1} ", i, position.Title); } } }
Where id = 0, position = Manager Where id = 1, position = Clerk Where id = 2, position = Clerk Where id = 3, position = Programmer
Discussion. Imagine you have a system that needs to create objects in many different places. Suppose the system has integers and you want objects for those integers.
And The factory pattern is ideal for this usage. You can use the Factory type to handle object creation in a consistent way.
Summary. We use the factory design pattern to instantiate objects based on another data type such as integers. Factories make it easier to change which objects need to be created.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 15, 2022 (edit link).
© 2007-2023 Sam Allen.