Basic Factory Pattern, using Interfaces

The Factory Pattern is a common software development code pattern for creating objects.

In this example we will create a Factory class which is responsible for creating objects that all conform to a common Interface.

The quasi-real world example used here is based on returning Workflow Controllers for managing content of different types within a Content Management System.

First lets create the Interface that the objects the Factory returns will all conform to:


namespace FactoryPatternTestApp.Interfaces
{
    public interface IWorkflowController
    {
        void Approve();
        void Update();
        void Reject();
        void Delete();
    }
}

As you can see all the WorkflowControllers will have 4 methods:

  • Approve()
  • Update()
  • Reject()
  • Delete()

Now lets create the two example Workflow Controllers.

News content WorkflowController.


namespace FactoryPatternTestApp.Controllers
{
    public class NewsWorkflowController : IWorkflowController
    {
        public void Approve()
        {
            // logic here
            Console.WriteLine("NewsWorkflowController.Approve");
        }

        public void Update()
        {
            // logic here
            Console.WriteLine("NewsWorkflowController.Update");
        }

        public void Reject()
        {
            // logic here
            Console.WriteLine("NewsWorkflowController.Reject");
        }

        public void Delete()
        {
            // logic here
            Console.WriteLine("NewsWorkflowController.Delete");
        }
    }
}

Events content WorkflowController.


namespace FactoryPatternTestApp.Controllers
{
    public class EventsWorkflowController : IWorkflowController
    {
        public void Approve()
        {
            // logic here
            Console.WriteLine("EventsWorkflowController.Approve");
        }

        public void Update()
        {
            // logic here
            Console.WriteLine("EventsWorkflowController.Update");
        }

        public void Reject()
        {
            // logic here
            Console.WriteLine("EventsWorkflowController.Reject");
        }

        public void Delete()
        {
            // logic here
            Console.WriteLine("EventsWorkflowController.Delete");
        }
    }
}

Next lets create the Workflow Controller Enumerator


namespace FactoryPatternTestApp
{
    public enum WorkflowControllerEnum
    {
        Events = 1,
        News = 2,
    }
}

OK, now we are ready to create our basic Workflow Controller Factory


namespace FactoryPatternTestApp
{
    public class WorkflowControllerFactory
    {
        public IWorkflowController CreateWorkflowController(WorkflowControllerEnum workflowControllerEnum)
        {
            switch (workflowControllerEnum)
            {
                case WorkflowControllerEnum.Events:

                    return new EventsWorkflowController();

                case WorkflowControllerEnum.News:

                    return new NewsWorkflowController();

                default:

                    throw new InvalidCastException("Unknown Workflow Controller");
            }
        }
    }
}

As you can see, the Factory will return the Workflow Controller based on the Workflow Enumerator passed into the CreateWorkflowController method.

Next we can shall use the Factory to manage some content.


namespace FactoryPatternTestApp
{
    internal class Program
    {
        private static IWorkflowController _workflowController;

        private static void Main(string[] args)
        {
            WorkflowControllerFactory factory = new WorkflowControllerFactory();

            _workflowController = factory.CreateWorkflowController(WorkflowControllerEnum.Events);

            _workflowController.Update();
            _workflowController.Reject();
            _workflowController.Approve();
            _workflowController.Delete();

            _workflowController = factory.CreateWorkflowController(WorkflowControllerEnum.News);

            _workflowController.Update();
            _workflowController.Reject();
            _workflowController.Approve();
            _workflowController.Delete();

            Console.ReadKey();
        }
    }
}

This will produce this output


EventsWorkflowController.Update
EventsWorkflowController.Reject
EventsWorkflowController.Approve
EventsWorkflowController.Delete
NewsWorkflowController.Update
NewsWorkflowController.Reject
NewsWorkflowController.Approve
NewsWorkflowController.Delete

You can download the Basic Factory Pattern project here



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.