Last active
March 1, 2018 16:44
-
-
Save wesleyduff/341e1bee891d4a94ae79919706bec43d to your computer and use it in GitHub Desktop.
C# Factory Pattern Example : Based on location, you can have an home address with longitude and latitude and a business address with a longitude and latitude. DotNet Fiddle : https://dotnetfiddle.net/Ti4UBz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Factory | |
{ | |
// Location class | |
public class Location | |
{ | |
public double Latitude {get; set;} | |
public double Longitude {get; set;} | |
} | |
// the 'Product' interface : in this example it is Locaton. So Location will be the product | |
public interface ILocationFactory | |
{ | |
Location GetLocation(); | |
void WriteLocation(Location location); | |
} | |
// A 'Concrete Product' class | |
public class Home : ILocationFactory | |
{ | |
public Location GetLocation() | |
{ | |
return new Location{Latitude = 40.741895, Longitude = -73.989308 }; | |
} | |
public void WriteLocation(Location location) | |
{ | |
Console.WriteLine("My Home location resides at latitude: " + location.Latitude + " and longitude: " + location.Longitude + " ."); | |
} | |
} | |
// A 'Concrete Product' class | |
public class Business : ILocationFactory | |
{ | |
public Location GetLocation() | |
{ | |
return new Location{ Latitude = 40.65133155546328, Longitude = -74.26483815644531 }; | |
} | |
public void WriteLocation(Location location) | |
{ | |
Console.WriteLine("My Business location resides at latitude: " + location.Latitude + " and longitude: " + location.Longitude + " ."); | |
} | |
} | |
// A Creator Abstract Class | |
public abstract class LocationFactory | |
{ | |
public abstract ILocationFactory GetPlace(string Place); | |
} | |
// A 'Concrete Creator' class | |
public class ConcreteLocationFactory : LocationFactory | |
{ | |
public override ILocationFactory GetPlace(string Place) | |
{ | |
switch(Place) | |
{ | |
case "Home": //could be an enum that represends Home class | |
return new Home(); | |
case "Business": | |
return new Business(); | |
default: | |
throw new ApplicationException(string.Format("Location '{0}' cannot be created", Place)); | |
} | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
LocationFactory factory = new ConcreteLocationFactory(); | |
ILocationFactory home = factory.GetPlace("Home"); | |
Location locationOfHome = home.GetLocation(); | |
home.WriteLocation(locationOfHome); | |
ILocationFactory business = factory.GetPlace("Business"); | |
Location locationOfBusiness = business.GetLocation(); | |
business.WriteLocation(locationOfBusiness); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Factory Pattern
-- is an Interface that is used by a concrete product
-- implements the Product Interface
-- Contains other methods and properties that maybe needed by the instantiated object.
-- contains properties and methods needed to orchestrate and deliver the correct object that is requested.
-- Implements the creator factory
-- overrides the methods needed to return the correct requested object
Program
-- call the factory to return you an instance of your Concrete Product
When to use the factory pattern
it's good for something where you have specialized instances of a class. You may need two methods to build an object, but you don't want to have to depend on using "new" every time because the constructors may be complex. This way I encapsulate all of the changes into a single method that is clear to others for future maintenance.