Core Java Design Pattern : Creational Pattern : Abstract Factory Design Pattern
Abstract Factory Design Pattern |
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
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.
There is a concrete subclass of WidgetFactory for each look-and-feel standard. Each subclass implements the operations to create the appropriate widget for the look and feel. For example, the CreateScrollBar operation on the MotifWidgetFactory instantiates and returns a Motif scroll bar, while the corresponding operation on the PMWidgetFactory returns a scroll bar for Presentation Manager. Clients create widgets solely through the WidgetFactory interface and have no knowledge of the classes that implement widgets for a particular look and feel. In other words, clients only have to commit to an interface defined by an abstract class, not a particular concrete class.
A WidgetFactory also enforces dependencies between the concrete widget classes. A Motif scroll bar should be used with a Motif button and a Motif text editor, and that constraint is enforced automatically as a consequence of using a MotifWidgetFactory.
Examples |
InterestCalculator.java
package com.javaskool.common;
public interface InterestCalculator {
public double calculate(double amt);
}
InterestFactoryAnyBank.java
package com.javaskool.factory;
import com.javaskool.common.InterestCalculator;
public abstract class InterestFactoryAnyBank {
public static InterestFactoryAnyBank getInterestFactory(char type)
{
switch(type){
case 'h': return new HDFCInterestFactory();
case 'i': return new ICICIInterestFactory();
}
return null;
}
public abstract InterestCalculator create(char type);
}
HDFCInterestFactory.java
package com.javaskool.factory;
import com.javaskool.hdfc.model.HDFCFDAccountCalculator;
import com.javaskool.hdfc.model.HDFCInterestCalculator;
import com.javaskool.hdfc.model.HDFCSavingAccountInterestCalculator;
public class HDFCInterestFactory extends InterestFactoryAnyBank{
public HDFCInterestCalculator create(char type){
switch(type){
case 's': return new HDFCSavingAccountInterestCalculator();
case 'f': return new HDFCFDAccountCalculator();
}
return null;
}
}
ICICIInterestFactory.java
package com.javaskool.factory;
import com.javaskool.common.InterestCalculator;
import com.javaskool.icici.model.ICICIFDAccountCalculator;
import com.javaskool.icici.model.ICICISavingAccountInterestCalculator;
public class ICICIInterestFactory extends InterestFactoryAnyBank {
public InterestCalculator create(char type){
switch(type){
case 's': return new ICICISavingAccountInterestCalculator();
case 'f': return new ICICIFDAccountCalculator();
}
return null;
}
}
For ICICI Bank
ICICIInterestCalculator.java
package com.javaskool.icici.model;
import com.javaskool.common.InterestCalculator;
public interface ICICIInterestCalculator extends InterestCalculator{
public double calculate(double amt);
}
ICICISavingAccountInterestCalculator.java
package com.javaskool.icici.model;
public class ICICISavingAccountInterestCalculator implements ICICIInterestCalculator {
private double roi=2.5;
@Override
public double calculate(double amt) {
// TODO Auto-generated method stub
return (roi/100.0)*amt/12.0;
}
}
ICICIFDAccountCalculator.java
package com.javaskool.icici.model;
public class ICICIFDAccountCalculator implements ICICIInterestCalculator {
private double roi=6.0;
private int duration=3;
@Override
public double calculate(double amt) {
// TODO Auto-generated method stub
return roi/100.0*amt*duration;
}
}
For HDFC Bank
HDFCInterestCalculator.java
package com.javaskool.hdfc.model;
import com.javaskool.common.InterestCalculator;
public interface HDFCInterestCalculator extends InterestCalculator{
public double calculate(double amt);
}
HDFCSavingAccountInterestCalculator.java
package com.javaskool.hdfc.model;
public class HDFCSavingAccountInterestCalculator implements HDFCInterestCalculator {
private double roi=3.5;
@Override
public double calculate(double amt) {
// TODO Auto-generated method stub
return (roi/100.0)*amt/12.0;
}
}
HDFCFDAccountCalculator.java
package com.javaskool.hdfc.model;
public class HDFCFDAccountCalculator implements HDFCInterestCalculator {
private double roi=8.0;
private int duration=3;
@Override
public double calculate(double amt) {
// TODO Auto-generated method stub
return roi/100.0*amt*duration;
}
}
TestDrive.java
/*
Here, we are having common calculator for diff model of HDFC and ICICI.
and client can get the object for FD or Saving for HDFC and ICICI bank
and get interest Calculated.
Abstract Factory Method Pattern :- Provide an interface for creating
families of related or dependent objects without specifying
their concrete classes.
*/
package com.javaskool;
import com.javaskool.common.InterestCalculator;
import com.javaskool.factory.InterestFactoryAnyBank;
public class TestDrive {
public static void main(String arg[]){
InterestFactoryAnyBank factory=InterestFactoryAnyBank.getInterestFactory('i');
InterestCalculator ic=(InterestCalculator)factory.create('s');
System.out.println(ic.calculate(1000));
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
2.0833333333333335
Recent Comments