设计模式--工厂模式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式--工厂模式相关的知识,希望对你有一定的参考价值。
- 通过Pizza项目引出简单工厂模式
- 工厂方法模式
- 抽象工厂模式
- 关键点
- 披萨项目:要方便披萨 品种的扩展、要便于维护、要能运行时扩展
- 披萨族设计:
- 定义一个Pizza的抽象类,定义四种方法prepare()抽象方法因为每个都不一样,但是bake(), cut(), box()都会一样。
- 按照这种设计方式,会更改类的内容所以就会为被了对扩展的开发,遇到问题分成变化的部分和不变化的部分
- 披萨工厂设计:if…else if …else工厂模式解决的时对象实例化的问题,将类的实例化放到某个地方进行管理
简单工厂模式:
- 定义一个创建对象的类,由这个类封装实例化对象的行为。主要解决大量实例化某些某类的情况,需要将大量类的实例化放到一个合适的地方。简单工厂这个类就是进行大量类的实现的类。把变化的部分抽取出来,将维护和扩展分割开来降低了耦合度。
package gongchangmoshi;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-16
* Time: 12:19
*/
public abstract class Pizza
public String getName()
return name;
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");
package gongchangmoshi;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-16
* Time: 12:28
*/
public class SimplePizzaFactory
public Pizza createPizza(String orderType)
//将变化的部分放到这里
Pizza pizza = null;
if (orderType.equals("greek"))
pizza = new GreekPizza();
return pizza;
package gongchangmoshi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-16
* Time: 12:31
*/
public class OrderPizza
SimplePizzaFactory simplePizzaFactory;
public OrderPizza(SimplePizzaFactory simplePizzaFactory)
setSimplePizzaFactory(simplePizzaFactory)
public void setSimplePizzaFactory(SimplePizzaFactory simplePizzaFactory)
Pizza pizza = null;
String orderType;
this.simplePizzaFactory = simplePizzaFactory;
do
orderType = gettype();
pizza = simplePizzaFactory.createPizza(orderType);
if(pizza!=null)
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 type:");
String str = strin.readLine();
return str;
catch (IOException e)
e.printStackTrace();
return "";
工厂模式:
- 将披萨项目里的披萨对象实例化功能抽象成抽象方法,在不同加盟点具体实现功能。
- 披萨加盟店的困惑:
- 如果使用简单工厂来做就会发现使用简单工厂就会在每个地方创建一个简单工厂,不利于代码维护。
package gongchangmoshi.gongchangmoshi;
import gongchangmoshi.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:26
*/
public abstract class OrderPizza
public OrderPizza()
Pizza pizza = null;
String ordertype;
do
ordertype = getType();
pizza = createPizza(ordertype);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
while (true);
abstract Pizza createPizza(String orderType);
private String getType()
try
BufferedReader strin = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("input pizza type:");
String str = strin.readLine();
return str;
catch (IOException e)
e.printStackTrace();
return "";
package gongchangmoshi.gongchangmoshi;
import gongchangmoshi.GreekPizza;
import gongchangmoshi.Pizza;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:32
*/
public class LDOrderPizza extends OrderPizza
@Override
Pizza createPizza(String orderType)
Pizza pizza = null;
if(orderType.equals("gw"))
pizza = new GreekPizza();
return pizza;
抽象工厂模式:
- 定义了一个接口 用于创建相关或有依赖关系的对象族,而无需明确指定具体类。
- 将工厂的公用部分抽象成接口,封装不同的方法,实现pizza族实现不变的功能,在订单中传入的是抽象的工厂,具体实现的时候可以进行具体化更改,然后实现即可
package gongchangmoshi.chouxianggongchang;
import gongchangmoshi.Pizza;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:43
*/
public interface AbsFactory
public Pizza createPizza(String orderType);
package gongchangmoshi.chouxianggongchang;
import gongchangmoshi.GreekPizza;
import gongchangmoshi.Pizza;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:46
*/
public class LDOrderPizza implements AbsFactory
@Override
public Pizza createPizza(String orderType)
Pizza pizza = new GreekPizza();
System.out.println("制造纽约风味的pizza");
return pizza;
package gongchangmoshi.chouxianggongchang;
import com.sun.org.apache.xpath.internal.operations.Or;
import gongchangmoshi.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:44
*/
public class OrderPizza
AbsFactory absFactory;
public OrderPizza(AbsFactory absFactory)
public void setFactory(AbsFactory absFactory)
Pizza pizza = null;
String orderType;
this.absFactory = absFactory;
do
orderType = getType();
pizza = absFactory.createPizza(orderType);
if(pizza!=null)
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 type:");
String str = strin.readLine();
return str;
catch (IOException e)
e.printStackTrace();
return "";
package gongchangmoshi.chouxianggongchang;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:49
*/
public class PizzaStore
public static void main(String[] args)
OrderPizza orderPizza;
orderPizza = new OrderPizza(new LDOrderPizza());
以上是关于设计模式--工厂模式的主要内容,如果未能解决你的问题,请参考以下文章