Monday, October 7, 2013

Implementing a simple factory

Ok so let's talk design patterns for a minute. To be specific the infamous factory. There is no doubt that the factory is quite handy, but what is it used for? Well, there arises a point when you are building an application and realize that you have a lot of similarly classified objects that need to be instantiated at some point in your code, but you don't know which one should be instantiated until the user or some other function implements it. Well a factory can allow you to instantiate those objects without worrying a whole lot about their actual implementation. Which is cool because then your objects become compliant with open-closed principle. But how exactly does this magical pattern work? Let's assume you have you have a system designed for selling different vehicles. Your company currently has objects set up for the busses, sedan and motorcycles it sells.


    public class Bus{
      public Bus(){
        //instantiation code here
      }

      //..
    }

    public class Sedan{
      public Sedan(){
        //instantiation code here
      }

      //..
    }

    public class Motorcycle{
      public Motorcycle(){
        //instantiation code here
      }

      //..
    }


This is great except then you realize that you are calling these individually throughout the business logic, meaning that each object is coupled to it's instantiating class pretty solidly. Sadly this makes your code fairly brittle and you end eventually updating code in dozens of locations when your requirements change. We've all been there and it isn't fun. What you want instead is to be able to replace all those areas with a more simple, more flexible solution that will allow you to get the object you want without necessarily having to be overly concerned about the implementation. So in swoops an interface to allow you to create a level of abstraction between you and the objects.



   public interface Vehicle{
     public String getVehicle();
   }


This means that each of your objects now look like the following.


    public class Bus extends Vehicle{
      public Bus(){
        //instantiation code here
      }

      public String getVehicle(){
        return "If you don't know what a bus is, look it up";
      }
      //..
    }

    public class Sedan extends Vehicle{
      public Sedan(){
        //instantiation code here
      }

      public String getVehicle(){
        return "A sedan is a car with 4 doors...sometimes";
      }
      //..
    }

    public class Motorcycle extends Vehicle{
      public Motorcycle(){
        //instantiation code here
      }

      public String getVehicle(){
        return "A motorcycle has 2 wheels.";
      }
      //..
    }


As you can see all you are doing is returning a String representation of the class name. Now that you are to this point you see that implementing these is just as easy as creating a factory using an class that will allow you to choose between them. So you code it up and get something like the following.



  public class VehicleFactory{
    public Vehicle createVehicle(String type){
      Vehicle vehicle = null;
      if( type.equals("bus"))
        vehicle = new Bus();
      else if( type.equals("sedan"))
        vehicle = new Sedan();
      else if( type.equals("motorcycle"))
        vehicle = new Motorcycle();

      return vehicle;
    }
  }


Now that we have everything in place let's make sure everything works.



public class FactoryMain {

public static void main(String[] argv){
Vehicle vehicle = VehicleFactory.createVehicle("bus");
System.out.println(vehicle.getVehicle());

Vehicle vehicle2 = VehicleFactory.createVehicle("sedan");
System.out.println(vehicle2.getVehicle());

Vehicle vehicle3 = VehicleFactory.createVehicle("motorcycle");
System.out.println(vehicle3.getVehicle());
}
}


There you go, now you have a factory implementation for your vehicles and all is right in the world.