Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Abstract Factory Pattern

Abstract Factory Pattern

The Abstract Factory Pattern creates other factories around a super factory. This super factory is also called the factory of other factories. This type of design pattern belongs to the creational pattern category, which provides an optimal way to create objects.

In the abstract factory pattern, an interface is a factory responsible for creating a related object, without explicitly specifying their classes. Each generated factory can provide objects according to the factory pattern.

The abstract factory pattern provides an interface for creating a series of related or interdependent objects without specifying concrete implementation classes. By using the abstract factory pattern, the client can be decoupled from the creation process of concrete products, allowing the client to create a family of products through the factory interface.

Summary

Intent

Provide an interface for creating a series of related or interdependent objects without specifying their concrete classes.

Main problem solved

The problem of interface selection.

Applicable scenarios

When a system needs to create multiple related or dependent objects without specifying concrete classes.

Solution

Define multiple products in a product family, with concrete factories implementing the methods to create these products.

Key code

Aggregate creation methods of multiple similar products in one factory.

Application Example

Suppose there are different types of wardrobes; each wardrobe (concrete factory) can only store one type of clothing (a set of concrete products), such as business wear, fashion wear, etc. Each set of clothing includes a specific top and trousers (concrete products). All wardrobes are concrete implementations of the wardrobe class (abstract factory), and all tops and trousers respectively implement the top interface and trousers interface (abstract products).

Advantages

  1. Ensure that objects of the same product family work together.
  2. The client does not need to know the concrete class of each object, simplifying the code.

Disadvantages

It is very difficult to extend a product family. Adding a new product family requires modifying the abstract factory and all concrete factory code.

Use Cases

  1. When QQ changes skins, the whole set of skins is changed together.
  2. When creating cross-platform applications, generate programs for different operating systems.

Notes

It is relatively easy to add a new product family, while adding a new product hierarchy is more difficult.

structures

The abstract factory pattern contains the following main roles:

The abstract factory pattern usually involves a family of related products, with each concrete factory class responsible for creating the concrete products in that family. The client creates product objects by using the abstract factory interface, without directly using the implementation classes of the concrete products.

Create a class that implements

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

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

UML diagram of the Abstract Factory pattern

Step 1

Create an interface for shapes.

Shape.java

public

interface

Shape

{

void

draw

(

)

;

}

Step 2

Create concrete classes that implement the interface.

Rectangle.java

Rectangle.java

public

class

Rectangle

implements

Shape

{

@

Override

public

void

draw

(

)

{

System

.

out

.

println

(

"

Inside Rectangle::draw() method.

"

)

;

}

}

Square.java

public

class

Square

implements

Shape

{

@

Override

public

void

draw

(

)

{

System

.

out

.

println

(

"

Inside Square::draw() method.

"

)

;

}

}

Circle.java

public

class

Circle

implements

Shape

{

@

Override

public

void

draw

(

)

{

System

.

out

.

println

(

"

Inside Circle::draw() method.

"

)

;

}

}

Step 3

Create an interface for colors.

Color.java

public

interface

Color

{

void

fill

(

)

;

}

Step 4

Create concrete classes that implement the interface.

Red.java

public

class

Red

implements

Color

{

@

Override

public

void

fill

(

)

{

System

.

out

.

println

(

"

Inside Red::fill() method.

"

)

;

}

}

Green.java

public

class

Green

implements

Color

{

@

Override

public

void

fill

(

)

{

System

.

out

.

println

(

"

Inside Green::fill() method.

"

)

;

}

}

Blue.java

public

class

Blue

implements

Color

{

@

Override

public

void

fill

(

)

{

System

.

out

.

println

(

"

Inside Blue::fill() method.

"

)

;

}

}

Step 5

Create abstract classes for Color and Shape objects to get factories.

AbstractFactory.java

public

abstract

class

AbstractFactory

{

public

abstract

Color

getColor

(

String

color

)

;

public

abstract

Shape

getShape

(

String

shape

)

;

}

Step 6

Create factory classes extending AbstractFactory, generating objects of concrete classes based on the given information.

ShapeFactory.java

public

class

ShapeFactory

extends

AbstractFactory

{

@

Override

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

;

}

@

Override

public

Color

getColor

(

String

color

)

{

return

null

;

}

}

ColorFactory.java

public

class

ColorFactory

extends

AbstractFactory

{

@

Override

public

Shape

getShape

(

String

shapeType

)

{

return

null

;

}

@

Override

public

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 creator/generator class to get factories by passing shape or color information.

FactoryProducer.java

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 FactoryProducer to get AbstractFactory, and get objects of concrete classes by passing type information.

AbstractFactoryPatternDemo.java

public

class

AbstractFactoryPatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

//

Get the shape factory

AbstractFactory

shapeFactory

=

FactoryProducer

.

getFactory

(

"

SHAPE

"

)

;

//

Get an object with shape Circle

Shape

shape1

=

shapeFactory

.

getShape

(

"

CIRCLE

"

)

;

//

Call Circle's draw method

shape1

.

draw

(

)

;

//

Get an object with shape Rectangle

Shape

shape2

=

shapeFactory

.

getShape

(

"

RECTANGLE

"

)

;

//

Call Rectangle's draw method

shape2

.

draw

(

)

;

//

Get an object with shape Square

Shape

shape3

=

shapeFactory

.

getShape

(

"

SQUARE

"

)

;

//

Call Square's draw method

shape3

.

draw

(

)

;

//

Get the color factory

AbstractFactory

colorFactory

=

FactoryProducer

.

getFactory

(

"

COLOR

"

)

;

//

Get an object with color Red

Color

color1

=

colorFactory

.

getColor

(

"

RED

"

)

;

//

Call Red's fill method

color1

.

fill

(

)

;

//

Get an object with color Green

Color

color2

=

colorFactory

.

getColor

(

"

GREEN

"

)

;

//

Call Green's fill method

color2

.

fill

(

)

;

//

Get an object with color Blue

Color

color3

=

colorFactory

.

getColor

(

"

BLUE

"

)

;

//

Call Blue's fill method

color3

.

fill

(

)

;

}

}

Step 9

Run the program, output result:



Inside Circle::draw() method.

Inside Rectangle::draw() method.

Inside Square::draw() method.

Inside Red::fill() method.

Inside Green::fill() method.

Inside Blue::fill() method.