Wednesday, 5 June 2019

Design Patterns

Factory Method Pattern

A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.
The Factory Method Pattern is also known as Virtual Constructor.

Advantage of Factory Design Pattern
  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.

Usage of Factory Design Pattern

  • When a class doesn't know what sub-classes will be required to create
  • When a class wants that its sub-classes specify the objects to be created.
  • When the parent classes choose the creation of objects to its sub-classes.

UML for Factory Method Pattern

  • We are going to create a Plan abstract class and concrete classes that inherited the Plan abstract class. A factory class GetPlanFactory is defined as a next step.
  • GenerateBill class will use GetPlanFactory to get a Plan object. It will pass information (DOMESTICPLAN / COMMERCIALPLAN / INSTITUTIONALPLAN) to GetPalnFactory to get the type of object it needs.

Calculate Electricity Bill : A Real World Example of Factory Method

using System;
//-------------------------------------Factory Design Pattern------------------------------------------//
namespace Design_Patterns
{
    public class GenerateBill
    {
        public static void Main(String[] args)
        {
            GetPlanFactory planFactory = new GetPlanFactory();
            Console.WriteLine("Enter the name of plan for which the bill will be generated: ");
            String planName = Console.ReadLine();
            Console.WriteLine("Enter the number of units for bill will be calculated: ");
            int units = Convert.ToInt32(Console.ReadLine());
            Plan p = planFactory.getPlan(planName);
            //call getRate() method and calculateBill()method of DomesticPaln. 
            Console.WriteLine("Bill amount for " + planName + " of  " + units + " units is: ");
            p.getRate();
            p.calculateBill(units);
            Console.ReadKey();
        }
    }
    public abstract class Plan
    {
        public double rate { get; set; }
        public abstract  void getRate();
        public void calculateBill(int units)
        {
            Console.WriteLine(units * rate);
        }
    }
    public class DomesticPlan : Plan
    {
        public override void getRate()
        {
            rate = 3.50;
        }
    }
    public class InstitutionalPlan : Plan
    {
        public override void getRate()
        {
            rate = 5.50;
        }
    }
    public class CommercialPlan : Plan
    { 
        public override void getRate()
        {
            rate = 7.50;
        }
    }
    public class GetPlanFactory
    { 
        //use getPlan method to get object of type Plan   
        public Plan getPlan(String planType)
        {
            if (planType == null)
            {
                return null;
            }
            if (planType.ToUpper() == "DOMESTICPLAN")
            {
                return new DomesticPlan();
            }
            else if (planType.ToUpper() == "COMMERCIALPLAN")
            {
                return new CommercialPlan();
            }
            else if (planType.ToUpper() == "INSTITUTIONALPLAN")
            {
                return new InstitutionalPlan();
            }
            return null;
        }
    }  
}

output:





No comments:

Post a Comment

Top Agile Interview Questions & Answers

Top Agile Interview Questions & Answers 1. What is Agile Testing? The first question of agile interview question tests your k...