Brief introduction:
From the type of design pattern, the simple factory pattern belongs to the creation pattern, also known as static Factory method (static Factory methods), but not one of the 23 gof design patterns. A simple factory pattern is a factory object that determines which instances of the product class are created.
Class Diagram:
From the UML class diagram, it can be seen that the simple factory model means that the inner part of a class, which needs to be generated, is extracted and transformed into a factory to new objects through the factory.
Let's say we're going to eat an apple, we can get a new apple out of the code, and when we need to eat a banana, we'll come out with a new banana in the code. This way you will not feel trouble, new things have to do their own. So you can give new things to a factory (as the name suggests, the factory is the place to produce things). And a simple factory decides what products to produce by passing in the parameters.
The words are not clear, please see the following code:
Code:
When we first didn't use the simple factory model, maybe we would do it and look at the following code
Class apple{public
Apple () {
System.out.println ("produced an apple");
}
Class banana{Public
banana () {
System.out.println ("Production of a Banana");
}
public class main{public
static void Main (string[] args) {
applea=new apple ();
Bananab=new Banana ();
}