Skip to content

Instantly share code, notes, and snippets.

@wesleyduff
Last active March 1, 2018 16:44
Show Gist options
  • Save wesleyduff/341e1bee891d4a94ae79919706bec43d to your computer and use it in GitHub Desktop.
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
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);
}
}
}
@wesleyduff
Copy link
Author

wesleyduff commented Mar 1, 2018

Factory Pattern

  • Product
    -- is an Interface that is used by a concrete product
  • Concrete Product
    -- implements the Product Interface
    -- Contains other methods and properties that maybe needed by the instantiated object.
  • Creator Factory is an Abstract class
    -- contains properties and methods needed to orchestrate and deliver the correct object that is requested.
  • Concrete Creator
    -- Implements the creator factory
    -- overrides the methods needed to return the correct requested object

Program

  • create a Factory Creator called factory
  • create a Concrete Product of the object you want to get from the factory.
    -- call the factory to return you an instance of your Concrete Product
  • Use your newly instantiated Concrete Product and call its methods.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment