Like the factory method mode, the abstract factory mode is also composed of four elements: abstract factory, concrete factory, abstract product and concrete product. However, the number of methods in the abstract factory is different, so is the number of abstract products.
The main roles of the abstract factory pattern are as follows:
1. Abstract factory
It provides an interface for creating products. It contains multiple methods for creating products. newProduct() can create multiple products of different levels.
2. Specific factory
It mainly implements multiple abstract methods in the abstract factory to complete the creation of specific products.
3. Abstract product
The product specification is defined, and the main characteristics and functions of the product are described. The abstract factory pattern has multiple Abstract products.
4. Specific products
It implements the interface defined by the abstract product role, which is created by the specific factory. It has a many-to-one relationship with the specific factory.
3, Advantages and disadvantages
1. Advantages
-
The multi-level products associated in the product family can be managed together within the class, instead of introducing multiple new classes for management;
-
When adding a new product family, there is no need to modify the original code to meet the opening and closing principle.
2. Shortcomings
When a new product needs to be added to the product family, all factory classes need to be modified.
4, After reading Head First design pattern
1. The abstract factory pattern provides an interface for creating families of related or dependent objects without explicitly specifying specific classes.
2. The factory method pattern uses inheritance, and the abstract factory pattern uses the combination of objects.
5, JDK source code analysis
package com.guor.jdk; import java.util.Calendar; public class Factory { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); // Note that the month subscript starts from 0, so the month should be + 1 System.out.println("year:" + cal.get(Calendar.YEAR)); System.out.println("month:" + (cal.get(Calendar.MONTH) + 1)); System.out.println("day:" + cal.get(Calendar.DAY_OF_MONTH)); System.out.println("Time:" + cal.get(Calendar.HOUR_OF_DAY)); System.out.println("branch:" + cal.get(Calendar.MINUTE)); System.out.println("second:" + cal.get(Calendar.SECOND)); } }
6, Code example
1. UML class diagram
2. Code architecture
3. Specific code
(1) order bag
package com.guor.abstractFactory.order; import com.guor.abstractFactory.pizza.Pizza; //Abstract layer (Interface) of an abstract factory pattern public interface AbsFactory { //Let the following factory subclass implement it public Pizza createPizza(String orderType); }
package com.guor.abstractFactory.order;
import com.guor.abstractFactory.pizza.BJCheesePizza;
import com.guor.abstractFactory.pizza.BJPepperPizza;
import com.guor.abstractFactory.pizza.Pizza;
//This is a factory subclass
public class BJFactory implements AbsFactory {
@Override public Pizza createPizza(String orderType) { System.out.println("~The abstract factory pattern is used~"); Pizza pizza = null; if(orderType.equals("cheese")) { pizza = new BJCheesePizza(); } else if (orderType.equals("pepper")){ pizza = new BJPepperPizza(); } return pizza; }
}
package com.guor.abstractFactory.order; import com.guor.abstractFactory.pizza.LDCheesePizza; import com.guor.abstractFactory.pizza.LDPepperPizza; import com.guor.abstractFactory.pizza.Pizza; public class LDFactory implements AbsFactory { @Override public Pizza createPizza(String orderType) { System.out.println("~The abstract factory pattern is used~"); Pizza pizza = null; if (orderType.equals("cheese")) { pizza = new LDCheesePizza(); } else if (orderType.equals("pepper")) { pizza = new LDPepperPizza(); } return pizza; } }
package com.guor.abstractFactory.order;
import com.guor.abstractFactory.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderPizza {
AbsFactory factory; // constructor public OrderPizza(AbsFactory factory) { setFactory(factory); } private void setFactory(AbsFactory factory) { Pizza pizza = null; String orderType = ""; // User input this.factory = factory; do { orderType = getType(); // Factory may be a factory subclass in Beijing or a factory subclass in London pizza = factory.createPizza(orderType); if (pizza != null) { // Order ok pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } else { System.out.println("Subscription failed");
summary
Although I often laugh at myself that I want to become a delivery specialist in ten years, in fact, relying on my own efforts can reduce the anxiety after the age of 35. After all, there are not many good architects.
Architect is the professional goal of most of our technicians. A good architect comes from the cooperation of opportunity (company), personal effort (hard work, willing to study) and talent (true love). Practice + opportunity + effort can help you become an excellent architect.
If you also want to be a good architect, maybe you need to read this Java growth note. I hope it can be helpful to your career development.
But actually, relying on their own efforts can reduce the anxiety after the age of 35. After all, there are not many good architects.
Architect is the professional goal of most of our technicians. A good architect comes from the cooperation of opportunity (company), personal effort (hard work, willing to study) and talent (true love). Practice + opportunity + effort can help you become an excellent architect.
If you also want to be a good architect, maybe you need to read this Java growth note. I hope it can be helpful to your career development.
[external chain picture transferring... (img-vXgCwOmH-1630928971679)]