Topics
Published on August 10, 2017 by kudvenkat
Want create site? Find Free WordPress Themes and plugins.

Text version of the video
csharp-video-tutorials.blogspot.com/2017/08/factory-method-design-pattern.html

Slides
csharp-video-tutorials.blogspot.com/2017/08/factory-method-design-pattern_10.html

Design Patterns Tutorial playlist

Design Patterns Text articles and slides
csharp-video-tutorials.blogspot.com/2017/06/design-patterns-tutorial-for-beginners.html

All Dot Net and SQL Server Tutorials in English
www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
www.youtube.com/c/KudvenkatArabic/playlists

In this tutorial we will learn
1. Simple Factory
2. Factory Method Pattern Implementation

Recap Simple Factory
1. Simple factory abstracts the creation details of the product
2. Simple factory refers to the newly created object through an interface
3. Any new type creation is handed with a change of code in the factory class and not at the client code

Factory Method Pattern Example

Business Requirement

1. Differentiate employees as permanent and contract and segregate their pay scales as well as bonus based on their employee types. ( We have achieved this using simple factory in the Part 8 of the Factory Pattern introduction session)
2. Calculate Permanent employee house rent allowance
3. Calculate Contract employee medical allowance

Steps to solve the above business requirement

Step 1: Add HouseAllowance and MedicalAllowance to the existing Employee table.
CREATE TABLE [dbo].[Employee] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR (50) NOT NULL,
[JobDescription] VARCHAR (50) NOT NULL,
[Number] VARCHAR (50) NOT NULL,
[Department] VARCHAR (50) NOT NULL,
[HourlyPay] DECIMAL (18) NOT NULL,
[Bonus] DECIMAL (18) NOT NULL,
[EmployeeTypeID] INT NOT NULL,
[HouseAllowance] DECIMAL (18) NULL,
[MedicalAllowance] DECIMAL (18) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Employee_EmployeeType] FOREIGN KEY ([EmployeeTypeID]) REFERENCES [dbo].[Employee_Type] ([Id]) );

Step 2: Open EmployeePortal.edmx under the Models folder of the solution and update the model from the database (Right click on the model designer and choose update from database option)

Step 3: Create FactoryMethod folder under existing Factory folder and add BaseEmployeeFactory class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Managers;
using Web.Models;

namespace Web.Factory.FactoryMethod
{
public abstract class BaseEmployeeFactory
{
protected Employee _emp;
public BaseEmployeeFactory(Employee emp)
{
_emp = emp;
}
public Employee ApplySalary()
{
IEmployeeManager manager = this.Create();
_emp.Bonus = manager.GetBonus();
_emp.HourlyPay = manager.GetPay();
return _emp;
}
public abstract IEmployeeManager Create();
}
}

Step 4: Create ContractEmployeeFactory class under FactoryMethod folder.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Managers;
using Web.Models;

namespace Web.Factory.FactoryMethod
{
public class ContractEmployeeFactory : BaseEmployeeFactory
{
public ContractEmployeeFactory(Employee emp) : base(emp)
{
}

public override IEmployeeManager Create()
{
ContractEmployeeManager manager = new ContractEmployeeManager();
_emp.MedicalAllowance = manager.GetMedicalAllowance();
return manager;
}
}
}
Step 5: Create PermanentEmployeeFactory class under FactoryMethod folder.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Managers;
using Web.Models;

namespace Web.Factory.FactoryMethod
{
public class PermanentEmployeeFactory : BaseEmployeeFactory
{
public PermanentEmployeeFactory(Employee emp) : base(emp)
{
}

public override IEmployeeManager Create()
{
PermanentEmployeeManager manager = new PermanentEmployeeManager();
_emp.HouseAllowance = manager.GetHouseAllowance();
return manager;
}
}
}
Step 6: Create EmployeeManagerFactory class under FactoryMethod folder and add new Method CreateFactory which returns BaseEmployeeFactory.
CreateFactory method is responsible to return base factory which is the base class of Permanent and Contract Factories.

Did you find apk for android? You can find new Free Android Games and apps.

Leave a Reply

13 Comments on "Factory Method Design Pattern"

Notify of
avatar

Tom Yang
Guest
Tom Yang
1 month 15 days ago

at 15:10 how is the employee object created from the data entered in the form ?

Stamps World
Guest
Stamps World
1 month 15 days ago

On 12.20 you have created ApplySalary() method whose return type is Employee.1) I think the method ApplySalary() should have void return type.2) What is the point of returning Employee object from ApplySalary() method? As it is the same Employee object which you are passing from controller. So in controller we already have Employee object so returning same Employee object from ApplySalary method doesn't make sense here.Please suggest.

Muhammad Farooq Raza
Guest
Muhammad Farooq Raza
1 month 21 days ago

Nice video and got a good idea about FMDP.. I have one quick question, Why we should not put this all this computational or business logic in a simple stored procedure rather than following this pretty pattern just for applying some regional rules. Does it make sense, because it will give more flexibility and you can change rules when ever you need. Because at the end you just need to store Employee object and you can do it easily with SP by applying what ever rules you need.

Ivaylo Zhelev
Guest
Ivaylo Zhelev
2 months 9 days ago

Thanks, but it could be better to have more experience development with a simple example and why we need to use those abstract classes and why we need to use one pattern instead simple implementation. What is the really benefit of that!

Sudhanshu Verma
Guest
Sudhanshu Verma
2 months 29 days ago

Now I do not fear from factory pattern .. thanks 🙂

Manas Mangal Pattanaik
Guest
Manas Mangal Pattanaik
3 months 26 days ago

Such a great lesson Avish. The quality of your narration & program representation is excellent.

nishant singh
Guest
nishant singh
3 months 26 days ago

Thanks. Can you plz train us on how to design a Framework…form Technical Architect Prospective…

RAQIBUL ALAM RASHED
Guest
RAQIBUL ALAM RASHED
4 months 3 days ago

Thanks a Billions.

kapavarapu pramod
Guest
kapavarapu pramod
4 months 3 days ago

Thanks for the tutorials..Realy good tutorials…But I did not see the concepts releated to OOPS Encapuslation,Abstraction….Please post those tutorials also

H KN
Guest
H KN
4 months 4 days ago

Pleae provide more example to get more claity

wpDiscuz