Sunday, June 27, 2010

Abstarct Factory Method

In this edition of my blog, the usage of related objects to form a single component using Abstract Factory method is shared. For instance, take a CAR as example. There are different types of cars available in the market. If a particular customer wants to check the price details and the features in a specific car model, then we could come up with two component (1) Pricing component – which provides the price based on type of customer like corporate ,VIP and type of payment Loan,Cash etc.
(2) Features component – describes the features of the given model of the car.
Now the above 2 are independent component which could be related to provide the details of a car by a dealer.

Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Applicability
Use the Abstract Factory pattern when
• a system should be independent of how its products are created, composed, and represented.
• a system should be configured with one of multiple families of products.
• a family of related product objects is designed to be used together, and you need to enforce this constraint.
• you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations

The below is an example which is written in a simple manner for beginners to understand, the example accepts the name of the car like “Tyota”, “HondaCity” and type of payment like “Loan” , “Cash” to provide the features and price of a car. The readers can extend the example according to their preference to provide more details.

abstract class VehicleFactory
{
public static Vehicle getVehicle(String name){ // static factory method.

try{
Class vehicleobj=Class.forName(name);
return (Vehicle)vehicleobj.newInstance();
}catch(Exception exp){
exp.printStackTrace();
}
return null;
}
}
/*
Factory interface which returns multiple products like Cost,Features are product interfaces
*/
interface Vehicle {
public Cost getCostSpecification();
public Features getFeatures();
}
interface Cost // product interface
{
public void checkPaymentType(String type);
public void print();
}
interface Features // product interface
{
// could check for type of car like with gear or without gear or subtype or model like innova,corolla

public void print();
}

class TyotaCar implements Vehicle{
public Cost getCostSpecification(){
return new TyotaCost();
}
public Features getFeatures(){

return new TyotaFeatures();
}
}

class TyotaFeatures implements Features{
public void print(){

System.out.println("Tyota Car Features");
}
}
class TyotaCost implements Cost
{
int amount=800000;
public void checkPaymentType(String type){

if(type.equalsIgnoreCase("Loan")){
amount+=amount*0.2;
}else{
amount-=amount*0.1;
}
}
public void print(){

System.out.println("Tyota Car:"+amount);
}

}
class HondaCityCar implements Vehicle{
public Cost getCostSpecification(){
return new HondaCost();
}
public Features getFeatures(){

return new HondaFeatures();
}
}

class HondaFeatures implements Features{
public void print(){

System.out.println("Honda City Car Features");
}
}
class HondaCost implements Cost
{
int amount=100000;
public void checkPaymentType(String type){

if(type.equalsIgnoreCase("Loan")){
amount+=amount*0.3;
}else{
amount-=amount*0.1;
}
}
public void print(){

System.out.println("HondayCity Car:"+amount);
}

}
class Client
{
public static void main(String args[]){

Vehicle car= VehicleFactory.getVehicle("TyotaCar");
Features carview=car.getFeatures();
carview.print();
Cost cost=car.getCostSpecification();
cost.checkPaymentType("Cash");
cost.print();


car= VehicleFactory.getVehicle("HondaCityCar");
carview=car.getFeatures();
carview.print();
cost=car.getCostSpecification();
cost.checkPaymentType("Loan");
cost.print();

}
}

Abstract Factory implementation could be seen in JDBC,JMS implementations. As in JDBC we change only the driver name and the internal implementation to execute the query according to database and retrieval of data is not exposed to the users of the API.

To Practice
1. Create a Customer, Account and Credit card class. When the customerid is given as input a single view of information related that customer should be displayed.
• Customer details like address, name, date of birth etc.
• Account information like accountno, opening balance, current balance, type of account
• Card information like cardno, available balance, date of last payment, expiry date should be printed.

2. Re-model the above implementation to provide the features for any four wheelers.

No comments:

Post a Comment