Design Pattern Abstract Factory Pattern - Design Patterns

What is Design Pattern Abstract Factory Pattern?

Abstract Factory patterns effort about a super-factory which creates other factories. This factory is also called as factory of factories. This kind of design pattern comes below creational pattern as this pattern provides single of the best ways to create an entity.

In Abstract Factory pattern a line is accountable for creating a factory of related objects without openly specifies their classes. Each generated factory can give the substance as per the Factory pattern.

Implementation

We are available to create a form and Colour interfaces and real classes implementing these interfaces. We make an abstract factory class summary Factory as after that step. Factory classes Shape Factory and ColorFactoryare define where each factory extends Abstract Factory. A factory creator/generator class Factory creator is created.

AbstractFactoryPatternDemo, our presentation class uses Factory maker to get an Abstract Factory object. It will pass information (CIRCLE / RECTANGLE / SQUARE for Shape) to Abstract Factory to get the form of entity it needs. It also passes in order (RED / GREEN / BLUE for Colour) to Abstract Factory to get the kind of object it requirements.
abstractfactory_pattern_uml_diagram

Step 1

Make an interface for Shapes.

Shape.java

Step 2

Create existing classes implementing the similar interface.

Rectangle.java

Square.java

Circle.java

Step 3

Create an interface for Colours.

Color.java

Step4

Generate concrete classes implement the same interface.

Red.java

Green.java

Blue.java

Step 5

Generate an Abstract class to get factories for Colour and Shape Objects.

AbstractFactory.java

Step 6

Construct Factory classes extending Abstract Factory to generate entity of real class based on known in order.

ShapeFactory.java

ColorFactory.java

Step 7

Create a works creator/producer class to get factories by passing in sequence such as form or Colour.

FactoryProducer.java

Step 8

Use the Factory maker to get Abstract Factory in direct to get factories of real classes by transient information such as form.

AbstractFactoryPatternDemo.java

Step 9

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics