Articles → .NET DESIGN PATTERN → Factory Method Design Pattern

Factory Method Design Pattern






Purpose





Scenario




public enum CalculationType
{
	Sum,
	Difference,
	Multiplication,
	Division
}

public class SimpleCalculator 
{
    private CalculationType calculationType;
    public SimpleCalculator(CalculationType _calculationType)
    {
		this.calculationType = _calculationType;
    }
	  public int ReturnResult(int arg1, int arg2)
	  {
		  int result = 0;
		  switch (calculationType)
		  {
			  case CalculationType.Sum:
				  result = arg1 + arg2;
				  break;
			  case CalculationType.Difference:
				  result = arg1 - arg2;
				  break;
			  case CalculationType.Multiplication:
				  result = arg1 * arg2;
				  break;
			  case CalculationType.Division:
				  result = arg1/arg2;
				  break;
		  }
		  return result;
	  }
}




SimpleCalculator oCalculator1 = new SimpleCalculator(CalculationType.Sum);
int result = oCalculator1.ReturnResult(1, 2);





Steps




Picture showing the UML diagram of factory method design pattern
Click to Enlarge



  1. Create a Product interface
  2. Create a Concrete Product class that implements the Product interface
  3. Create an abstract Creator class
  4. create the Concrete Creator class and implement the Creator abstract class
  5. Call the class method



Create A Product Interface




public interface iCalc
{
	int ReturnResult(int arg1, int arg2);
}



Create A Concrete Product Class That Implements The Product Interface




public class Sum : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return (arg1 + arg2);
	}
}

public class Difference : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return (arg1 - arg2);
	}
}

public class Multiplication : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return (arg1 * arg2);
	}
}

public class Division : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return Convert.ToInt32(arg1 / arg2);
	}
}



Create An Abstract Creator Class




public abstract class AbstractCalculator
{
	public abstract iCalc CreateOperation(CalculationType oCalculationType);
}



Create The Concrete Creator Class And Implement The Creator Abstract Class




public class Calculator : AbstractCalculator
{
	public override iCalc CreateOperation(CalculationType oCalculationType)
	{
		if (oCalculationType == CalculationType.Sum)
		{
			return new Sum();
		}
		else if (oCalculationType == CalculationType.Difference)
		{
			return new Difference();
		}
		else if (oCalculationType == CalculationType.Multiplication)
		{
			return new Multiplication();

		}
		else if (oCalculationType == CalculationType.Division)
		{
			return new Division();
		}
		else
		{
			throw new ApplicationException("Problem Occurs");
		}
	}


}



Call The Class Method




Calculator oCalculator = new Calculator();
iCalc x = oCalculator.CreateOperation(CalculationType.Sum);
int result = x.ReturnResult(1, 2);
Console.WriteLine(String.Format("Output is: {0}", result));



Output


Picture showing the output of factory method design pattern
Click to Enlarge


Complete Code


// A Simple Interface
public interface iCalc
{
	int ReturnResult(int arg1, int arg2);
}


public class Sum : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return (arg1 + arg2);
	}
}

public class Difference : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return (arg1 - arg2);
	}
}

public class Multiplication : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return (arg1 * arg2);
	}
}

public class Division : iCalc
{
	public int ReturnResult(int arg1, int arg2)
	{
		return Convert.ToInt32(arg1 / arg2);
	}
}


public enum CalculationType
{
	Sum,
	Difference,
	Multiplication,
	Division
}
public abstract class AbstractCalculator
{
	public abstract iCalc CreateOperation(CalculationType oCalculationType);
}
public class Calculator : AbstractCalculator
{
	public override iCalc CreateOperation(CalculationType oCalculationType)
	{
		if (oCalculationType == CalculationType.Sum)
		{
			return new Sum();
		}
		else if (oCalculationType == CalculationType.Difference)
		{
			return new Difference();
		}
		else if (oCalculationType == CalculationType.Multiplication)
		{
			return new Multiplication();

		}
		else if (oCalculationType == CalculationType.Division)
		{
			return new Division();
		}
		else
		{
			throw new ApplicationException("Problem Occurs");
		}
	}


}





Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, November 15, 2022

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250