Home >Java >javaTutorial >Quick and easy understanding of Java's three factory modes
Quick and easy to understand java’s three factory patterns
1. Simple Factory Pattern (Simple Factory)
is a pattern that is specifically responsible for instantiating a large number of classes with common interfaces, and there is no need to know in advance which class to instantiate each time . It defines an interface for creating objects, and subclasses decide which class to instantiate.
//Define the fruit interface public interface Fruit { void plantFruit(); void eatFruit(); } //Apple implements the fruit interface public class Apple implements Fruit { @Override public void plantFruit() { System.out.println(" Plant apples. "); } @Override public void eatFruit() { System.out.println("Eat apples."); } } //Orange implements the fruit interface public class Orange implements Fruit { @Override public void plantFruit() { System.out.println("Grow oranges."); } @Override public void eatFruit() { System.out.println("Eat oranges."); } } //Fruit factory public class FruitFactory { public static Fruit getFurit( String fruitName) { //Simple factory mode if (fruitName.equalsIgnoreCase("Apple")) { // If it is Apple, return the Apple instance return new Apple(); } else if (fruitName.equalsIgnoreCase("Orange")) { // If it is an orange, return an orange instance return new Orange(); } else { return null; } } } //Test class public class Test { public static void main(String[]args){ //Call a simple factory Pattern FruitFactory.getFurit("Orange").plantFruit(); } } Output: Plant oranges.
Summary
A: If I want to buy apples, I just need to make a request to the factory role (FruitFactory). After receiving the request, the factory role will decide which product to create and provide on its own.
B: But for the factory role (FruitFactory), adding new products (such as adding strawberries) is a painful process. The factory role must know each product, how to create them, and when to provide them to clients. In other words, accepting new products means modifying the factory.
C: Therefore, the openness of this simple factory model is relatively poor.
================================================== ================
2. Factory Method Pattern
Leave the creation of objects to a standard method defined in the parent class instead of its constructor , it is up to the specific subclass to decide what kind of object should be created.
//Define the fruit interface public interface Fruit { void plantFruit(); void eatFruit(); } //Apple implements the fruit interface public class Apple implements Fruit { @Override public void plantFruit() { System.out.println(" Plant apples. "); } @Override public void eatFruit() { System.out.println("Eat apples."); } } //Orange implements the fruit interface public class Orange implements Fruit { @Override public void plantFruit() { System.out.println("Grow oranges."); } @Override public void eatFruit() { System.out.println("Eat oranges."); } } //Fruit factory ([b] Note: This factory declares It is an interface with good scalability[/b]) public interface FactoryMethod { /*** Factory method*/ Fruit getFruit(); //Define the process of getting fruit} //Apple’s implementation of the fruit factory public class getApple implements FactoryMethod{ @Override public Fruit getFruit() { // TODO Auto-generated method stub return new Apple(); } } //Orange’s implementation of the fruit factory public class getOrange implements FactoryMethod { @Override public Fruit getFruit() { // TODO Auto-generated method stub return new Orange(); } } //Test class public class TestFactoryMethod { public static void main(String[] args) { getApple apple = new getApple(); apple.getFruit(). eatFruit(); } } Output: Eat the apple.
Summary:
A: The structural difference between the factory method pattern and the simple factory pattern is obvious. The core of the factory method pattern is an abstract factory class, while the simple factory pattern places the core on a concrete class. The factory method pattern allows many concrete factory classes to inherit the creation behavior from the abstract factory class, thus becoming a synthesis of multiple simple factory patterns, thus popularizing the simple factory pattern.
B: The factory method pattern can become very similar to the simple factory pattern after degeneration. Imagine that if you are very sure that a system only needs one concrete factory class, then you might as well merge the abstract factory class into the concrete factory class. Since there is only one specific factory class anyway, you might as well change the factory method to a static method. At this time, you will get the simple factory pattern.
C: If you need to add a new fruit, you only need to add a new fruit class and its corresponding factory class. There is no need to modify the client, nor to modify the abstract factory role or other existing concrete factory roles. For adding new fruit categories, this system fully supports the "open-closed" principle.
D: For the Factory Method mode, it only targets one category (such as Fruit in this example), but if we also want to buy meat, that won’t work. This is where the Abstract Factory mode must help. .
================================================== ==============
3. Abstract Factory Pattern (Abstract Factory)
The Abstract Factory Pattern can be said to be an extension of the Simple Factory Pattern. Their main difference lies in the complexity of creating objects. .
In the abstract factory pattern, there may be one or more abstract products, thus forming one or more product families. In the case of only one product family, the abstract factory pattern actually degenerates into the factory method pattern.
Summary:
A: The abstract factory pattern can provide an interface to the client, allowing the client to create product objects in multiple product families without having to specify the specific type of product. This is the purpose of the abstract factory pattern.
B: The abstract factory pattern is the most abstract and general form of all forms of factory patterns.
C: The biggest difference between the abstract factory pattern and the factory method pattern is that the factory method pattern targets a product (Fruit) hierarchical structure; while the abstract factory pattern needs to face multiple product hierarchical structures (Fruit, Meat).