Why do we use Factory design pattern, and Spring Framework?


 Say there are three objects in real world :
  •     A showroom that does some value added services(eg like adding license plate and give the finishing touch by adding varnish) for cars and scooters and uses both for sale after doing the value add.
  •  Car 
  • Scooter 


Now if the showroom has to do finishing for the scooter(without using either Factory Pattern or IOC) as the customer wants, it should know what kind of scooter it is going to do Value added services for i.e. the specifications of the scooter and car(like color,length and the showroom would provide the specifications and get it from the scooter factory and the car factory. So the details of the car/ scooter have to be known to the showroom. Hence, there is a strong dependency/ coupling on showroom for the both of these objects. 

If you look at it programmatically 

to create car object and scooter object from the Car class(ie the Car factory in real world) and Scooter class, the  showroom will have to know about the constructor arguments of car and scooter.

Class Showroom
{
            //say the customer feeds in the choice of vehicle for which he wants to perform value added service
            Public createObjectBasedOnDemand(string choice){
                        If(choice.Equals(“Scooter”))
                                    {Scooter s = new Scooter(length,breadth,ht,color);}
                        Else if(choice.Equals(“Car”))
                                    Car c = new Car(color,height,weight,length);
            }
}

Now firstly, we do not want the showroom to necessarily know all the details of the scooter and car as it is only going to do value added services like adding license plate and give the finishing shine by adding varnish. But in this approach the Showroom knows all the intricate details about the car like height, weight and color.
Secondly (programmatic issue), the “new” keyword is used too many times. This would mean in case another type of vehicle is added eg lorry, a new line of code will have to be added for choice lorry inside the method createObjectBasedOnDemand. This would mean that the whole code will have to be recompiled.
To do away with these 2 problems we use factory method pattern in conjunction with spring.
To solve the first problem 1(showroom knows everything about individual vehicles) we use Factory method pattern:
Step 1: We introduce an interface called Vehicle which the classes Car, Scooter, Lorry implement. The benefit of this is that we will be able to introduce any number of new vehicles that implement this interface (like motorboat, bike etc) and the showroom will not have to know about either them or the specifications of theirs. The showroom will point to the interface alone and will not know anything more than the choice of the customer i.e. the name of the vehicle the customer needs.
Public interface IVehicle{ public doSomeValueAdd();}
Public class car implements IVehicle{ public Car(string color,int ht,int wt,int len){ //some code; }
Public doSomeValueAdd(){//some code;}}
// and similar code for the other classes
Step 2: We introduce a factory class that knows what the concrete classes available. This factory class however will return only thetype Vehicle back to the customer as:
Public class Factory
{
           
            Public static IVehicle getVehicleBasedOnChoice(string choice)
            {
            IVehicle vehicle;
            If(choice.Equals(“Scooter”))
                        {vehicle = new Scooter(len,br,ht,color);}
            ElseIf(choice.Equals(“Car”))
                        {vehicle = new Car(color,ht,wt,len);}
            //some more conditions
            Return vehicle;
            }
}
Step 3: We change the code of the showroom class to reference only the factory and obtain the object of interface and do the value add
Public class Showroom
{
            Console.Writeline(“Enter the choice”);
            string choice = Console.Readline();
            IVehicle vehicle = Factory. getVehicleBasedOnChoice(choice);
            Vehicle.DoVaueAdd();
}

Using this approach the showroom is totally oblivious about what kind of vehicles are present and the specifications of the vehicle.

Solving issue 2( Enormous number of new keywords) Spring Injection
As the new keyword problem is not solved by using Factory method alone, we use the Spring framework to do the work of the factory class by means of xml files. If you have read my first article on Inversion of Control at : http://dotnetnhibernateconcepts.blogspot.in/2013/05/inversion-of-control-in-laymans-terms.html
We have spoken about a container/dealer that does work of the factory class in our example. Spring accomplishes this by xml files which contain data for example:
<objects xmlns="http://www.springframework.net">
  <object id="IVehicle" type="vehicle, someNamespace, Version=9.0.0.0, Culture=neutral, PublicKeyToken=a978a715ccadf176" singleton="true" autowire="constructor" singleton="true" />
We would then instantiate the object of type via a customized factory class that would
obtain the details of the class it is instantiating from this xml file.
More about spring and the xml files etc will be discussed in the articles yet to come J




Thanks for reading.

1 comment:

  1. thanks for this information .
    you can find factory design pattern in simple and easy to understand language in a detail on
    http://www.itsoftpoint.com/?page_id=2666

    ReplyDelete