This article describes the factory model of Java design pattern. Share it for your reference, as follows:
1. Simple factory
Let’s think about a question first. When we write programs, there will be such a situation. Object A needs to call the method of object B. At this time, we usually use the new keyword to create an instance B and then call the method of instance B. The disadvantage of this approach is that the method of Class A directly calls the class name of Class B (this method is also called hard-coded coupling). Once the system needs to be refactored: when class C needs to be used instead of class B, the program has to modify the Class A code. If 100 or 10,000 classes in the application are hard-coded, 100 or 10,000 places need to be modified, which is obviously a very terrible thing.
Looking at this problem from another perspective: for an object already A, it only needs to call the method of object B, and does not care about the implementation and creation process of object B, consider letting class B implement an IB interface, while class A only needs to be coupled with the IB interface - Class A does not directly use the new keyword to create B instances, but redefines a factory class: IBFactory, which is responsible for creating IB instances, and Class A uses the method of calling the IBFactory factory to obtain IB instances. Through the above design: if you need to use Class C instead of Class B, you only need to let Class C implement the IB interface and rewrite the implementation code for creating an IB instance in the IBFactory factory, so that the factory can generate a C instance. This design method of handing over multiple class objects to factory classes for generation is called the simple factory pattern.
Here is the code for simple factory pattern:
/** * Simple factory mode * Common interface that requires factory-produced object instances* Hair style interface* @author Administrator * */public interface Hair { /** * Draw hairstyle*/ public void draw();}/** * Left side-distribution hairstyle* @author Administrator * */public class LeftHair implements Hair { @Override public void draw() { System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- * */public class RightHair implements Hair { @Override public void draw() { System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Administrator * */public class HairTest { public static void main(String[] args) { HairFactory factory = new HairFactory(); Hair hair = factory.getHair(); hair.draw(); }}As you can see, if you want to change the LeftHair generated in HairTest to RightHair, you just need to modify the implementation of the getHair method in HairFactory.
The advantage of using the simple factory mode is that it separates the object caller and the object creation process. When the object caller needs an object, it can directly request it from the factory, thereby avoiding the object caller and the object implementation class being coupled in a hard-coded manner to improve the system's maintainability and scalability. Of course, the factory model also has a small flaw. When the product is modified, the factory class also needs to be modified accordingly. Here you can use the policy model to solve it. The following is the code.
public interface HairBuilder { /** * Make hairstyle* @return */ public Hair getHair();}public class LeftHairBuilder implements HairBuilder { @Override public Hair getHair() { return new LeftHair(); }}public class RightHairBuilder implements HairBuilder { @Override public Hair getHair() { return new RightHair(); }}public class HairFactory { private HairBuilder hairBuilder; public HairFactory(HairBuilder hairBuilder) { this.hairBuilder = hairBuilder; } public void setHairBuilder(HairBuilder hairBuilder) { this.hairBuilder = hairBuilder; } public Hair getHair() { return hairBuilder.getHair(); }}public class HairTest { public static void main(String[] args) {// HairBuilder builder = new LeftHairBuilder(); HairBuilder builder = new RightHairBuilder(); HairFactory factory = new HairFactory(builder); Hair hair = factory.getHair(); hair.draw(); }}The advantage of this approach is that there is no need to modify the factory class anymore, and abstract the creation logic in the factory according to different strategies. What objects do the program need to create? Just pass the corresponding builder into the network factory.
2. Factory method
In the simple factory model, the system uses the factory class to produce all product instances, and the factory class decides which class of instances to produce, that is, the factory class is responsible for all logical judgments, instance creation and other work.
If you do not want to make logical judgments in the factory class, the program can provide different factories for different product categories, and different factory classes produce different products, without making complex logical judgments in the factory class. This is a bit similar to the simple factory model combined with strategy model above. The difference is that the former only has one factory, while the latter requires multiple factories. Below is the code for the factory method pattern.
/** * Factory method pattern* Common interface required to be implemented by object instances produced by the factory* @author Administrator * */public interface Person { public void drawPerson();}public class Man implements Person { @Override public void drawPerson() { System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @author Administrator * */public class WomenFactory implements PersonFactory { @Override public Person getPerson() { return new Women(); }}/** * Client test class* @author Administrator * */public class PersonTest { public static void main(String[] args) {// PersonFactory factory = new ManFactory(); PersonFactory factory = new WomenFactory(); Person person = factory.getPerson(); person.drawPerson(); }}The typical feature of this kind is that it produces its corresponding products according to different factories in the client code, and there is no need to put all the complex logic into the factory class to judge. There is an obvious flaw in this implementation, which is that the client is coupled with the factory class.
3. Abstract factory
Using the above design architecture of the factory method, the client code is successfully separated from the implementation class of the called object, but it brings another coupling: the client code is coupled with different factory classes. To solve this coupling problem, consider adding a factory class to generate factory instances and separating the factory from the client for producing products. This design method is called the abstract factory model. Below is the code for abstract factory pattern
/** * Abstract factory pattern* Factory producing PersonFactory* @author Administrator * */public class PersonFactoryFactory { public static PersonFactory getPersonFactory(String type) { if(type.equalsIgnoreCase("man")) { return new ManFactory(); } else { return new WomenFactory(); } }}/** * Client test class* @author Administrator * */public class PersonTest { public static void main(String[] args) { PersonFactory factory = PersonFactoryFactory.getPersonFactory("man"); Person person = factory.getPerson(); person.drawPerson(); }}For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.