其他分享
首页 > 其他分享> > 设计模式@第6章:工厂模式

设计模式@第6章:工厂模式

作者:互联网

第6章:工厂模式

一、简单工厂模式

(一)看一个具体的需求

看一个披萨的项目:要便于披萨种类的扩展,要便于维护

(二)使用传统的方式来完成

传统思路

2) 看老师代码的演示

public abstract class Pizza{
    protect String name;
    // 因为不同的 Pizza 准备方法不同,因此做成抽象方法
    public abstract void perpard();
    public void bake(){
        // XXXXX
    }
    public void cut(){
        // XXXXX
    }
    public void box(){
        // XXXXX
    }
}
//-------------------------------------------------------
// 然后具体的 Pizza 实体继承 Pizza
public void CheessPizza extends Pizza(){
    //实现 prepare 方法
}
//-------------------------------------------------------
public void GreekPizza extends Pizza(){
    //实现 prepare 方法
}

//-------------------------------------------------------
// 然后实现最终的 OrderPizza 类
public class OrderPizza {
    // 构造器
    public OrderPizza() {
        Pizza pizza = null;
        String orderType; //  订购披萨的类型
        do {
            orderType = getType();
            if (orderType.equals("greek")) {
                pizza = new GreekPizza();
                pizza.setName(" 希腊披萨 ");
            } else if (orderType.equals("cheese")) {
                pizza = new CheesePizza();
                pizza.setName(" 奶酪披萨 ");
            } else if (orderType.equals("pepper")) {
                pizza = new PepperPizza();
                pizza.setName("胡椒披萨");
            } else {
                break;
            }
            //输出 pizza 制作过程
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();

        } while (true);
    }

(三)传统的方式的优缺点

(四)基本介绍

6.1.5 使用简单工厂模式

1) 简单工厂模式的设计方案: 定义一个可以实例化 Pizaa 对象的类,封装创建对象的代码。

简单工厂模式

  package com.gjxaiou.factory.simplefactory.pizzastore.pizza;
  
  //将Pizza 类做成抽象
  public abstract class Pizza {
    protected String name; //名字
  
    //准备原材料, 不同的披萨不一样,因此,我们做成抽象方法
    public abstract void prepare();
  
    
    public void bake() {
        System.out.println(name + " baking;");
    }
  
    public void cut() {
        System.out.println(name + " cutting;");
    }
  
    //打包
    public void box() {
        System.out.println(name + " boxing;");
    }
  
    public void setName(String name) {
        this.name = name;
    }
  }
  
  //----------------------------------------------------
  package com.gjxaiou.factory.simplefactory.pizzastore.pizza;
  
  public class CheesePizza extends Pizza {
    @Override
    public void prepare() {
        System.out.println(" 给制作奶酪披萨 准备原材料 ");
    }
  }
  //----------------------------------------------------
  package com.gjxaiou.factory.simplefactory.pizzastore.pizza;
  public class GreekPizza extends Pizza {
    @Override
    public void prepare() {
        System.out.println(" 给希腊披萨 准备原材料 ");
    }
  }
  //----------------------------------------------------
  package com.gjxaiou.factory.simplefactory.pizzastore.pizza;
  
  public class CheesePizza extends Pizza {
    @Override
    public void prepare() {
        System.out.println(" 给制作奶酪披萨 准备原材料 ");
    }
  }
  
  

简单的工厂类:因为工厂类中可以使用一般方法,也可以使用静态方法,代码中两种都实现了:

package com.gjxaiou.factory.simplefactory.pizzastore.order;

import com.gjxaiou.factory.simplefactory.pizzastore.pizza.CheesePizza;
import com.gjxaiou.factory.simplefactory.pizzastore.pizza.GreekPizza;
import com.gjxaiou.factory.simplefactory.pizzastore.pizza.PepperPizza;
import com.gjxaiou.factory.simplefactory.pizzastore.pizza.Pizza;

//简单工厂类
public class SimpleFactory {

  //更加orderType 返回对应的Pizza 对象
  public Pizza createPizza(String orderType) {

      Pizza pizza = null;

      System.out.println("使用简单工厂模式");
      if (orderType.equals("greek")) {
          pizza = new GreekPizza();
          pizza.setName(" 希腊披萨 ");
      } else if (orderType.equals("cheese")) {
          pizza = new CheesePizza();
          pizza.setName(" 奶酪披萨 ");
      } else if (orderType.equals("pepper")) {
          pizza = new PepperPizza();
          pizza.setName("胡椒披萨");
      }
      return pizza;
  }
  
  //简单工厂模式 也叫 静态工厂模式 
  
  public static Pizza createPizza2(String orderType) {
      Pizza pizza = null;

      System.out.println("使用简单工厂模式2");
      if (orderType.equals("greek")) {
          pizza = new GreekPizza();
          pizza.setName(" 希腊披萨 ");
      } else if (orderType.equals("cheese")) {
          pizza = new CheesePizza();
          pizza.setName(" 奶酪披萨 ");
      } else if (orderType.equals("pepper")) {
          pizza = new PepperPizza();
          pizza.setName("胡椒披萨");
      }
      return pizza;
  }
}

一般工厂方法对应的 Order 程序:

package com.gjxaiou.factory.simplefactory.pizzastore.order;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import com.gjxaiou.factory.simplefactory.pizzastore.pizza.Pizza;

public class OrderPizza {
  //定义一个简单工厂对象
  SimpleFactory simpleFactory;
  Pizza pizza = null;
  
  //构造器
  public OrderPizza(SimpleFactory simpleFactory) {
      setFactory(simpleFactory);
  }
  
  public void setFactory(SimpleFactory simpleFactory) {
      String orderType = ""; //用户输入的
      
      this.simpleFactory = simpleFactory; //设置简单工厂对象
      
      do {
          orderType = getType(); 
          pizza = this.simpleFactory.createPizza(orderType);
          
          //输出pizza
          if(pizza != null) { //订购成功
              pizza.prepare();
              pizza.bake();
              pizza.cut();
              pizza.box();
          } else {
              System.out.println(" 订购披萨失败 ");
              break;
          }
      }while(true);
  }
  
  // 写一个方法,可以获取客户希望订购的披萨种类
  private String getType() {
      try {
          BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
          System.out.println("input pizza 种类:");
          String str = strin.readLine();
          return str;
      } catch (IOException e) {
          e.printStackTrace();
          return "";
      }
  }
}

静态工厂对应的 ORDER 方法:

package com.gjxaiou.factory.simplefactory.pizzastore.order;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.gjxaiou.factory.simplefactory.pizzastore.pizza.Pizza;

public class OrderPizza2 {

  Pizza pizza = null;
  String orderType = "";
  // 构造器
  public OrderPizza2() {
      
      do {
          orderType = getType();
          pizza = SimpleFactory.createPizza2(orderType);

          // 输出pizza
          if (pizza != null) { // 订购成功
              pizza.prepare();
              pizza.bake();
              pizza.cut();
              pizza.box();
          } else {
              System.out.println(" 订购披萨失败 ");
              break;
          }
      } while (true);
  }

  // 写一个方法,可以获取客户希望订购的披萨种类
  private String getType() {
      try {
          BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
          System.out.println("input pizza 种类:");
          String str = strin.readLine();
          return str;
      } catch (IOException e) {
          e.printStackTrace();
          return "";
      }
  }
}

执行测试上面方法:

package com.gjxaiou.factory.simplefactory.pizzastore.order;

//相当于一个客户端,发出订购
public class PizzaStore {
    public static void main(String[] args) {
        // 使用简单工厂模式
        new OrderPizza(new SimpleFactory());
        // 使用静态工厂模式
        new OrderPizza2();
    }
}

二、工厂方法模式

==部分抽象==

(一)看一个新的需求

披萨项目新的需求:客户在点披萨时,可以点不同口味的披萨,比如 北京的奶酪 pizza、北京的胡椒 pizza 或者是伦敦的奶酪 pizza、伦敦的胡椒 pizza。

使用简单工厂模式,创建不同的简单工厂类,比如 BJPizzaSimpleFactory、LDPizzaSimpleFactory 等等.从当前这个案例来说,也是可以的,但是考虑到项目的规模,以及软件的可维护性、可扩展性并不是特别好

使用工厂方法模式

(二)工厂方法模式介绍

(三)工厂方法模式应用案例

1) 披萨项目新的需求:客户在点披萨时,可以点不同口味的披萨,比如 北京的奶酪 pizza、北京的胡椒 pizza 或者是伦敦的奶酪 pizza、伦敦的胡椒 pizza

2) 思路分析图解

工厂方法模式

下面是 Pizza 抽象类和三个具体的实现类

package com.gjxaiou.factory.absfactory.pizzastore.pizza;

//将Pizza 类做成抽象
public abstract class Pizza {
    protected String name; //名字

    //准备原材料, 不同的披萨不一样,因此,我们做成抽象方法
    public abstract void prepare();

    public void bake() {
        System.out.println(name + " baking;");
    }

    public void cut() {
        System.out.println(name + " cutting;");
    }

    //打包
    public void box() {
        System.out.println(name + " boxing;");
    }

    public void setName(String name) {
        this.name = name;
    }
}

//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class BJCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("北京的奶酪pizza");
        System.out.println(" 北京的奶酪pizza 准备原材料");
    }
}
//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class BJPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("北京的胡椒pizza");
        System.out.println(" 北京的胡椒pizza 准备原材料");
    }
}
//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class LDCheesePizza extends Pizza{
    @Override
    public void prepare() {
        setName("伦敦的奶酪pizza");
        System.out.println(" 伦敦的奶酪pizza 准备原材料");
    }
}
//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class LDPepperPizza extends Pizza{
    @Override
    public void prepare() {
        setName("伦敦的胡椒pizza");
        System.out.println(" 伦敦的胡椒pizza 准备原材料");
    }
}

具体的工厂实现

package com.gjxaiou.factory.factorymethod.pizzastore.order;

import com.gjxaiou.factory.factorymethod.pizzastore.pizza.BJCheesePizza;
import com.gjxaiou.factory.factorymethod.pizzastore.pizza.BJPepperPizza;
import com.gjxaiou.factory.factorymethod.pizzastore.pizza.Pizza;

public class BJOrderPizza extends OrderPizza {
    @Override
    Pizza createPizza(String orderType) {
    
        Pizza pizza = null;
        if(orderType.equals("cheese")) {
            pizza = new BJCheesePizza();
        } else if (orderType.equals("pepper")) {
            pizza = new BJPepperPizza();
        }
        return pizza;
    }
}

//----------------------------------------------------
package com.gjxaiou.factory.factorymethod.pizzastore.order;

import com.gjxaiou.factory.factorymethod.pizzastore.pizza.BJCheesePizza;
import com.gjxaiou.factory.factorymethod.pizzastore.pizza.BJPepperPizza;
import com.gjxaiou.factory.factorymethod.pizzastore.pizza.LDCheesePizza;
import com.gjxaiou.factory.factorymethod.pizzastore.pizza.LDPepperPizza;
import com.gjxaiou.factory.factorymethod.pizzastore.pizza.Pizza;

public class LDOrderPizza extends OrderPizza {
    @Override
    Pizza createPizza(String orderType) {
    
        Pizza pizza = null;
        if(orderType.equals("cheese")) {
            pizza = new LDCheesePizza();
        } else if (orderType.equals("pepper")) {
            pizza = new LDPepperPizza();
        }
        return pizza;
    }
}

订购 Order

package com.gjxaiou.factory.factorymethod.pizzastore.order;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.gjxaiou.factory.factorymethod.pizzastore.pizza.Pizza;

public abstract class OrderPizza {

    //定义一个抽象方法,createPizza , 让各个工厂子类自己实现
    abstract Pizza createPizza(String orderType);
    
    // 构造器
    public OrderPizza() {
        Pizza pizza = null;
        String orderType; // 订购披萨的类型
        do {
            orderType = getType();
            pizza = createPizza(orderType); //抽象方法,由工厂子类完成
            //输出pizza 制作过程
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();

        } while (true);
    }

    // 写一个方法,可以获取客户希望订购的披萨种类
    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza 种类:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}

测试方法

package com.gjxaiou.factory.factorymethod.pizzastore.order;

public class PizzaStore {
    public static void main(String[] args) {
        String loc = "bj";
        if (loc.equals("bj")) {
            //创建北京口味的各种Pizza
            new BJOrderPizza();
        } else {
            //创建伦敦口味的各种Pizza
            new LDOrderPizza();
        }
    }
}

三、抽象工厂模式

==完全抽象==

(一)基本介绍

5) 类图

抽象工厂模式

(二)抽象工厂模式应用实例

下面是 Pizza 抽象类和三个具体的实现类

package com.gjxaiou.factory.absfactory.pizzastore.pizza;

//将Pizza 类做成抽象
public abstract class Pizza {
    protected String name; //名字

    //准备原材料, 不同的披萨不一样,因此,我们做成抽象方法
    public abstract void prepare();

    public void bake() {
        System.out.println(name + " baking;");
    }

    public void cut() {
        System.out.println(name + " cutting;");
    }

    //打包
    public void box() {
        System.out.println(name + " boxing;");
    }

    public void setName(String name) {
        this.name = name;
    }
}

//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class BJCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("北京的奶酪pizza");
        System.out.println(" 北京的奶酪pizza 准备原材料");
    }
}
//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class BJPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("北京的胡椒pizza");
        System.out.println(" 北京的胡椒pizza 准备原材料");
    }
}
//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class LDCheesePizza extends Pizza{
    @Override
    public void prepare() {
        setName("伦敦的奶酪pizza");
        System.out.println(" 伦敦的奶酪pizza 准备原材料");
    }
}
//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.pizza;

public class LDPepperPizza extends Pizza{
    @Override
    public void prepare() {
        setName("伦敦的胡椒pizza");
        System.out.println(" 伦敦的胡椒pizza 准备原材料");
    }
}

工厂抽象化和具体的工厂实现

package com.gjxaiou.factory.absfactory.pizzastore.order;

import com.gjxaiou.factory.absfactory.pizzastore.pizza.Pizza;

//一个抽象工厂模式的抽象层(接口)
public interface AbsFactory {
    //让下面的工厂子类来 具体实现
    public Pizza createPizza(String orderType);
}

//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.order;

import com.gjxaiou.factory.absfactory.pizzastore.pizza.BJCheesePizza;
import com.gjxaiou.factory.absfactory.pizzastore.pizza.BJPepperPizza;
import com.gjxaiou.factory.absfactory.pizzastore.pizza.Pizza;

//这是工厂子类
public class BJFactory implements AbsFactory {
    @Override
    public Pizza createPizza(String orderType) {
        System.out.println("~使用的是抽象工厂模式~");
        Pizza pizza = null;
        if(orderType.equals("cheese")) {
            pizza = new BJCheesePizza();
        } else if (orderType.equals("pepper")){
            pizza = new BJPepperPizza();
        }
        return pizza;
    }
}

//----------------------------------------------------
package com.gjxaiou.factory.absfactory.pizzastore.order;

import com.gjxaiou.factory.absfactory.pizzastore.pizza.LDCheesePizza;
import com.gjxaiou.factory.absfactory.pizzastore.pizza.LDPepperPizza;
import com.gjxaiou.factory.absfactory.pizzastore.pizza.Pizza;

public class LDFactory implements AbsFactory {
    @Override
    public Pizza createPizza(String orderType) {
        System.out.println("~使用的是抽象工厂模式~");
        Pizza pizza = null;
        if (orderType.equals("cheese")) {
            pizza = new LDCheesePizza();
        } else if (orderType.equals("pepper")) {
            pizza = new LDPepperPizza();
        }
        return pizza;
    }
}

订购 Order

package com.gjxaiou.factory.absfactory.pizzastore.order;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.gjxaiou.factory.absfactory.pizzastore.pizza.Pizza;

public class OrderPizza {

    AbsFactory factory;

    // 构造器
    public OrderPizza(AbsFactory factory) {
        setFactory(factory);
    }

    private void setFactory(AbsFactory factory) {
        Pizza pizza = null;
        String orderType = ""; // 用户输入
        this.factory = factory;
        do {
            orderType = getType();
            // factory 可能是北京的工厂子类,也可能是伦敦的工厂子类
            pizza = factory.createPizza(orderType);
            if (pizza != null) { // 订购ok
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("订购失败");
                break;
            }
        } while (true);
    }

    // 写一个方法,可以获取客户希望订购的披萨种类
    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza 种类:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}

测试方法

package com.gjxaiou.factory.absfactory.pizzastore.order;

public class PizzaStore {

    public static void main(String[] args) {
        //new OrderPizza(new BJFactory());
        new OrderPizza(new LDFactory());
    }
}

(四)工厂模式在 JDK-Calendar 应用的源码分析

1) JDK 中的 Calendar 类中,就使用了简单工厂模式

(五)工厂模式小结

将实例化对象的代码提取出来,放到一个类(工厂类)中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性。

标签:gjxaiou,factory,模式,工厂,pizza,设计模式,pizzastore,public,Pizza
来源: https://www.cnblogs.com/qq438649499/p/12178277.html