“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
First, what is the factory model
Factory mode is a specific factory to create specific products, users only need to know the name of the factory, can obtain specific products. There is an abstract product factory that produces abstract cats. Then, each specific product is produced by a specific factory. For example, a red cat needs to be produced by the specific factory that produces the red cat. We only need to know the name of the factory that produces the red cat to get the red cat.
Second, the realization of factory model
public interface CatFactory {
CatProduct getCat();
}
Copy the code
An abstract product factory that produces cats
public interface CatProduct {
void sayMyName();
}
Copy the code
Abstract product cat
public class RedCatProduct implements CatProduct{
@Override
public void sayMyName() {
System.out.println("I am a red cat!");
}
}
Copy the code
Specific products red cat
public class RedCatFactory implements CatFactory{ @Override public CatProduct getCat() { return new RedCatProduct(); }}Copy the code
Specific factory red cat factory
public class BlankCatProduct implements CatProduct{ @Override public void sayMyName() { System.out.println("I am a blank cat!" ); }}Copy the code
Specific products black cat
public class BlankCatFactory implements CatFactory{
@Override
public CatProduct getCat() {
return new BlankCatProduct();
}
}
Copy the code
Specific factory black cat factory
public static void main(String[] args) {
CatFactory redCatFactory = new RedCatFactory();
CatProduct catA = redCatFactory.getCat();
CatFactory blankFactory = new BlankCatFactory();
CatProduct catB = blankFactory.getCat();
catA.sayMyName();
catB.sayMyName();
}
Copy the code
Factory model to produce products, there need to be abstract factories and abstract products, and then concrete products and factories concrete implementation, different products different factories. The final use directly through different factories to obtain different products.
Third, summary
Advantages: You can hide the specific process of product creation, and only need to know the name of the factory corresponding to the product, you can obtain the corresponding product. In addition, new products only need to add factory and product categories (for example, if there are more yellow cat products, a new yellow cat factory is needed).
Disadvantages: With the increase of products, the number of classes will continue to increase, increasing the complexity of the system. And a factory can only create one product.
To sum up, the factory pattern is suitable for users who only need to obtain the product, without knowing the specific implementation. In addition, the factory mode conforms to the open – close principle.
The simple factory pattern differs from the factory pattern and the abstract factory pattern:
-
The simple factory model means that one factory can produce multiple products of the same kind, such as the simple factory model dog factory can produce dogs of various sizes. If new products are added, we need to modify the code of the simple factory, which does not conform to the open and close principle.
-
Factory model Each factory produces specific products, in accordance with the open and closed principle. The difference with a simple factory is that there is no need to modify the factory code as in a simple factory.
-
Abstract factory is an upgraded version of factory mode, which solves the problem that one factory cannot create multiple products, and also conforms to the open and closed principle.