Factory Method Design Pattern

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

Design Patterns Tutorial playlist

Design Patterns Text articles and slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

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 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.
Рекомендации по теме
Комментарии
Автор

I am taking my skillset to the next level after going through these really awesome videos. Thanks a lot.

dp-bhatt
Автор

I was playing the kudvenkat Sir Video's at 1.25 speed and have found myself playing Avish Sir video's at the same speed :). By the way the content is great and is one of the best explanation available on the internet about implementing design pattern using C#

mayankbhadani
Автор

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

manasmangalpattanaik
Автор

Nice video.
One suggestion: Better if you can explain the concept/blueprint in simple words at the starting than giving heavy definitions. The concept, problem and how it solves like we explain on paper. Jumping to implementation needs lot of time to understand what is achieved.
Thanks again for the video.

shahdhaval
Автор

happy to see you back. Just a request can you please make a 2-3 videos together or at least complete 1 design with one go because time between two videos are much more so it's hard to remind all the things and have to watch all the videos from start to end. Nothing other than this. It's awesome to see real time example. thank you for this.

saurabhchauhan
Автор

Tahnk you so much for wanderful lessons, very clear explaining with code example.

prasantadas
Автор

🙏 Good explanation Avish. Keep going, I'm learning lot of new things. Now I'm confident an Factory.

itsLuckyVideoCreater
Автор

Suggestion:


Wouldn't it be nice to describe scenario what we are going to implement before jumping to code? Kind of blueprint at high level..
Content is good but couldn't related to why we are doing this .


Thanks for making this video. Appreciated !!!

shahdhaval
Автор

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

tomyang
Автор

Now I do not fear from factory pattern .. thanks :)

sudhanshuverma
Автор

excellent real world based example.. thank you...keep the great work

piinetu
Автор

Oh well... Now this one is a little bit trickier than the singleton and the simple factory man.. Anyways lets dive deep.. Nice tutorial.. Thanks..

thameemansari
Автор

Thank you so much Avish for your wonderful lessons!

kamdemkakengne
Автор

V nice explanation.. thanks for sharing knowledge...

AmitSoni
Автор

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.

muhammadfarooqraza
Автор

I think the ApplySalary() method should not be creating the base employee factory instance. Am I wrong here? The way it was coded it seems to be adding a hidden behavior. Shouldn't ApplySalary() be only about applying salaries? My intuition says it should receive an employee instance and return a void. I guess that means that depending on what the lazy or immediate requirements are, the factory would expose an additional method for loading. Thoughts?

robertwinkler
Автор

I think you have provided the wrong example for this.
you should show the example where you can get the HRA or MRA to the controller based on object type
ultimately the client should be only aware of the creator(Base Employee Factory) and the Product(IEmployeeManager) in your case. Actually to satisfy the factory Method implementation you may need to introduce 2 new concrete products.

Correct me If I'm wrong

satishrachamalla
Автор

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.

stampsworld
Автор

It looks like inception movie😁 but overall nice explanations

ashismahapatra
Автор

Summary — through this approach, we are basically creating three heirechy factories. One for common class operations, one for specific to the class and last for interacting with the client for giving final result object. Am i right ?

AmitArora-eb