definition:
Product hierarchy structure: product hierarchy structure is the inheritance structure of products. For example, an abstract class is TV, and its subclasses include Haier TV, Hisense TV and TCL TV. Then the abstract TV forms a product hierarchy between TVs without specific brands. The image TV is the parent class, while the TV of specific brands is its subclass.
Product family: in the abstract factory model, product family refers to a group of products produced by the same factory and located in different product hierarchy, such as Haier TV and Haier refrigerator produced by Haier Electric Appliance Factory. Haier TV is located in the TV product hierarchy and Haier refrigerator is located in the refrigerator product hierarchy.
Abstract factory pattern: provides an interface to create a series of related or interdependent objects without specifying their specific classes. Abstract factory pattern, also known as Kit pattern, belongs to object creation pattern.
advantage:
The generation of specific classes is isolated, so that the client does not need to know what is created
When multiple objects in a product family are designed to work together, it can ensure that clients always use only objects in the same product family
It is convenient to add a new product family without modifying the existing system, which conforms to the opening and closing principle
Disadvantages:
It is troublesome to add a new product hierarchy, which requires major modifications to the original system, or even to modify the abstract layer code, which will obviously bring great inconvenience and violate the opening and closing principle
Applicable environment:
A system should not rely on the details of how product class instances are created, combined and expressed
There are more than one product family in the system, but only one of them is used at a time
Products belonging to the same product family will be used together, and this constraint must be reflected in the design of the system
The product grade structure is stable. After the design is completed, no new product grade structure will be added to the system or the existing product grade structure will be deleted
UML diagram:
Instance Directory:
Specific code:
TV.java
package abstractFactory; public interface TV { public void play(); }
HaierTV.java
package abstractFactory; public class HaierTV implements TV{ public void play() { System.out.println("Haier TV broadcasting"); } }
HisenseTV.java
package abstractFactory; public class HisenseTV implements TV { @Override public void play() { // TODO Auto-generated method stub System.out.println("Hisense TV broadcasting"); } }
AirConditioner.java
package abstractFactory; public interface AirConditioner { public void changeTemperture(); }
HaierAirConditioner.java
package abstractFactory; public class HaierAirConditioner implements AirConditioner { @Override public void changeTemperture() { // TODO Auto-generated method stub System.out.println("Haier air conditioning refrigeration..."); } }
HisenseAirConditioner.java
package abstractFactory; public class HisenseAirConditioner implements AirConditioner { @Override public void changeTemperture() { // TODO Auto-generated method stub System.out.println("Hisense air conditioning refrigeration..."); } }
Factory.java
package abstractFactory; public interface Factory { public TV produceTV(); public AirConditioner produceAirConditioner(); }
HaierFactory.java
package abstractFactory; public class HaierFactory implements Factory { @Override public TV produceTV() { // TODO Auto-generated method stub return new HaierTV(); } @Override public AirConditioner produceAirConditioner() { // TODO Auto-generated method stub return new HaierAirConditioner(); } }
HisenseFactory.java
package abstractFactory; public class HisenseFactory implements Factory { @Override public TV produceTV() { // TODO Auto-generated method stub return new HisenseTV(); } @Override public AirConditioner produceAirConditioner() { // TODO Auto-generated method stub return new HisenseAirConditioner(); } }
XMLUtil.java
package abstractFactory; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XMLUtil { public static Object getBean() { try { //Create document object DocumentBuilderFactory dFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder =dFactory.newDocumentBuilder(); Document doc; doc=builder.parse(new File("abstractFactoryConfig.xml")); //Get text node NodeList nl=doc.getElementsByTagName("className"); Node classNode=nl.item(0).getFirstChild(); String cName=classNode.getNodeValue(); Class c=Class.forName(cName); Object obj=c.newInstance(); return obj; } catch(Exception e) { e.printStackTrace(); return null; } } }
abstractFactoryConfig.java
<?xml version="1.0" encoding="UTF-8"?> <config> <className>abstractFactory.HaierFactory</className> </config>
Operation results: