Abstract Factory Design Pattern C#

Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes.

AbstractFactory

Florist:

    /// <summary>
    /// Product A
    /// </summary>
    public interface IFlorist { void DecorateFlowers(); }
    
    /// <summary>
    /// Product A1
    /// </summary>
    public class HighBudgetFlorist : IFlorist
    {
        public void DecorateFlowers()
        {
            Console.WriteLine("Florist: Thanks for choosing us to serve...");
        }
    }

    /// <summary>
    /// Product A1
    /// </summary>
    public class LowBudgetFlorist : IFlorist
    {
        public void DecorateFlowers()
        {
            Console.WriteLine("Florist: Thanks for giving us the chance to serve...");
        }
    }

Caterer:

    /// <summary>
    /// Product B
    /// </summary>
    public interface ICaterer { void ServeFood(); }

    /// <summary>
    /// Product B1
    /// </summary>
    public class HighBudgetCaterer : ICaterer
    {
        public void ServeFood()
        {
            Console.WriteLine("Caterer: Thanks for choosing us to serve...");
        }
    }

    /// <summary>
    /// Product B2
    /// </summary>
    public class LowBudgetCaterer : ICaterer
    {
        public void ServeFood()
        {
            Console.WriteLine("Caterer: Thanks for giving us the chance to serve...");
        }
    }

EventOrganiser:

    /// <summary>
    /// Abstract Factory
    /// </summary>
    public interface IEventOrganiser
    {
        IFlorist GetFlorist();
        ICaterer GetCaterer();
    }

    /// <summary>
    /// Concrete factory 1
    /// </summary>
    public class HighBudetEventOrganiser : IEventOrganiser
    {
        public IFlorist GetFlorist()
        {
            return new HighBudgetFlorist();
        }

        public ICaterer GetCaterer()
        {
            return new HighBudgetCaterer();
        }
    }

    /// <summary>
    /// Concrete factory 2
    /// </summary>
    public class LowBudetEventOrganiser : IEventOrganiser
    {
        public IFlorist GetFlorist()
        {
            return new LowBudgetFlorist();
        }

        public ICaterer GetCaterer()
        {
            return new LowBudgetCaterer();
        }
    }

Interacting Client:

    /// <summary>
    /// Interacting Client
    /// </summary>
    public class EventManagementFirm
    {
        IEventOrganiser eventOrganiser;
        public EventManagementFirm(IEventOrganiser _eventOrganiser)
        {
            this.eventOrganiser = _eventOrganiser;
        }

        public void OganiseEvent()
        {
            IFlorist florist = eventOrganiser.GetFlorist();
            ICaterer caterer = eventOrganiser.GetCaterer();

            florist.DecorateFlowers();
            caterer.ServeFood();
        }
    }

Main Method:

static void Main(string[] args)
        {
            IEventOrganiser highProfileEvent = new HighBudetEventOrganiser();   //or LowBudetEventOrganiser
            EventManagementFirm eventManager = new EventManagementFirm(highProfileEvent);
            eventManager.OganiseEvent();
        }

Output:
AbstractFactoryOutput

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s