Abstract Factory Mode

Abstract Factory Pattern is the creation of other factories around a super factory. This super factory is also known as the factory of other factories. This type of design pattern is a creative model that provides the best way to create objects.

In the abstract factory pattern, interfaces are the factories responsible for creating a related object, and do not need to explicitly specify their classes. Each generated factory can provide objects in factory mode.

Introduction

Intent: Provides an interface to create a series of related or interdependent objects without specifying their concrete classes.

Main solution: Mainly solve the problem of interface selection.

When to use: The system's products have more than one product family, and the system consumes only one of the products.

How to solve: Define multiple products in one product family.

Key Code: Aggregate multiple products in one factory.

Application example: Work, in order to participate in some gatherings, there must be two or more sets of clothes, such as business suits (sets, a series of specific products), fashion suits (sets, A series of specific products), even for a family, there may be business women's wear, business men's wear, fashion women's wear, fashion men's wear, these are also complete sets, that is, a series of specific products. Suppose a situation (in reality, it doesn't exist, otherwise it can't enter communism, but it helps to illustrate the abstract factory model). In your home, a certain wardrobe (specific factory) can only store one kind of this. The clothes (set, a series of specific products), naturally take out this set of clothes every time from the wardrobe. Understand the idea of OO, all the wardrobes (specific factories) are one of the wardrobe type (abstract factory), and each set of clothes includes a specific top (a specific product), pants (a specific Products), these specific tops are actually tops (abstract products), and the specific pants are also pants (another abstract product).

Advantages: When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects from the same product family.

Disadvantages: Product family extensions are very difficult. To add a product to a series, add code to the abstract Creator and add code to it.

Usage scenarios: 1, QQ change skin, a whole set together. 2. Generate programs for different operating systems.

Note: Product families are difficult to expand and product grades are easy to expand.

Implementation

We will create Shape and Color interfaces and entity classes that implement these interfaces. The next step is to create the abstract factory class AbstractFactory. Then define the factory class ShapeFactory and ColorFactory, both of which extend AbstractFactory. Then create a factory creator/builder class FactoryProducer.

AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get the AbstractFactory object. It will pass the shape information Shape(CIRCLE / RECTANGLE / SQUARE) to AbstractFactory to get the type of object it needs. It also passes the color information Color(RED / GREEN / BLUE) to AbstractFactory to get the type of object it needs.

abstractfactory pattern uml diagram

Step 1

Create an interface for the shape.

Shape.java

public interface Shape { void draw(); }

Step 2

Create an entity class that implements the interface.

Rectangle.java

Rectangle.java

public class Rectangle implements Shape { @Override public void draw() { System.out< Span class="hl-code">.println("Inside Rectangle::draw() method."); } }

Square.java

public class Square implements Shape { @Override public void draw() { System.out< Span class="hl-code">.println("Inside Square::draw() method."); } }

Circle.java

public class Circle implements Shape { @Override public void draw() { System.out< Span class="hl-code">.println("Inside Circle::draw() method."); } }

Step 3

Create an interface for the color.

Color.java

public interface Color { void fill(); }

Step 4

Create an entity class that implements the interface.

Red.java

public class Red implements Color { @Override public void fill() { System.out< Span class="hl-code">.println("Inside Red::fill() method."); } }

Green.java

public class Green implements Color { @Override public void fill() { System.out< Span class="hl-code">.println("Inside Green::fill() method."); } }

Blue.java

public class Blue implements Color { @Override public void fill() { System.out< Span class="hl-code">.println("Inside Blue::fill() method."); } }

Step 5

Create an abstract class for the Color and Shape objects to get the factory.

AbstractFactory.java

public abstract class AbstractFactory { public abstract Color getColor(String color); public abstract Shape getShape(String shape) ; }

Step 6

Create a factory class that extends AbstractFactory to generate objects of the entity class based on the given information.

ShapeFactory.java

public class ShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ if(shapeType< Span class="hl-code"> == null){ return null; } if(shapeType< Span class="hl-code">.equalsIgnoreCase("CIRCLE") ){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } @Override public Color getColor(String color) { return }





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition