Design Patterns_Factory Design Patterns

Definition of factory pattern:

Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses.

Define an interface for creating objects and let subclasses decide which class to instantiate. A factory method delays the instantiation of a class to its subclasses.

Factory pattern generic class diagram:

 

 

1. Extract an abstract product class

1  package cn.rocker.designpattern.factory;
 2  
3  /** 
4  * @author rocker
 5  * @version V1.0
 6  * @Description: abstract product class
 7  * @date 2018/5/6 17:09
 8   */ 
9  public  abstract  class Product {
 10      // public method of product class 
11      public  void method1(){
 12          // business processing logic 
13      }
 14  
15      // abstract method 
16      public  abstract  void method2();
17 }

2. According to different needs, realize different product categories

1  package cn.rocker.designpattern.factory;
 2  
3  /** 
4  * @author rocker
 5  * @version V1.0
 6  * @Description: Product implementation class 1
 7  * @date 2018/5/6 17:11
 8   * / 
9  public  class ConcreteProduct1 extends Product{
 10      @Override
 11      public  void method2() {
 12          // Business logic processing 
13          System.out.println("ConcreteProduct1 is made..." );
 14      }
 15}
1  package cn.rocker.designpattern.factory;
 2  
3  /** 
4  * @author rocker
 5  * @version V1.0
 6  * @Description: Product implementation class 2
 7  * @date 2018/5/6 17:12
 8   * / 
9  public  class ConcreteProduct2 extends Product{
 10      @Override
 11      public  void method2() {
 12          // Business processing logic 
13          System.out.println("ConcreteProduct2 is made..." );
 14      }
 15}

 

3. Extract the abstract class of a factory

1  package cn.rocker.designpattern.factory;
 2  
3  /** 
4  * @author rocker
 5  * @version V1.0
 6  * @Description: Create a product object, in fact, the parameter type can be set by yourself
 7  * Usually String, Enum, Class, etc., of course can also be empty
 8  * @date 2018/5/6 17:13
 9   */ 
10  public  abstract  class Creator {
 11      public  abstract <T extends Product> T createProduct(Class<T> c);
 12 }

 

4. According to the abstract class, in the implementation class, implement the specific production method

1  package cn.rocker.designpattern.factory;
 2  
3  /** 
4  * @author rocker
 5  * @version V1.0
 6  * @Description: How to produce a product object is implemented by a specific factory class
 7  * @date 2018/5/6 17:33
 8   */ 
9  public  class ConcreteCreator extends Creator{
 10      @Override
 11      public <T extends Product> T createProduct(Class<T> c) {
 12          Product product = null ;
 13          try {
14             product = (Product) Class.forName(c.getName()).newInstance();
15         } catch (InstantiationException e) {
16             e.printStackTrace();
17         } catch (IllegalAccessException e) {
18             e.printStackTrace();
19         } catch (ClassNotFoundException e) {
20             e.printStackTrace();
21         }
22         return (T)product;
23     }
24 }

5. Obtain specific product categories by operating the factory class on the client side

1  package cn.rocker.designpattern.factory;
 2  
3  
4  import org.junit.Test;
 5  
6  /** 
7  * @author rocker
 8  * @version V1.0
 9  * @Description: use factory to produce product
 10  * @date 2018/5/6 17:36
 11   */ 
12  public  class Client {
 13      @Test
 14      public  void test(){
 15          // Create factory 
16          Creator creator = new ConcreteCreator();
 17 
18         //生产ConcreteProduct1
19         ConcreteProduct1 product1 = creator.createProduct(ConcreteProduct1.class);
20         product1.method2();
21 
22         //生产ConcreteProduct2
23         ConcreteProduct2 product2 = creator.createProduct(ConcreteProduct2.class);
24         product2.method2();
25     }
26 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325356083&siteId=291194637