The document outlines the implementation of the Abstract Factory design pattern in Java for creating shapes. It includes an interface for shapes, concrete classes for different shapes, and factory classes that produce shape objects based on whether they are normal or rounded. The final step demonstrates the usage of the factories to create and draw different shapes, verifying the output of the draw methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views
Abstract FactoryPatternExample
The document outlines the implementation of the Abstract Factory design pattern in Java for creating shapes. It includes an interface for shapes, concrete classes for different shapes, and factory classes that produce shape objects based on whether they are normal or rounded. The final step demonstrates the usage of the factories to create and draw different shapes, verifying the output of the draw methods.
@Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } Step 3 Create an Abstract class to get factories for Normal and Rounded Shape Objects.
AbstractFactory.java
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ; } Step 4 Create Factory classes extending AbstractFactory to generate object of concrete class based on given information.
ShapeFactory.java
public class ShapeFactory extends AbstractFactory {
@Override public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } } RoundedShapeFactory.java
public class RoundedShapeFactory extends AbstractFactory {
@Override public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new RoundedRectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new RoundedSquare(); } return null; } } Step 5 Create a Factory generator/producer class to get factories by passing an information such as Shape
FactoryProducer.java
public class FactoryProducer {
public static AbstractFactory getFactory(boolean rounded){ if(rounded){ return new RoundedShapeFactory(); }else{ return new ShapeFactory(); } } } Step 6 Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing an information such as type.
AbstractFactoryPatternDemo.java
public class AbstractFactoryPatternDemo {
public static void main(String[] args) { //get shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory(false); //get an object of Shape Rectangle Shape shape1 = shapeFactory.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape1.draw(); //get an object of Shape Square Shape shape2 = shapeFactory.getShape("SQUARE"); //call draw method of Shape Square shape2.draw(); //get shape factory AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true); //get an object of Shape Rectangle Shape shape3 = shapeFactory1.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape3.draw(); //get an object of Shape Square Shape shape4 = shapeFactory1.getShape("SQUARE"); //call draw method of Shape Square shape4.draw();