Abstract Factory Pattern provides an interface for creating a factory of related objects without explicitly specifying their classes. In other words, the Abstract Factory is a super-factory which creates other factories (Factory of factories).
Implementation
In the above URL class diagram,
AbstractFactory defines a Factory Method per product:
ConcreteFactoryA and ConcreteFactoryB implement methods to create concrete product A and B
AbstractProductA declares the interface of product A
AbstractProductB declares the interface of product B
Example code:
1 2 3 4 5 6 |
package net.tecbar.designpattern.abstractfactory; public interface AbstractProduct1 { } |
1 2 3 4 5 6 |
package net.tecbar.designpattern.abstractfactory; public interface AbstractProduct2 { } |
1 2 3 4 5 6 7 8 9 |
package net.tecbar.designpattern.abstractfactory; public class FactoryAProduct1 implements AbstractProduct1 { public FactoryAProduct1() { System.out.println("FactoryAProduct1 created"); } } |
1 2 3 4 5 6 7 8 9 |
package net.tecbar.designpattern.abstractfactory; public class FactoryAProduct2 implements AbstractProduct2 { public FactoryAProduct2() { System.out.println("FactoryAProduct2 created"); } } |
1 2 3 4 5 6 7 8 9 |
package net.tecbar.designpattern.abstractfactory; public class FactoryBProduct1 implements AbstractProduct1 { public FactoryBProduct1() { System.out.println("FactoryBProduct1 created"); } } |
1 2 3 4 5 6 7 8 9 |
package net.tecbar.designpattern.abstractfactory; public class FactoryBProduct2 implements AbstractProduct2 { public FactoryBProduct2() { System.out.println("FactoryBProduct2 created"); } } |
1 2 3 4 5 6 7 8 |
package net.tecbar.designpattern.abstractfactory; public interface AbstractFactory { AbstractProduct1 createProduct1(); AbstractProduct2 createProduct2(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package net.tecbar.designpattern.abstractfactory; public class ConcreteFactoryA implements AbstractFactory { private final static ConcreteFactoryA factory = new ConcreteFactoryA(); public static ConcreteFactoryA getInstance() { return factory; } @Override public AbstractProduct1 createProduct1() { return new FactoryAProduct1(); } @Override public AbstractProduct2 createProduct2() { return new FactoryAProduct2(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package net.tecbar.designpattern.abstractfactory; public class ConcreteFactoryB implements AbstractFactory { private final static ConcreteFactoryB factory = new ConcreteFactoryB(); public static ConcreteFactoryB getInstance() { return factory; } @Override public AbstractProduct1 createProduct1() { return new FactoryBProduct1(); } @Override public AbstractProduct2 createProduct2() { return new FactoryBProduct2(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package net.tecbar.designpattern.abstractfactory; public class FactoryCreator { static AbstractFactory getFactory(String choice){ if("a".equals(choice)){ return ConcreteFactoryA.getInstance(); } else if("b".equals(choice)){ return ConcreteFactoryB.getInstance(); } else { throw new RuntimeException("Cannot find factory"); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package net.tecbar.designpattern.abstractfactory; public class AbstractFactoryDemo { public static void main(String[] args) { AbstractFactory factory = FactoryCreator.getFactory("a"); AbstractProduct1 product = factory.createProduct1(); AbstractFactory factory2 = FactoryCreator.getFactory("b"); AbstractProduct2 product2 = factory2.createProduct2(); } } |
Output:
FactoryAProduct1 created
FactoryBProduct2 created
Pingback: Software Design Patterns