Skip to content

Instantly share code, notes, and snippets.

@mizrael
Created October 1, 2021 14:05
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 mizrael/e61a8261bd5e87e23b4eb5e816637946 to your computer and use it in GitHub Desktop.
Save mizrael/e61a8261bd5e87e23b4eb5e816637946 to your computer and use it in GitHub Desktop.
Simple Factory implementation in C#
public interface IShapeRenderer { }
public class CircleRenderer : IShapeRenderer { }
public class TriangleRenderer : IShapeRenderer { }
public class SquareRenderer : IShapeRenderer { }
public interface IShapeRendererFactory
{
IShapeRenderer Create(string type);
}
public class ShapeRendererFactory : IShapeRendererFactory
{
public IShapeRenderer Create(string type)
{
return type.ToLower() switch
{
"circle" => new CircleRenderer(),
"triangle" => new TriangleRenderer(),
"square" => new SquareRenderer(),
_ => throw new NotImplementedException(),
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment