When dealing with the login page business, the need to pop up a custom alert box when the user name is not standard, you need to create a class as follows.

If there is a requirement at this time to add a register button in the alert box when the username input does not exist,

At this point, you can’t reuse the above class and create a new class.

When the login is successful, a custom popup window will pop up, prompting you to come back. This is a new class again.

However, if we write the above, when writing the registration business, we need to find the corresponding class every time we create, and the above class name starts with login and does not conform to the registration, such code is not standard, in this case, we can use the simple factory mode.

The simple factory pattern, in which a factory object decides to create an instance of a product object class, is used to create objects of the same class.

Like the following code:

We only need to know the factory function to create the object, not the creation of the underlying base class.

The above three classes have a lot in common, so we can extract them:

Team project development has great restrictions on global variables, so it is necessary to create global variables as little as possible. For the same class of objects in different requirements for repeated use, there is no need to create them repeatedly in many cases. Code reuse is a criterion of object-oriented programming.

Simple factories create objects that can share some resources and have some resources private, but the simple factory pattern is usually limited to creating a single object.

Let’s consider the following scenario: there is a group advertising computer training.

One batch is Java, with green font, and one batch is PHP, with yellow font, with red background. Let’s create these two classes as required:

Here is a javascript AD with a pink background:

For the above scenario we can use the simple factory pattern

If there’s a requirement, UI subject AD, for a red border, are we writing a class and then modifying the factory function? In fact, at this point we can use the factory method pattern.

The factory method pattern, which abstracts product classes so that the creation business is primarily responsible for creating instances of multiple internal products.

Let’s look at the following factory function:

The decision statement in the above code is to prevent the user from forgetting to use the new operator when using the class. As follows:

With this Factory class, when you want to add other classes in the future, you can just write it inside the Factory prototype.

The factory method pattern allows you to create instance objects of multiple classes, so that the factory method creates objects in a way that avoids coupling between the user and the object class. The user does not need to care about the specific class that created the object, but only needs to call the factory method.

Now let’s look at the most common type of factory pattern: the Abstract factory pattern.

The Abstract factory pattern enables businesses to create clusters of product classes by factory abstraction of classes, rather than creating instances of a particular class of products.

Let’s look at the following car abstract class:

The CAR class does nothing, doesn’t have any attributes, and can’t call any of the methods on the prototype, but it’s still useful for inheritance. But the subclass inherits from the parent class, without overriding the method will inherit, when the parent class method will report an error.

Methods defined in an abstract class only explicitly define some function, but there is no concrete implementation, so the object created by an abstract class is also abstract and cannot create a real object

So it is common to create subclasses of abstract classes as superclasses.

Let’s look at the following abstract factory method:

Let’s see how to create a class:

The above abstract factory is actually the implementation of the subclass inherit the parent class method, in this method, by passing the subclass and the name of the abstract class to inherit, and in the abstract factory method to add a judgment on the existence of the abstract class, if there is, the subclass inherit the parent class method, the subclass through parasitic inheritance.

Abstract factory is a method that does not need to instantiate the object, so we can add class attributes to the abstract factory.

Finally, let’s summarize the differences between the above three factory models:

The difference between the above three factory patterns is that the abstract factory pattern creates not a real object instance, but a class cluster that customizes the structure of the class, whereas the simple factory pattern creates a single object, whereas the factory method pattern creates multiple objects.

This article refers to javascript Design Patterns