【设计模式】FactoryPattern工厂模式

Factory Pattern

简单工厂模式

将变化的部分封装起来

 1 //简单工厂
 2 class SimpleProductFactory{
 3     Product createProduct(String type){
 4         //选择--varied
 5         if (type.equals("type1")){
 6             product = new ...
 7         }else if(...){
 8             ...
 9         }else{
10             ...
11         }
12         return product;
13     }
14 }
15 
16 
17 Product orderProduct(String type){
18 
19     SimpleProductFactory factory = new SimpleProductFactory();
20 
21     Product product = factory.createProduct(type);
22 
23     //other operations...
24 
25 }
26 
27 //进一步封装
28 public class ProductStore{
29     SimpleProductFactory factory;
30     public ProductStore(SimpleProductFactory factory){
31         this.factory = factory;
32     }
33 
34     public Product orderProduct(String type){
35         Product product = factory.createProduct(type);
36         //other operations...
37 
38     }
39 }
40 
41 /*
42 这一步:
43 使用了一个Store类,包含了生产产品的方法。
44 factory在构造函数的时候传进来。
45 推迟了new操作。
46 */

工厂方法模式

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

 1 //工厂方法模式
 2 public abstract class ProductStore{
 3     public Product orderProduct(String type){
 4         Product product = createProduct(type);
 5         //product.test();
 6         //other methods...
 7         return product;
 8     }
 9 
10     protected abstract Product createProduct; //->工厂方法
11     //other methods...
12 }

抽象工厂模式

Abstract Factory creates a family of concrete objects together needed by the client.

 1 //抽象工厂模式
 2 
 3 //原料工厂
 4 public interface ProductIngredientFactory{
 5     
 6     public Ingredient1 createIngredient1();
 7     public Ingredient2 createIngredient2();
 8     public Ingredient3 createIngredient3();
 9     //...
10 }
11 //具体的原料工厂类
12 //实现每一种对应的ingredient
13 
14 
15 public class Product1 extends Product{
16     ProductIngredientFactory ingredientFactory;
17     public Product1(ProductIngredientFactory ingredientFactory){
18         this.ingredientFactory = ingredientFactory;
19     }
20     void prepare(){
21         ingredient1 = ingredientFactory.createIngredient1();
22         ingredient2 = ingredientFactory.createIngredient2();
23         ingredient3 = ingredientFactory.createIngredient3();
24     }
25     
26 }
27 
28 //重构产品商店
29 //每个具体的商店(继承自抽象的ProductStore)都要实现里面的工厂方法
30 //可能不止createProduct这一种工厂方法
31 //在这个工厂方法里,实现具体工厂的实例(包含了多个abstract方法),将具体工厂传入产品中
32 
33 public class ProductStore1 extends ProductStore{
34 
35     //implement an abstract method
36     protected Product createProduct(String type){
37         Product product = null;
38         ProductIngredientFactory ingredientFactory = new ProductIngredientFactory1(); //指定是原料工厂1,具体的类
39 
40         if (type.equals("type1")){
41             product = new Product1(ingredientFactory);
42         }else if(...){
43             ...
44         }else{
45             ...
46         }
47         return product;
48     }
49 }
原文地址:https://www.cnblogs.com/christy99cc/p/11745112.html