Skip to main content

Abstract Factory Design Pattern

Abstract Factory :return families of related or dependent objects without specifying their concrete classes.


  • AbstractFactory- IVehicle
  • ConcreteFactory - Maruti, Honda
  • AbstractProduct- IDiesel, IPetrol
  • Product- DezirePetrol, ZenDiesel, AmazeDiesel, CityPetrol
  • Client-This is a class which uses AbstractFactory and AbstractProduct interfaces to create a family of related objects
Implementation is pretty much straight forward: Since now you have good understanding of Factory, there is nothing much to explain.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            IVehicle marutiVehicle = new Maruti();
            CarClient marutiClient = new CarClient(marutiVehicle);
            Console.WriteLine("********* MARUTI **********");
            Console.WriteLine(marutiClient.GetDieselModelDetails());
            Console.WriteLine(marutiClient.GetPetrolModelDetails());
            IVehicle hondaMobilePhone = new Honda();
            CarClient hondaClient = new CarClient(hondaMobilePhone);
            Console.WriteLine("******* HONDA **********");
            Console.WriteLine(hondaClient.GetDieselModelDetails());
            Console.WriteLine(hondaClient.GetPetrolModelDetails());
            Console.ReadKey();
        }
    }
    public interface IVehicle
    {
        IDieselVehicle GetDieselVehicle();
        IPetrolVehicle GetPetrolVehicle();
    }
    public interface IPetrolVehicle
    {
        string GetModelDetails();
    }
    public interface IDieselVehicle
    {
        string GetModelDetails();
    }
    public class Maruti : IVehicle
    {
        public IDieselVehicle GetDieselVehicle()
        {
            return new DezirePetrol();
        }
        public IPetrolVehicle GetPetrolVehicle()
        {
            return new ZenDiesel();
        }
    }
    public class ZenDiesel : IPetrolVehicle
    {
        public string GetModelDetails()
        {
            return "Maruti Zen Diesel Variant";
        }
    }
    public class DezirePetrol : IDieselVehicle
    {
        public string GetModelDetails()
        {
            return "Maruti Swift Dezire Petrol Variant";
        }
    }
    public class Honda : IVehicle
    {
        public IDieselVehicle GetDieselVehicle()
        {
            return new AmazeDiesel();
        }
        public IPetrolVehicle GetPetrolVehicle()
        {
            return new CityPetrol();
        }
    }
    public class CityPetrol : IPetrolVehicle
    {
        public string GetModelDetails()
        {
            return "Honda City Petrol Variant";
        }
    }
    public class AmazeDiesel : IDieselVehicle
    {
        public string GetModelDetails()
        {
            return "Amaze Diesel Variant";
        }
    }
    public class CarClient
    {
        IPetrolVehicle petrolVehicle;
        IDieselVehicle dieselVehicle;
        public CarClient(IVehicle factory)
        {
            petrolVehicle = factory.GetPetrolVehicle();
            dieselVehicle = factory.GetDieselVehicle();
        }
        public string GetPetrolModelDetails()
        {
            return petrolVehicle.GetModelDetails();
        }
        public string GetDieselModelDetails()
        {
            return dieselVehicle.GetModelDetails();
        }
    }
}


Comments

Popular posts from this blog

About Us

Ashish Joshi C# TestDome Certificate Hi, Welcome to my blog on various topics on .Net Stack basically. My name is Ashish Joshi and I’m a software developer. I majorly work with .NET. I recently started writing  blogs about .NET but since now .Net Stack is huge and crossing it's boundaries, it's  compatible with other technology stacks, you’ll see posts from other technologies as well. I hope you’ll find a lot of interesting topics here that you can use in your own assignments. You can find me on LinkedIn I am.planning to write in coming days (2017-2018) on: Popular Interview Questions For Experienced .Net resources  MongoDb in .NET Various Design Patterns Machine Learning - will try to demonstrate how it can be done in .net and in other technology Artificial Intelligence - same as machine learning I'll demonstrate using some running examples Feel free to provide your feedback and suggestions.

Dependency Injection and Inversion Of Control C# - Par 2 (Implementation)

Before going through this post, I suggest you to go through my previous post on basic concepts in Dependency injection and Inversion of control. Demo on product. This demo is based on a traditional implementation approach: We have a Product class and it has a method which takes the discount and based on some business logic it applies the discount on the product. Product service will interact with database using ProductRepository. If you will notice ProductService has dependency on 2 concrete classes i.e. Discount and ProductRepository new Discount(); new ProductRepository(); We can hence say that ProductService is tightly coupled because of these 2 dependencies. We may require to use different discount based on several factors such as Product type, Some Festival or any other factor. Based on different discounts we may need to change ProductService class also in future you may change the database from SQL to Oracle, Again it may require ProductService changes which is viol...