Simple factory pattern starting from the design pattern

Preface

The simple factory pattern is not one of the 23 GoF design patterns, but it is generally used as the starting point for learning design patterns. In the simple factory pattern, you can return instances of different classes according to different parameters. The simple factory pattern specifically defines a class to be responsible for creating instances of other classes. This class is called a factory class. The created instances usually have a common parent class.

1. Simple factory model

Simple Factory Pattern (Simple Factory Pattern), also known as Static Factory Method Pattern (Static Factory Method Pattern), belongs to the class creation pattern.
Definition : Define a class and return different class instances according to different parameters. These classes have public parent classes and some public methods. The simple factory pattern does not belong to the GoF design pattern, it is the simplest factory pattern.
Frequency of use : 4-star
simple factory model structure diagram :
Insert picture description here
Advantages :
1. The factory class contains the necessary logical judgments, which can determine when to create an instance of which product. The client can be exempted from the responsibility of directly creating product objects, and it is very convenient to create corresponding products. The responsibilities of factories and products are clearly distinguished.
2. The client does not need to know the class name of the specific product created, only the parameters.
3. It is also possible to introduce configuration files to replace and add new specific product categories without modifying the client code.
Disadvantages :
1. The simple factory model has a single factory type and is responsible for the creation of all products. The responsibilities are too heavy. Once abnormal, the entire system will be affected. And the factory code will be very bloated, which violates the principle of high aggregation.
2. Using the simple factory model will increase the number of classes in the system (introducing new factory classes), increase the complexity of the system and the difficulty of understanding
3. It is difficult to expand the system. Once a new product is added, the factory logic has to be modified. For a long time, the logic may be too complicated.
4. The simple factory model uses the static factory method, causing the factory role to fail to form a hierarchical structure based on inheritance.
Application scenarios :
For relatively few product types, consider using a simple factory model. Clients that use the simple factory pattern only need to pass in the parameters of the factory class, do not need to care about the logic of how to create the object, and can easily create the required products.

2. Graphic factory of simple factory pattern example

1. Example description

Use the simple factory pattern to design a drawing tool class that can create different geometric shapes (Shape), such as Circle, Rectangle and Triangle objects. Each geometric figure has draw and erase The two methods of erase require an UnsupportedShapeException to be thrown when drawing unsupported geometric figures, draw the class diagram and implement it programmatically.

2. Instance class diagram

Insert picture description here

3. Example code

In this example, Shape acts as an abstract product, its subclasses Circle, Rectangle, and Triangle act as concrete products, and ShapeFactory is a factory class.

//形状接口:抽象产品
interface Shape
{
    
    
	public void draw();
	public void erase();
}

//圆形类:具体产品
class Circle implements Shape
{
    
    
	public void draw()
	{
    
    
		System.out.println("绘制圆形");
	}
	public void earse()
	{
    
    
		System.out.println("删除圆形");
	}
}
//矩形类:具体产品
class Rectangle implements Shape
{
    
    
	public void draw()
	{
    
    
		System.out.println("绘制矩形");
	}
	public void earse()
	{
    
    
		System.out.println("删除矩形");
	}
}
//三角形类:具体产品
class Triangle implements Shape
{
    
    
	public void draw()
	{
    
    
		System.out.println("绘制三角形");
	}
	public void earse()
	{
    
    
		System.out.println("删除三角形");
	}
}

//形状工厂类:工厂
class ShapeFactory
{
    
    
	//静态工厂方法
	public static Shape createShape(String type)throws UnsupportedShapeException
	{
    
    
		if(type.equalsIgnoreCase("c")
		{
    
    
			return new Circle();
		}
		else if(type.equalsIgnoreCase("r")
		{
    
    
			return new Rectangle();
		} 
		else if(type.equalsIgnoreCase("t")
		{
    
    
			return new Triangle();
		}
		else
		{
    
    
			throw new UnsupportedShapeException("不支持该形状!");
		}
	}
}
//自定义异常类
class UnsupportedShapeException extends Exception
{
    
    
	public UnsupportedShapeException(String message)
	{
    
    
		super(message);
	}
}

Client test code:

class Client
{
    
    
	public static vodi main(String[] args)
	{
    
    
		try
		{
    
    
			Shape shape;
			shape = ShapeFactory.createShape("r");
			shape.draw();
			shape.erase();
		}
		catch(UnsupportedShapeException e)
		{
    
    
			System.out.println(e.getMessage());
		}
	}
}

The results of the operation are as follows:

绘制矩形
删除矩形

references

[1] Simple factory mode
[2] Design mode training course (2nd edition) Tsinghua University Press, edited by Liu Wei

Guess you like

Origin blog.csdn.net/weixin_43594279/article/details/114919377