Factory

Creational Pattern, providing an interface for objects in base class, although allowing subclasses to modify types of objects that are gonna be created 

Problems to solve by Factory Pattern.

Let’s assume, that we want to create an application which role is to design UML diagrams or flowcharts. In this application on every one of these diagrams, you can insert as many elements as you want, which are independent from given diagram – in this case to UML diagram we can insert circle shape, triangle shape etc. To not duplicate the code, we can create a Factory Pattern, which is going to share an interface to create base class objects – that means, our shapes have to have some common feature – in this case assume a given location of our shape on diagram, and render method for base class. Factory will be allowed to return any implementation of shape. 

 

We have isolated Shape abstraction, which is going to have the same properties and method as other three figures. Now it is enough for our three classes to inherit from abstract class Shape. Next step is to create our Factory, which will create given shape. Because of the fact that there can be some shapes to create, our ShapeFactory has to adopt a value, E.g. enum or string, based on which given object will be returned. In conclusion – to our Factory by method createShape() we will pass value, which will identify given shape, and then in the form of Shape class, will be returned implementation.

 public abstract class Shape
{
        public int X { get; set; }
        public int Y { get; set; }
        public abstract void Render();
}

public class ShapeFactory
{
        public Shape createShape(ShapeType type)
        {
            switch (type)
            {
                case ShapeType.Circle:
                    return new Circle();
                case ShapeType.REctangle:
                    return new REctangle();
                case ShapeType.Triangle:
                    return new Triangle();
                default:
                    throw new Exception($"Shape type {type} is not handled");
            }
        }
}

public class Triangle : Shape
{
        public override void Render()
        {
            Console.WriteLine("Render Triangle");
        }
}

public class REctangle : Shape
{
        public override void Render()
        {
            Console.WriteLine("Render rectangle");
        }
}

 public class Circle : Shape
{
        public override void Render()
        {
            Console.WriteLine("Render circle");
        }
}

public enum ShapeType
{
        Circle,
        REctangle,
        Triangle
}

Factory allowed us to separate code which creates objects from code which uses them. In this way its simpler to extend code creating new objects without interfering in rest of code. For example – if we would like to add another shape to our application, it is enough in our createShape() method, in proper way we could implement new case, by implementing new object, which must have to inherit from base class Shape.