Abstract Factory Design Pattern – Object Oriented Design – C Sharp

0
  Uncategorized

We will see c sharp implementation of Abstract factory design pattern in object oriented design that supports the creation of products that exist in families and are designed to be produced together. The abstract factory can be refined to concrete factories, each of which can create different products of different types and in different combinations. The pattern isolates the product definitions and their class names from the client so that the only way to get one of them is through a factory. For this reason, product families can easily be interchanged or updated without upsetting the structure of the client.

 

abstract-factory-design-pattern-uml-diagram

abstract-factory-design-pattern-uml-diagram

AbstractFactory Pattern

Interface IProduct

Interface IFactory

Class Product  Implements IProduct

Class Factory  Implements IFactory

Class Client

 

Member Variables Member Variables Member Variables Member Variables Memory Variables
IProduct product   IProduct product IFactory
Member Functions Member Functions Member Functions Member Functions Member Funcitons
   

 

Use it when
Use when
  • A system should be independent of how its products are created, composed, and represented.
  • A system can be configured with one of multiple families of products.
  • The constraint requiring products from the same factory to be used together must be enforced.
  • The emphasis is on revealing interfaces, not implementations.

Abstract factory design pattern c sharp code

 

using System;
using System.Collections.Generic;

namespace AbstractFactory
{
    class MainApp
    {
        public static void Main()
        {
            var africa = new AnimalWorld();
            africa.RunFoodChain();
			Console.ReadKey();
        }
    }
	interface IContinentFactory
    {
        IHerbivore CreateHerbivore();
        ICarnivore CreateCarnivore();
    }
	class Africa : IContinentFactory
    {
        public IHerbivore CreateHerbivore()
        {
            return new Wildebeest();
        }
        public ICarnivore CreateCarnivore()
        {
            return new Lion();
        }
    }
	interface IHerbivore
    {
    }
	interface ICarnivore
    {
        void Eat(IHerbivore h);
    }
	class Wildebeest : IHerbivore
    {
    }
	class Lion : ICarnivore
    {
        public void Eat(IHerbivore h)
        {
            // Eat Wildebeest
            Console.WriteLine(this.GetType().Name +
                " eats " + h.GetType().Name);
        }
    }
	interface IAnimalWorld
    {
        void RunFoodChain();
    }
	class AnimalWorld : IAnimalWorld where T : IContinentFactory, new()
    {
        private IHerbivore herbivore;
        private ICarnivore carnivore;
        private T factory;
		public AnimalWorld()
        {
            // Create new continent factory
            factory = new T();
            // Factory creates carnivores and herbivores
            carnivore = factory.CreateCarnivore();
            herbivore = factory.CreateHerbivore();
        }
		public void RunFoodChain()
        {
            carnivore.Eat(herbivore);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *