Concept of simple factory pattern
It is to establish a factory class and create instances of some classes that implement the same interface. The essence of a simple factory pattern is that a factory class dynamically determines which instance of product class (these product classes inherit from a parent class or interface) should be created based on the incoming parameters.
UML diagram of simple factory mode
Simple factory pattern code
When I was learning the simple factory model, I used an example related to humans. Humans are divided into men and women in the world, and first define an abstract interface for Human products.
/** * This is factory pattern package */package com.roc.factory;/** * Abstract interface of product human* @author liaowp * */public interface Human { public void say();}Then, both men and women have ways to speak.
/** * This is factory pattern package */package com.roc.factory;/** * man man* @author liaowp * */public class Man implements Human { /* say method * @see com.roc.factory.Human#say() */ @Override public void says() { System.out.println("Man"); }} /** * This is factory pattern package */package com.roc.factory;/**Woman* @author liaowp * */public class Woman implements Human { /* say method * @see com.roc.factory.Human#say() */ @Override public void says() { System.out.println("Woman"); }}Finally, write a factory class to create men and women. The first method is achieved using logical judgment.
package com.roc.factory;/** * Simple factory* @author liaowp * */public class SampleFactory { public static Human makeHuman(String type){ if(type.equals("man")){ Human man = new Man(); return man; }else if(type.equals("womman")){ Human woman = new Woman(); return woman; }else{ System.out.println("can't produce"); return null; } }}The second method is to use java radiation to implement it. I think this type of implementation is better.
package com.roc.factory;/** * Simple factory radiation implementation* @author liaowp * */public class SampleFactory1 { public static Human makeHuman(Class c){ Human human = null; try { human = (Human) Class.forName(c.getName()).newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block System.out.println("Abstract classes or interfaces are not supported"); e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("No sufficient permissions, that is, private objects cannot be accessed"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block System.out.println("ClassNot exists"); e.printStackTrace(); } return human; }}Finally, the client's code
package com.roc.factory;/** * Simple factory test* @author liaowp * */public class Client { public static void main(String[] args) {// Human man = SampleFactory.makeHuman("man");// man.say();// Human womman = SampleFactory.makeHuman("womman");// womman.say();// Human test = SampleFactory.makeHuman("tttt"); Human man = SampleFactory1.makeHuman(Man.class); man.say(); Human woman = SampleFactory1.makeHuman(Woman.class); woman.say(); }} Simple factory mode application scenario
Advantages: The factory class is the key to the entire model. It contains necessary logical judgments and determines which specific class object should be created based on the information given by the outside world. By using the factory class, the outside world can get rid of the embarrassing situation of directly creating specific product objects, and only need to be responsible for "consumption" objects. It doesn't matter how these objects are created and organized. Their respective responsibilities and rights are clarified, which is conducive to the optimization of the entire software architecture.
Disadvantages: Since the factory class concentrates the creation logic of all instances, violates the principle of high cohesive responsibility allocation, all creation logic is concentrated in one factory class; the classes it can create can only be considered in advance. If a new class needs to be added, the factory class needs to be changed. As the number of specific product categories in the system continues to increase, there may be a requirement for factory classes to create different instances according to different conditions. This judgment of conditions and judgment of specific product types are interlaced together, making it difficult to avoid the spread of module functions, which is very unfavorable to system maintenance and expansion;
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.