Design Patterns - Abstract Factory
Design Patterns - Abstract Factory: works around a super-factory which creates other factories.
// Step 1: Create an interface for shapes
public interface Shape {
void draw();
}
// Step 2: Create concrete classes implementing the same interface.
public class Rectangle implements Shape {
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public class Square implements Shape {
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
public class Circle implements Shape {
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
// Step 3: Create an interface for colors:
public interface Color {
void fill();
}
// Step 4: Create concrete classes implementing the Color interface
public class Red implements Color {
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
public class Green implements Color {
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
public class Blue implements Color {
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
// Step 5: Create an Abstract class to get factories for Color and Shape Objects.
public abstract class AbstractFactory {
abstract Color getColor(String color);
abstract Shape getShape(String shape) ;
}
// Step 6: Create Factory classes extending AbstractFactory to generate object
of concrete class based on given information.
public class ShapeFactory extends AbstractFactory {
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
Color getColor(String color) {
return null;
}
}
public class ColorFactory extends AbstractFactory {
public Shape getShape(String shapeType){
return null;
}
Color getColor(String color) {
if (color == null) {
return null;
}
if (color.equalsIgnoreCase("RED")) {
return new Red();
} else if(color.equalsIgnoreCase("GREEN")){
return new Green();
} else if(color.equalsIgnoreCase("BLUE")) {
return new Blue();
}
return null;
}
}
// Step 7: Create a Factory generator/producer class to get factories by
passing an information such as Shape or Color.
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if (choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}
return null;
}
}
// Step 8: Use the FactoryProducer to get AbstractFactory in order to get
// factories of concrete classes by passing an information such as type.
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
Color color1 = colorFactory.getColor("RED");
color1.fill();
page revision: 2, last edited: 08 Nov 2016 16:41