Factory Pattern

I had to use Factory pattern for one my mutual fund house project.The investment strategies includes buying funds as Lumpsum,SIP etc..and the funds which has be bought have options like Redeem,Purchase,Switch.This might not sound familiar if you are not from banking and finance domain,you can have a read over here.

Intention :

There are various Investment plans like SIP,LumpSum etc..and all investment plans has the following option (Purchase,Redeem,Switch).So Its clear that i would have an interface and classes below


public interface IFundOption

{

void Purchase();

void Redeem();

void Switch();

}

//Investment Plan

class Luupsum: IFundOption

{

public void Purchase()

{

//Do Purchase

}

public void Redeem()

{

//Do Redeem

}

public void Switch()

{

//Do Switch

}

}

class SIP : IFundOption

{

public void Purchase()

{

//Do Purchase

}

public void Redeem()

{

//Do Redeem

}

public void Switch()

{

//Do Switch

}

}

 

Problem  :

Given an investment plan as argument,i want to get the instance of investment plan and perform the various options like redeem,purchase etc..Typically i don’t want to write a switch case where i make a comparison based on investment plan and then initiate the class(against open closed principle). The goal is to write a method which accepts investment plan name and returns the instance of investment plan.


public investmentplaninstance   GetinstanceofInvesmentPlan(string investementplanname)

{

return investmentplaninstance ;// goal

}

This is where Factory Pattern comes in,You create a interface for creating object and let sub classes decide which class to initiate.

Factory Pattern :

1.Lets create a interface which is responsible for returning investmentplaninstance.


public interface IGetInvestmentPlanInstance

{

IFundOption CreateInvestmentPlanInstance(); //the instance should be used to create investment plan.

}

2. Lets create Managers (sub classes) which creates the instance of investment plan.


class SIPManager : IGetInvestmentPlanInstance

{

public IFundOption CreateInvestmentPlanInstance()

{

var sipinstance = new SIP(); //sip class implements IFundOption  interface.

return sipinstance;

}

}

class LumpsumManager : IGetInvestmentPlanInstance

{

public IFundOption CreateInvestmentPlanInstance()

{

var lumsuminstance = new Lumpsum(); //Lumpsum class implements IFundOption  interface.

return lumsuminstance ;

}

}

 

3. We have the managers who are ready to give the investment plan instance based on investment plan name.Typically we would put all manager class in settings file/config,and based on input,we create the manager instance based on reflection .


static IGetInvestmentPlanInstance GetManagerClass(string investmetpan)

{

var getmanager = _dictionary.Key(investmentplan);

return Assembly.GetExecutingAssembly.CreateInstance(getmanager) as IGetInvestmentPlanInstance ;

}

[/csharp]

we would make use of this manager class and get the instance of investment plan as below,



var getmanager = GetManagerClass("SIPManager ");

IFundOption options = getmanager.CreateInvestmentPlanInstance();

options.Purchase();

options.Redeem();

 

 

 

#50-SOLID -Liskov Substitution Principle

Liskov Substitution Principle: SubType (Child Class)Must be Substitutable For Base Type(Base Class).

LSP comes into use when you use inheritance/Abstraction.

Rules:

1.Child Class Should not remove Base Class Behaviour.

2.Child Class Should not violate base class invariants.

In short,the Calling code should not know whether it is calling the base class or derived class.