mercredi 5 avril 2017

C#: When one should go for factory method pattern instead of factory pattern

i understand the both factory and factory method pattern. in factory pattern we create instance of my classed by another class function dynamically where i pass some parameter to another class function and based on that parameter another class function return right instance of class.

in factory method pattern we have to proceed one further step. in factory method pattern subclass create instance of my class. i do not find a scenario where people has to go for factory method pattern. so please some one come with a scenario where normal factory pattern will not be used rather people prefer to use factory method pattern.

here i am posting two set of code first one done by factory pattern and second one done by factory design pattern

1st set of code where factory pattern used

public enum Shipper
{
    UPS = 1,
    FedEx = 2,
    Purolator = 3
}

public interface IShip
{
    void Ship();
}

public class ShipperPurolator : IShip
{
    public void Ship()
    {
        //-- code logic to implement shipping method for Purolator
        MessageBox.Show("Purolator ship start");
    }
}

public class ShipperUPS : IShip
{
    public void Ship()
    {
        //-- code logic to implement shipping method for Purolator
        MessageBox.Show("UPS ship start");
    }
}

public class ShipperFexEx : IShip
{
    public void Ship()
    {
        //-- code logic to implement shipping method for Purolator
        MessageBox.Show("FedEx ship start");
    }
}

public class ShipperFactory
{
    public static IShip CreateInstance(Shipper enumModuleName)
    {
        IShip objActivity = null;

        switch (enumModuleName)
        {
            case Shipper.UPS:
                objActivity = new ShipperUPS();
                break;
            case Shipper.FedEx:
                objActivity = new ShipperFexEx();
                break;
            case Shipper.Purolator:
                objActivity = new ShipperPurolator();
                break;
            default:
                break;
        }
        return objActivity;
    }
}

Calling this way

IShip objActivity = null;


private void btnUPS_Click(object sender, EventArgs e)
{
    objActivity = ShipperFactory.CreateInstance(Shipper.UPS);
    objActivity.Ship();
}

private void btnFedEx_Click(object sender, EventArgs e)
{
    objActivity = ShipperFactory.CreateInstance(Shipper.FedEx);
    objActivity.Ship();
}

private void btnPurolator_Click(object sender, EventArgs e)
{
    objActivity = ShipperFactory.CreateInstance(Shipper.Purolator);
    objActivity.Ship();
}

now same thing done by factory method pattern where i have to write more code to get the job done

public interface IShip
{
    void Ship();
}

public class ShipperPurolator : IShip
{
    public void Ship()
    {
        //-- code logic to implement shipping method for Purolator
        MessageBox.Show("Purolator ship start");
    }
}

public class ShipperUPS : IShip
{
    public void Ship()
    {
        //-- code logic to implement shipping method for Purolator
        MessageBox.Show("UPS ship start");
    }
}

public class ShipperFedEx : IShip
{
    public void Ship()
    {
        //-- code logic to implement shipping method for Purolator
        MessageBox.Show("FedEx ship start");
    }
}

// factory class start

public interface IShipFactory
{
    IShip GetShipper();  
}

public class ShipperFexExFactory : IShipFactory
{
    public IShip GetShipper()
    {
        //-- code logic to implement shipping method for Purolator
        //MessageBox.Show("FedEx ship start");
        return new ShipperFedEx();
    }
}

public class ShipperUPSFactory : IShipFactory
{
    public IShip GetShipper()
    {
        //-- code logic to implement shipping method for Purolator
        return new ShipperUPS();
    }
}

public class ShipperPurolatorFactory : IShipFactory
{
    public IShip GetShipper()
    {
        //-- code logic to implement shipping method for Purolator
        return new ShipperPurolator();
    }
}

calling like this way

IShipFactory _IShipFactory = null;

private void btnUPS_Click(object sender, EventArgs e)
{
    _IShipFactory = new ShipperUPSFactory();
    _IShipFactory.GetShipper().Ship();
}

private void btnFedEx_Click(object sender, EventArgs e)
{
    _IShipFactory = new ShipperFexExFactory();
    _IShipFactory.GetShipper().Ship();
}

private void btnPurolator_Click(object sender, EventArgs e)
{
    _IShipFactory = new ShipperPurolatorFactory();
    _IShipFactory.GetShipper().Ship();
}

please tell me a solid scenario when people feel right to choose factory method pattern. thanks

Aucun commentaire:

Enregistrer un commentaire