Factory Method pattern

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:

publicinterfaceIDevice{intMeasure();voidTurnOff();voidTurnOn();}

Now, we can create classes that represent our devices. Those classes must implement IDevice interface: