Skip to content

Instantly share code, notes, and snippets.

@flaviorl-net
Last active March 6, 2022 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaviorl-net/6bdbbeac5afdc7d753d05a9fee9aeed5 to your computer and use it in GitHub Desktop.
Save flaviorl-net/6bdbbeac5afdc7d753d05a9fee9aeed5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace FactoryMethod
{
public class Program
{
static void Main(string[] args)
{
var creators = new List<ICreator>() {
new ConcreteCreatorA(),
new ConcreteCreatorB()
};
Console.WriteLine("{0}", creators[0].FactoryMethod().Tipo());
Console.WriteLine("{0}", creators[1].FactoryMethod().Tipo());
Console.ReadKey();
}
}
public interface IProduct {
string Tipo();
}
public class ConcreteProductA : IProduct
{
public string Tipo() => "ConcreteProductA";
}
public class ConcreteProductB : IProduct
{
public string Tipo() => "ConcreteProductB";
}
public interface ICreator
{
IProduct FactoryMethod();
}
public class ConcreteCreatorA : ICreator
{
public IProduct FactoryMethod()
{
return new ConcreteProductA();
}
}
public class ConcreteCreatorB : ICreator
{
public IProduct FactoryMethod()
{
return new ConcreteProductB();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment