This is the sixth day of my participation in the August Text Challenge.More challenges in August

Factory pattern, this type of design pattern is the creation pattern, which provides a way to create objects.

Factory Model Introduction

For example, if a shoe factory distributes each pair of shoes to various stores, it will not sell a pair of shoes without making a pair.

So-> In factory mode, we create objects without exposing creation logic to clients, and by using a common interface to point to newly created objects.

introduce

Intent: Define an interface to create an object and let subclasses decide which factory class to instantiate. The factory pattern delays the creation of the object to subclasses.

Main solution: The main solution is interface selection.

When to use: Explicitly plan to create different instances under different conditions.

How to fix it: Subclasses implement the factory interface and return an abstract product.

Key code: The creation process is performed in its subclass.

Example of application: You need a car that can be picked up directly from the factory, regardless of how the car is made and the implementation inside the car.

Advantages: 1. A caller who wants to create an object needs only to know its name. 2. High scalability. If you want to add a product, just extend a factory class. 3. Mask the concrete implementation of the product, and the caller only cares about the interface of the product.

Disadvantages: Every time a product is added, a concrete class and object implementation factory need to be added, which doubles the number of classes in the system, increasing the complexity of the system to a certain extent, but also increasing the dependence of concrete classes in the system. This is not a good thing.

Usage scenarios: 1. Log logger: Logs may be recorded to local hard disks, system events, and remote servers. Users can choose where to record logs. 2. Database access, when the user does not know which kind of database is used in the last system, and the database may change. 3. Design a framework to connect the server, need three protocols, “POP3”, “IMAP”, “HTTP”, can take these three as a product class, common implementation of an interface.

Note: As a class creation pattern, the factory method pattern can be used anywhere complex objects need to be generated. It is important to note that complex objects are suitable for factory patterns, while simple objects, especially those that can be created by simply using New, do not need factory patterns. If you use the factory pattern, you need to introduce a factory class, which adds complexity to the system.

Singleton schema factory

public abstract class Singleton<T> { private T instance; protected abstract T create(); public final T get(){ if(Objects.isNull(instance)){ synchronized (this){ if(Objects.isNull(instance)){ instance = create(); } } } return instance; }}Copy the code
public class Factory {
    public static A getA(){
        return new Singleton<A>(){
            @Override
            protected A create() {
                return new A();
            }
        }.get();
    }

    public static B getB(){
        return new Singleton<B>(){

            @Override
            protected B create() {
                return new B();
            }
        }.get();
    }
}

class A{}
class B{}
Copy the code