Creational Design Pattern: Abstract Factory Pattern - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Creational Design Pattern: Abstract Factory Pattern

Share This

The Abstract Factory Pattern involves defining an interface or abstract class that is responsible for constructing families of linked or dependent items, without explicitly declaring the concrete sub-classes of these objects.Abstract Factory allows a class to return a factory of classes. The Abstract Factory Pattern is considered to be one level higher than the Factory Pattern due to this reason. 
 
The advantage of the Abstract Factory Pattern is that it allows for the creation of families of related objects without specifying their concrete classes.

The Abstract Factory Pattern separates the client code from the specific implementation classes. It facilitates the exchange of object families. It fosters uniformity among entities.

Application of the Abstract Factory Pattern

  • When the system requires independence from the creation, composition, and representation of its objects. 
  • When there is a need to use a group of objects that are related to each other, it is necessary to impose this restriction. 
  • When you desire to offer a collection of objects that conceals their implementations and just exposes their interfaces. 
  • When the system requires configuration with one of several object families.
Source code of Shape.java
package com.t4b.test.java.dp.cp.afp; interface Shape { void draw(); }
Source code of CircleShape.java
package com.t4b.test.java.dp.cp.afp; class CircleShape implements Shape { @Override public void draw() { System.out.println("I am in Circle draw!"); } }
Source code of RectangleShape.java
package com.t4b.test.java.dp.cp.afp; class RectangleShape implements Shape { @Override public void draw() { System.out.println("I am in Rectangle draw!"); } }
Source code of SquareShape.java
package com.t4b.test.java.dp.cp.afp; class SquareShape implements Shape { @Override public void draw() { System.out.println("I am in Square draw!"); } }
Source code of Printer.java
package com.t4b.test.java.dp.cp.afp; interface Printer { void print(); }
Source code of PaperPrinter.java
package com.t4b.test.java.dp.cp.afp; class PaperPrinter implements Printer { @Override public void print() { System.out.println("I am in PaperPrinter print!"); } }
Source code of ScreenPrinter.java
package com.t4b.test.java.dp.cp.afp; class ScreenPrinter implements Printer { @Override public void print() { System.out.println("I am in ScreenPrinter print!"); } }
Source code of WebPrinter.java
package com.t4b.test.java.dp.cp.afp; class WebPrinter implements Printer { @Override public void print() { System.out.println("I am in WebPrinter print!"); } }
Source code of AbstractFactory.java
package com.t4b.test.java.dp.cp.afp; abstract class AbstractFactory { abstract Printer getPrinter(String type); abstract Shape getShape(String shape); }
Source code of ShapeFactory.java
package com.t4b.test.java.dp.cp.afp; class ShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType) { if (shapeType == null) { return null; } if (shapeType.equalsIgnoreCase("Circle")) { return new CircleShape(); } else if (shapeType.equalsIgnoreCase("Rectangle")) { return new RectangleShape(); } else if (shapeType.equalsIgnoreCase("Square")) { return new SquareShape(); } return null; } @Override Printer getPrinter(String type) { return null; } }
Source code of PrinterFactory.java
package com.t4b.test.java.dp.cp.afp; class PrinterFactory extends AbstractFactory { @Override public Shape getShape(String shapeType) { return null; } @Override Printer getPrinter(String type) { if (type == null) { return null; } if (type.equalsIgnoreCase("paper")) { return new PaperPrinter(); } else if (type.equalsIgnoreCase("web")) { return new WebPrinter(); } else if (type.equalsIgnoreCase("Screen")) { return new ScreenPrinter(); } return null; } }
Source code of FactoryProducer.java
package com.t4b.test.java.dp.cp.afp; class FactoryProducer { public static AbstractFactory getFactory(String choice) { if (choice.equalsIgnoreCase("Shape")) { return new ShapeFactory(); } else if (choice.equalsIgnoreCase("Printer")) { return new PrinterFactory(); } return null; } }
Source code of TestMain.java
package com.t4b.test.java.dp.cp.afp; public class TestMain { public static void main(String[] args) { AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape"); Shape shapeCircle = shapeFactory.getShape("Circle"); Shape shapeSquare = shapeFactory.getShape("Square"); Shape shapeRectangle = shapeFactory.getShape("Rectangle"); shapeCircle.draw(); shapeSquare.draw(); shapeRectangle.draw(); AbstractFactory printerFactory = FactoryProducer.getFactory("Printer"); Printer paperPrinter = printerFactory.getPrinter("Paper"); Printer webPrinter = printerFactory.getPrinter("Web"); Printer screenPrinter = printerFactory.getPrinter("Screen"); paperPrinter.print(); webPrinter.print(); screenPrinter.print(); } }

Happy Exploring!

No comments:

Post a Comment