1. Design Pattern-factory method pattern, design pattern-factory pattern
The factory method mode is applicable to the scenario where "a large number of products need to be created frequently and have the same interface ".
1. In the common factory mode, a unified instance is created for some classes that implement the same interface, and the unified creation work is handed over to a class for implementation. This class is called a factory class.
Job interface (defines what to do)
1 public interface Job {2 public void say (); 3}View Code
ReadJob implementation class, which implements the Job interface and defines the Job interface:
1 public class ReadJob implements Job {2 @ Override3 public void say () {4 System. out. println ("This is read Job"); 5} 6}View Code
WriteJob implementation class, which implements the Job interface and defines the Job interface:
1 public class WriteJob implements Job {2 @ Override3 public void say () {4 System. out. println ("This is write Job"); 5} 6}View Code
The common factory obtains the returned instance by passing parameters in a common method, as shown in figure
1 public class SimpleJobFactory {2 public static final String READ = "read"; 3 public static final String WRITE = "write"; 4 public Job produce (String type) 5 {6 if (READ. repeated signorecase (type) 7 {8 return new ReadJob (); 9} else if (WRITE. repeated signorecase (type) 10 {11 return new WriteJob (); 12} else13 {14 System. out. println ("Parameter type error, can not create object"); 15 return null; 16} 17} 18}View Code
Test Case: generate two object instances with unified interfaces based on parameters
1 public class SimpleJobFactoryTest {2 public static void main(String[] args) {3 SimpleJobFactory factory=new SimpleJobFactory();4 Job job1=factory.produce(SimpleJobFactory.READ);5 job1.say();6 Job job2=factory.produce(SimpleJobFactory.WRITE);7 job2.say();8 }9 }
Test output:
1 This is read Job2 This is write Job
Ii. Multiple factory method ModesIs an improvement on the common factory method mode. In the common factory method mode, if the passed string fails, the object cannot be created correctly, multiple factory method modes provide multiple factory methods to create objects respectively.
1 public class MethodsJobFactory {2 public Job produceReadJob(){3 return new ReadJob();4 }5 public Job produceWriteJob(){6 return new WriteJob();7 }8 }
Test code:
1 public class MethodsJobFactoryTest {2 public static void main(String[] args) {3 MethodsJobFactory factory=new MethodsJobFactory();4 Job job1=factory.produceReadJob();5 Job job2=factory.produceWriteJob();6 job1.say();7 job2.say();8 }9 }
Test results:
1 1 This is read Job2 2 This is write Job
III,Static factory method modeSet the methods in the preceding factory method mode to static. You can directly call the methods without creating an instance.
public class MethodsJobStaticFactory { public static Job produceReadJob(){ return new ReadJob(); } public static Job produceWriteJob(){ return new WriteJob(); }}
Test code:
1 public class MethodsJobStaticFactoryTest {2 public static void main(String[] args) {3 Job job1=MethodsJobStaticFactory.produceReadJob();4 Job job2=MethodsJobStaticFactory.produceWriteJob();5 job1.say();6 job2.say();7 }8 }
Test results:
1 This is read Job2 This is write Job
Generally, the use of static factories is better, and the common factory mode and multiple factory method modes are not easy to use.
Next, we will learn the abstract factory model.
22:02:40