[Design Pattern Notes] Factory Pattern

Introduction:

One of the most used design patterns in java.

Belongs to the creation mode. Provides an optimal way to create objects.

In the factory pattern, creating an object does not expose the creation logic to the client, and points to the newly created object by using a common interface.

Intent: Define an interface to create an object, and let its subclasses decide which factory class to instantiate. The factory pattern defers creation to subclasses.

Solving the problem: the problem of interface selection

When to use: When explicitly planning to create different instances under different conditions

How to solve: let its subclass implement the factory interface, and return an abstract product

Key code:

class Factory{
   接口 get接口(参数){

    if(参数 is a){
      return new A();
   }else if...
}
}

Example: If you need a car, you can pick it up directly from the factory, regardless of how the car is made

advantage:

① If the caller wants to create an object, it only needs to know the name.

②High scalability, if you want to add a product. Just extend a factory class.

③Shield the specific implementation of the product, and the caller only cares about the interface of the product

shortcoming:

Each time a product is added, a specific class and object implementation factory need to be added, which doubles the number of classes in the system, increases the complexity of the system to a certain extent, and also increases the dependency of specific classes in the system.

scenes to be used:

①Log recorder : The record may be recorded to the local hard disk, system time, remote server, etc. The user can choose where to record the log.

②Database access : when the user does not only know which type of database the system uses at the end, and when the database may change.

③ To design a framework for connecting to a server , three protocols are needed, "POP3", "IMAP", and "HTTP". These three can be used as product classes to jointly implement an interface.

Precautions:

The factory method pattern can be used anywhere complex objects need to be generated.

But simple objects, especially objects that can be created only by new, do not need to use the factory pattern.

============================

Code:

Create a Shape interface:

public interface shape {
    void draw();
}

An implementation class that implements the Shape interface:

public class Rectangle implements shape {
    @Override
    public void draw() {
        System.out.println("rectangle draw");
    }
}
public class Circle implements shape{
    @Override
    public void draw() {
        System.out.println("circle draw");
    }
}
public class Squre implements shape{
    @Override
    public void draw() {
        System.out.println("square draw");
    }
}

Create a factory that generates entity class objects based on given information

public class ShapeFatory {
    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 Squre();
        }
        return null;
    }
}

public class FactoryPatternDemo {
    public static void main(String[] args) {
        //创建一个工厂
        ShapeFatory shapeFatory=new ShapeFatory();
        //获取Circle对象
        shape shape1=shapeFatory.getShape("circle");
        shape1.draw();
        //获取Rectangle对象
        shape shape2=shapeFatory.getShape("rectangle");
        shape2.draw();
        //获取squre对象
        shape shape3=shapeFatory.getShape("square");
        shape3.draw();
    }
}

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124087946