Factory schema definition (Baidu Encyclopedia):
Factory pattern is most commonly used to instantiate the object model, a model is in place with a new plant operating method. The famous Jive forum on the extensive use of factory pattern, factory pattern can be said to be everywhere in the Java programming system. Because the factory mode is equivalent to creating a new instance of an object, we often generated according to Class class instance object, such as A a = new A () is used to create an instance of the factory model objects, since it is necessary when a plurality of new eye, consider whether you can use the factory pattern, although doing so may be a bit more work, but will give you greater system scalability and the amount of modifying as little as possible.
First, do not use the factory pattern to achieve:
clothes clothes interfaces, Jeans jeans overalls WorkClothes
package com.xxx.routine.intf; public interface Clothes { void doclothes (); // make clothes }
package com.xxx.routine.implintf; import com.xxx.routine.intf.Clothes; // Jeans public class Jeans the implements Clothes { @Override public void doclothes() { System.out.println ( "do jeans" ); } }
package com.xxx.routine.implintf; import com.xxx.routine.intf.Clothes; //工装裤 public class WorkClothes implements Clothes { @Override public void doclothes() { System.out.println ( "do overalls son!" ); } }
Test implementation:
package com.xxx.routine; import com.xxx.routine.implintf.Jeans; import com.xxx.routine.implintf.WorkClothes; import com.xxx.routine.intf.Clothes; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class RoutineApplicationTests { @Test void contextLoads() { C1 Clothes = new new Jeans (); // jeans Clothes C2 = new new WorkClothes (); // overalls c1.doclothes (); // do jeans c2.doclothes (); // do overalls } }
------------- ----------- output
Do jeans
do overalls son!
Second, the simple factory pattern:
[Source] Baidu Encyclopedia simple factory pattern to create a schema belongs, also known as static factory method (Static Factory Method) mode, but does not belong to one of 23 kinds of GOF design patterns. Simple factory pattern is determined by a factory object to create an instance of what kind of product class. Simple factory pattern is the factory model family is the most simple and practical model can be understood as a special mode of realization of different plants.
The essence of simple factory pattern is a class factory according to the incoming parameters, dynamic decision should create a product category (product of these classes inherit from a parent class or interface) instance.
package com.xxx.routine.factory; import com.xxx.routine.implintf.Jeans; import com.xxx.routine.implintf.WorkClothes; import com.xxx.routine.intf.Clothes; //做衣服工厂 public class ClothesFactory { public static Clothes createClothes(String type){ if("jeans".equals(type)){ return new Jeans(); }else if ("workclothes".equals(type)){ return new WorkClothes(); }else { return null; } } }
@Test // simple way to test plants void SimpleFactory () { Clothes jeans = ClothesFactory.createClothes("jeans"); Clothes workclothes = ClothesFactory.createClothes("workclothes"); jeans.doclothes(); workclothes.doclothes(); }
// output -------------------
do jeans
do overalls son!
[As soon as possible to add on, thank you understand ~! ]