Factory Method pattern C# with Example



Factory Method pattern C# with Example

Factory Method is one of creational design patterns. It is used to deal with the problem of creating objects without 
specifying exact result type. This document will teach you how to use Factory Method DP properly. 
Let me explain the idea of it to you on a simple example. Imagine you're working in a factory that produces three 
types of devices - Ammeter, Voltmeter and resistance meter. You are writing a program for a central computer that 
will create selected device, but you don't know final decision of your boss on what to produce. 
Let's create an interface IDevice with some common functions that all devices have: 
public interface IDevice 
{ 
int Measure(); 
void TurnOff(); 
void TurnOn(); 
} 
Now, we can create classes that represent our devices. Those classes must implement IDevice interface: 
public class AmMeter : IDevice 
{ 
private Random r = null; 
public AmMeter() 
{ 
 

r = new Random(); 
} 
public int Measure() { return r.Next(-25, 60); } 
public void TurnOff() { Console.WriteLine("AmMeter flashes lights saying good bye!"); } 
public void TurnOn() { Console.WriteLine("AmMeter turns on..."); } 
} 
public class OhmMeter : IDevice 
{ 
private Random r = null; 
public OhmMeter() 
{ 
r = new Random(); 
} 
public int Measure() { return r.Next(0, 1000000); } 
public void TurnOff() { Console.WriteLine("OhmMeter flashes lights saying good bye!"); } 
public void TurnOn() { Console.WriteLine("OhmMeter turns on..."); } 
} 
public class VoltMeter : IDevice 
{ 
private Random r = null; 
public VoltMeter() 
{ 
r = new Random(); 
} 
public int Measure() { return r.Next(-230, 230); } 
public void TurnOff() { Console.WriteLine("VoltMeter flashes lights saying good bye!"); } 
public void TurnOn() { Console.WriteLine("VoltMeter turns on..."); } 
} 
Now we have to define factory method. Let's create DeviceFactory class with static method inside: 
public enum Device 
{ 
AM, 
VOLT, 
OHM 
} 
public class DeviceFactory 
{ 
public static IDevice CreateDevice(Device d) 
{ 
switch(d) 
{ 
case Device.AM: return new AmMeter(); 
case Device.VOLT: return new VoltMeter(); 
case Device.OHM: return new OhmMeter(); 
default: return new AmMeter(); 
} 
} 
} 
Great! Let's test our code: 
public class Program 
{ 
static void Main(string[] args) 
{ 
IDevice device = DeviceFactory.CreateDevice(Device.AM); 
device.TurnOn(); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
 

Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
device.TurnOff(); 
Console.WriteLine(); 
device = DeviceFactory.CreateDevice(Device.VOLT); 
device.TurnOn(); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
device.TurnOff(); 
Console.WriteLine(); 
device = DeviceFactory.CreateDevice(Device.OHM); 
device.TurnOn(); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
Console.WriteLine(device.Measure()); 
device.TurnOff(); 
Console.WriteLine(); 
} 
} 
This is the example output you might see after running this code: 
AmMeter turns on... 
36 
6 
33 
43 
24 
AmMeter flashes lights saying good bye! 
VoltMeter turns on... 
102 
-61 
85 
138 
36 
VoltMeter flashes lights saying good bye! 
OhmMeter turns on... 
 

723828 
368536 
685412 
800266 
578595 
OhmMeter flashes lights saying good bye! 

0 Comment's

Comment Form

Submit Comment