看一个具体的需求
看一个披萨的项目 :要便于披萨种类的扩展,要便于维护
1)披萨的种类很多(比如GreekPizz、CheesePizz等)
2)披萨的制作有prepare、bake、cut、box
3)完成披萨店订购功能。
传统的方式 :
package com.example.demo.simplefactory.pizzastore.pizza;
/**
* 将Pizza 类做成抽象
* @author Administrator
*
*/
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.example.demo.simplefactory.pizzastore.pizza;
public class GreekPizza extends Pizza{
@Override
public void prepare() {
System.out.println(" 给希腊披萨 准备原材料");
}
}
package com.example.demo.simplefactory.pizzastore.pizza;
public class CheesePizza extends Pizza{
@Override
public void prepare() {
System.out.println(" 给制作奶酪披萨,准备原材料");
}
}
package com.example.demo.simplefactory.pizzastore.order;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;
import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
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 {
break;
}
// 输出pizza 制作过程
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} while (true);
}
/**
* 写一个方法,可以获取客户希望订购的披萨种类
* @return
*/
private String getType() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type : ");
String string = bufferedReader.readLine();
return string;
} catch(IOException e) {
e.printStackTrace();
return "";
}
}
}
package com.example.demo.simplefactory.pizzastore.order;
/**
* 相当于一个客户端,发出订购
* @author Administrator
*
*/
public class PizzaStore {
public static void main(String[] args) {
new OrderPizza();
}
}
传统的方式优缺点 :
1)优点是比较好理解,简单易操作。
2)缺点是违反了设计模式的ocp原则,即对扩展开放,对修改关闭。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码。
3)比如我们这时要新增加一个Pizza的种类,我们需要如下修改。
package com.example.demo.simplefactory.pizzastore.pizza;
public class PepperPizza extends Pizza{
@Override
public void prepare() {
System.out.println(" 给胡椒披萨准备原材料 ");
}
}
package com.example.demo.simplefactory.pizzastore.order;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;
import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;
import com.example.demo.simplefactory.pizzastore.pizza.PepperPizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
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);
}
/**
* 写一个方法,可以获取客户希望订购的披萨种类
* @return
*/
private String getType() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type : ");
String string = bufferedReader.readLine();
return string;
} catch(IOException e) {
e.printStackTrace();
return "";
}
}
}
4)改进的思路分析
分析 :修改代码可以接受,但是如果我们在其它的地方也有创建Pizza的代码,就意味着,也需要修改,而创建Pizza的代码,往往有多处。
思路 :把创建Pizza对象封装到一个类中,这样我们有新的Pizza种类时,只需要修改该类即可,其它有创建到Pizza对象的代码就不需要修改了 -》
基本介绍
1)简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式
2)简单工厂模式 :定义了一个创建对象的类,由这个类来封装实例化对象的行位(代码)
3)在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式。
package com.example.demo.simplefactory.pizzastore.order;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;
import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;
import com.example.demo.simplefactory.pizzastore.pizza.PepperPizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
/**
* 简单工厂类
* @author Administrator
*
*/
public class SimpleFactory {
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;
}
}
package com.example.demo.simplefactory.pizzastore.order;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;
import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;
import com.example.demo.simplefactory.pizzastore.pizza.PepperPizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
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); }
*/
/**
* 构造器
* @param simpleFactory
*/
public OrderPizza(SimpleFactory simpleFactory) {
setFactory(simpleFactory);
}
/**
* 定义一个简单工厂对象
*/
private SimpleFactory simpleFactory;
private Pizza pizza = null;
private void setFactory(SimpleFactory simpleFactory) {
// 用户输入的
String orderTypeString = "";
// 设置简单工厂对象
this.simpleFactory = simpleFactory;
do {
orderTypeString = getType();
pizza = this.simpleFactory.createPizza(orderTypeString);
// 输出pizza
// 订购成功
if (pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println(" 订购披萨失败 ");
break;
}
} while (true);
}
/**
* 写一个方法,可以获取客户希望订购的披萨种类
* @return
*/
private String getType() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type : ");
String string = bufferedReader.readLine();
return string;
} catch(IOException e) {
e.printStackTrace();
return "";
}
}
}
package com.example.demo.simplefactory.pizzastore.order;
/**
* 相当于一个客户端,发出订购
* @author Administrator
*
*/
public class PizzaStore {
public static void main(String[] args) {
//new OrderPizza();
// 使用简单工厂模式
new OrderPizza(new SimpleFactory());
System.out.println("~~退出程序~~");
}
}
看一个新的需求
披萨项目新的需求 : 客户在点披萨时,可以点不同口味的披萨,比如北京的奶酪pizza、北京的胡椒pizza或者是伦敦的奶酪pizza、伦敦的胡椒pizza。
思路1
使用简单工厂模式,创建不同的简单工厂类,比如BJPizzaSimpleFactory、LDPizzaSimpleFactory等等,从当前这个案例来说,也是可以的,但是考虑到项目的规模,以及软件的可维护性、可扩展性并不是特别好
思路2
使用工厂方法模式
工厂方法模式介绍
工厂方法模式设计方案 : 将披萨项目的实例化功能抽象成抽象方法,在不同的口味点餐子类中具体实现。
工厂方法模式 : 定义了一个创建对象的抽象方法,由子类决定要实例化的类。工厂方法模式将对象的实例化推迟到子类。
package com.example.demo.factorymethod.pizzastore.order;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
import com.fasterxml.jackson.databind.deser.ValueInstantiator.Gettable;
public abstract class OrderPizza {
public OrderPizza() {
Pizza pizza = null;
// 订购披萨的类型
String orderType;
do {
orderType = getType();
createPizza(orderType);
// 输出Pizza 制作过程
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} while (true);
}
/**
* 定义一个抽象方法,createPizza,让各个工厂子类自己实现
* @param orderType
* @return
*/
abstract Pizza createPizza(String orderType);
/**
* 写一个方法,可以获取客户希望订购的披萨种类
* @return
*/
private String getType() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type : ");
String string = bufferedReader.readLine();
return string;
} catch(IOException e) {
e.printStackTrace();
return "";
}
}
}
package com.example.demo.factorymethod.pizzastore.order;
import org.apache.tomcat.util.security.Escape;
import com.example.demo.factorymethod.pizzastore.pizza.LDCheesePizza;
import com.example.demo.factorymethod.pizzastore.pizza.LDPepperPizza;
import com.example.demo.simplefactory.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;
}
}
package com.example.demo.factorymethod.pizzastore.order;
import com.example.demo.factorymethod.pizzastore.pizza.BJCheesPizza;
import com.example.demo.factorymethod.pizzastore.pizza.BJPepperPizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJOrderPizza extends OrderPizza{
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if (orderType.equals("cheese")) {
pizza = new BJCheesPizza();
} else if (orderType.equals("pepper")) {
pizza = new BJPepperPizza();
}
return pizza;
}
}
package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJCheesPizza extends Pizza{
@Override
public void prepare() {
setName("北京的奶酪pizza");
System.out.println(" 北京的奶酪pizza 准备原材料 ");
}
}
package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJPepperPizza extends Pizza{
@Override
public void prepare() {
setName(" 北京的胡椒pizza ");
System.out.println(" 北京的胡椒pizza 准备原材料 ");
}
}
package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDCheesePizza extends Pizza{
@Override
public void prepare() {
setName(" 伦敦的奶酪pizza ");
System.out.println(" 伦敦的奶酪pizza 准备原材料 ");
}
}
package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDPepperPizza extends Pizza{
@Override
public void prepare() {
setName(" 伦敦的胡椒pizza ");
System.out.println(" 伦敦的胡椒pizza 准备原材料 ");
}
}
package com.example.demo.factorymethod.pizzastore.order;
public class PizzaStore {
public static void main(String[] args) {
// 创建北京口味的各种Pizza
new BJOrderPizza();
}
}
基本介绍
1)抽象工厂模式 : 定义了一个interface用于创建相关或有依赖关系的对象簇,而无需指明具体的类
2)抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合
3)从设计层面看,抽象工厂模式就是对简单工厂模式的改进(或者称为进一步的抽象)。
4)将工厂抽象成两层,AbsFactory(抽象工厂)和具体实现的工厂子类。程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。
5)类图
package com.example.demo.absfactory.pizzastore.order;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
/**
* 一个抽象工厂模式的抽象层(接口)
* @author Administrator
*
*/
public interface AbsFactory {
/**
* 让下面的工厂子类来 具体实现
* @param orderType
* @return
*/
Pizza createPizza(String orderType);
}
package com.example.demo.absfactory.pizzastore.order;
import com.example.demo.absfactory.pizzastore.pizza.BJCheesPizza;
import com.example.demo.factorymethod.pizzastore.pizza.BJPepperPizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
/**
* 这是工厂子类
* @author Administrator
*
*/
public class BJFactory implements AbsFactory{
@Override
public Pizza createPizza(String orderType) {
System.out.println("~使用的是抽象工厂模式~");
Pizza pizza = null;
if (orderType.equals("cheese")) {
pizza = new BJCheesPizza();
} else if (orderType.equals("pepper") ) {
pizza = new BJPepperPizza();
}
return pizza;
}
}
package com.example.demo.absfactory.pizzastore.order;
import com.example.demo.absfactory.pizzastore.pizza.LDCheesePizza;
import com.example.demo.absfactory.pizzastore.pizza.LDPepperPizza;
import com.example.demo.simplefactory.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;
}
}
package com.example.demo.absfactory.pizzastore.order;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class OrderPizza {
private AbsFactory factory;
/**
* 构造器
* @param factory
*/
public OrderPizza(AbsFactory factory) {
setFactory(factory);
}
private void setFactory(AbsFactory factory) {
Pizza pizza = null;
// 用户输入
String orderTypeString = "";
this.factory = factory;
do {
orderTypeString = getType();
pizza = factory.createPizza(orderTypeString);
if (pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println("~订购失败~");
break;
}
} while (true);
}
/**
* 写一个方法,可以获取客户希望订购的披萨种类
* @return
*/
private String getType() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type : ");
String string = bufferedReader.readLine();
return string;
} catch(IOException e) {
e.printStackTrace();
return "";
}
}
}
package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJCheesPizza extends Pizza{
@Override
public void prepare() {
setName("北京的奶酪pizza");
System.out.println(" 北京的奶酪pizza 准备原材料 ");
}
}
package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJPepperPizza extends Pizza{
@Override
public void prepare() {
setName(" 北京的胡椒pizza ");
System.out.println(" 北京的胡椒pizza 准备原材料 ");
}
}
package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDCheesePizza extends Pizza{
@Override
public void prepare() {
setName(" 伦敦的奶酪pizza ");
System.out.println(" 伦敦的奶酪pizza 准备原材料 ");
}
}
package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDPepperPizza extends Pizza{
@Override
public void prepare() {
setName(" 伦敦的胡椒pizza ");
System.out.println(" 伦敦的胡椒pizza 准备原材料 ");
}
}
package com.example.demo.absfactory.pizzastore.order;
public class PizzaStore {
public static void main(String[] args) {
new OrderPizza(new BJFactory());
}
}
工厂模式在JDK-Calendar 应用的源码分析
1)JDK中的Calendar类中,使用了简单工厂模式
public static Calendar getInstance()
{
return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
}
private static Calendar createCalendar(TimeZone zone,
Locale aLocale)
{
CalendarProvider provider =
LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
.getCalendarProvider();
if (provider != null) {
try {
return provider.getInstance(zone, aLocale);
} catch (IllegalArgumentException iae) {
// fall back to the default instantiation
}
}
Calendar cal = null;
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
switch (caltype) {
case "buddhist":
cal = new BuddhistCalendar(zone, aLocale);
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case "gregory":
cal = new GregorianCalendar(zone, aLocale);
break;
}
}
}
if (cal == null) {
// If no known calendar type is explicitly specified,
// perform the traditional way to create a Calendar:
// create a BuddhistCalendar for th_TH locale,
// a JapaneseImperialCalendar for ja_JP_JP locale, or
// a GregorianCalendar for any other locales.
// NOTE: The language, country and variant strings are interned.
if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale);
} else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
&& aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
return cal;
}
工厂模式小结 :
1)工厂模式的意义
将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性。
2)三种工厂模式
3)设计模式的依赖抽象原则
创建对象实例时,不要直接new类,而是把这个new类的动作放在一个工厂的方法中,并返回。有的书上说,变量不要直接持有具体类的引用。
不要让类继承具体类,而是继承抽象类或者是实现interface(接口)
不要覆盖基类中已经实现的方法。
@[TOC](图解Java设计模式之工厂模式)## 简单工厂模式看一个具体的需求看一个披萨的项目 :要便于披萨种类的扩展,要便于维护1)披萨的种类很多(比如GreekPizz、CheesePizz等)2)披萨的制作有prepare、bake、cut、box3)完成披萨店订购功能。传统的方式 :![在这里插入图片描述](https://img-blog.csdnimg.cn/20200302165146817.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_70)```package com.example.demo.simplefactory.pizzastore.pizza;
/** * 将Pizza 类做成抽象 * @author Administrator * */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.example.demo.simplefactory.pizzastore.pizza;
public class GreekPizza extends Pizza{
@Overridepublic void prepare() {System.out.println(" 给希腊披萨 准备原材料");}
}package com.example.demo.simplefactory.pizzastore.pizza;
public class CheesePizza extends Pizza{
@Overridepublic void prepare() {System.out.println(" 给制作奶酪披萨,准备原材料");}
}package com.example.demo.simplefactory.pizzastore.order;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
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 {break;}// 输出pizza 制作过程pizza.prepare();pizza.bake();pizza.cut();pizza.box();} while (true);}/** * 写一个方法,可以获取客户希望订购的披萨种类 * @return */private String getType() {try {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));System.out.println("input pizza type : ");String string = bufferedReader.readLine();return string;} catch(IOException e) {e.printStackTrace();return "";}}}package com.example.demo.simplefactory.pizzastore.order;
/** * 相当于一个客户端,发出订购 * @author Administrator * */public class PizzaStore {
public static void main(String[] args) {new OrderPizza();}
}```传统的方式优缺点 :1)优点是比较好理解,简单易操作。2)缺点是违反了设计模式的ocp原则,即对扩展开放,对修改关闭。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码。3)比如我们这时要新增加一个Pizza的种类,我们需要如下修改。![在这里插入图片描述](https://img-blog.csdnimg.cn/20200302165644275.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_70)
```package com.example.demo.simplefactory.pizzastore.pizza;
public class PepperPizza extends Pizza{
@Overridepublic void prepare() {System.out.println(" 给胡椒披萨准备原材料 ");}
}package com.example.demo.simplefactory.pizzastore.order;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;import com.example.demo.simplefactory.pizzastore.pizza.PepperPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
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);}/** * 写一个方法,可以获取客户希望订购的披萨种类 * @return */private String getType() {try {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));System.out.println("input pizza type : ");String string = bufferedReader.readLine();return string;} catch(IOException e) {e.printStackTrace();return "";}}}```4)改进的思路分析分析 :修改代码可以接受,但是如果我们在其它的地方也有创建Pizza的代码,就意味着,也需要修改,而创建Pizza的代码,往往有多处。思路 :把创建Pizza对象封装到一个类中,这样我们有新的Pizza种类时,只需要修改该类即可,其它有创建到Pizza对象的代码就不需要修改了 -》## 简单工厂模式**基本介绍**1)简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式2)简单工厂模式 :定义了一个创建对象的类,由这个类来封装实例化对象的行位(代码)3)在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式。![在这里插入图片描述](https://img-blog.csdnimg.cn/20200302171554396.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_70)![在这里插入图片描述](https://img-blog.csdnimg.cn/20200302175645117.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_70)```package com.example.demo.simplefactory.pizzastore.order;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;import com.example.demo.simplefactory.pizzastore.pizza.PepperPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
/** * 简单工厂类 * @author Administrator * */public class SimpleFactory {
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;}}package com.example.demo.simplefactory.pizzastore.order;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.CheesePizza;import com.example.demo.simplefactory.pizzastore.pizza.GreekPizza;import com.example.demo.simplefactory.pizzastore.pizza.PepperPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
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); } *//** * 构造器 * @param simpleFactory */public OrderPizza(SimpleFactory simpleFactory) {setFactory(simpleFactory);}/** * 定义一个简单工厂对象 */private SimpleFactory simpleFactory;private Pizza pizza = null;private void setFactory(SimpleFactory simpleFactory) {// 用户输入的String orderTypeString = "";// 设置简单工厂对象this.simpleFactory = simpleFactory;do {orderTypeString = getType();pizza = this.simpleFactory.createPizza(orderTypeString);// 输出pizza// 订购成功if (pizza != null) {pizza.prepare();pizza.bake();pizza.cut();pizza.box();} else {System.out.println(" 订购披萨失败 ");break;}} while (true);}/** * 写一个方法,可以获取客户希望订购的披萨种类 * @return */private String getType() {try {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));System.out.println("input pizza type : ");String string = bufferedReader.readLine();return string;} catch(IOException e) {e.printStackTrace();return "";}}}package com.example.demo.simplefactory.pizzastore.order;
/** * 相当于一个客户端,发出订购 * @author Administrator * */public class PizzaStore {
public static void main(String[] args) {//new OrderPizza();// 使用简单工厂模式new OrderPizza(new SimpleFactory());System.out.println("~~退出程序~~");}
}```## 工厂方法模式**看一个新的需求**披萨项目新的需求 : 客户在点披萨时,可以点不同口味的披萨,比如北京的奶酪pizza、北京的胡椒pizza或者是伦敦的奶酪pizza、伦敦的胡椒pizza。**思路1**使用简单工厂模式,创建不同的简单工厂类,比如BJPizzaSimpleFactory、LDPizzaSimpleFactory等等,从当前这个案例来说,也是可以的,但是考虑到项目的规模,以及软件的可维护性、可扩展性并不是特别好**思路2**使用工厂方法模式**工厂方法模式介绍**工厂方法模式设计方案 : 将披萨项目的实例化功能抽象成抽象方法,在不同的口味点餐子类中具体实现。工厂方法模式 : 定义了一个创建对象的抽象方法,由子类决定要实例化的类。工厂方法模式将**对象的实例化推迟到子类**。![!\[在这里插入图片描述\](https://img-blog.csdnimg.cn/20200303090520160.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_](https://img-blog.csdnimg.cn/20200303092224750.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_70)```package com.example.demo.factorymethod.pizzastore.order;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;import com.fasterxml.jackson.databind.deser.ValueInstantiator.Gettable;
public abstract class OrderPizza {public OrderPizza() {Pizza pizza = null;// 订购披萨的类型String orderType;do {orderType = getType();createPizza(orderType);// 输出Pizza 制作过程pizza.prepare();pizza.bake();pizza.cut();pizza.box();} while (true);}/** * 定义一个抽象方法,createPizza,让各个工厂子类自己实现 * @param orderType * @return */abstract Pizza createPizza(String orderType);/** * 写一个方法,可以获取客户希望订购的披萨种类 * @return */private String getType() {try {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));System.out.println("input pizza type : ");String string = bufferedReader.readLine();return string;} catch(IOException e) {e.printStackTrace();return "";}}}package com.example.demo.factorymethod.pizzastore.order;
import org.apache.tomcat.util.security.Escape;
import com.example.demo.factorymethod.pizzastore.pizza.LDCheesePizza;import com.example.demo.factorymethod.pizzastore.pizza.LDPepperPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDOrderPizza extends OrderPizza{
@OverridePizza createPizza(String orderType) {Pizza pizza = null;if (orderType.equals("cheese")) {pizza = new LDCheesePizza();} else if (orderType.equals("pepper")) {pizza = new LDPepperPizza();}return pizza;}
}package com.example.demo.factorymethod.pizzastore.order;
import com.example.demo.factorymethod.pizzastore.pizza.BJCheesPizza;import com.example.demo.factorymethod.pizzastore.pizza.BJPepperPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJOrderPizza extends OrderPizza{
@OverridePizza createPizza(String orderType) {Pizza pizza = null;if (orderType.equals("cheese")) {pizza = new BJCheesPizza();} else if (orderType.equals("pepper")) {pizza = new BJPepperPizza();}return pizza;}
}package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJCheesPizza extends Pizza{
@Overridepublic void prepare() {setName("北京的奶酪pizza");System.out.println(" 北京的奶酪pizza 准备原材料 ");}
}package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJPepperPizza extends Pizza{
@Overridepublic void prepare() {setName(" 北京的胡椒pizza ");System.out.println(" 北京的胡椒pizza 准备原材料 ");}
}package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDCheesePizza extends Pizza{
@Overridepublic void prepare() {setName(" 伦敦的奶酪pizza ");System.out.println(" 伦敦的奶酪pizza 准备原材料 ");}
}package com.example.demo.factorymethod.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDPepperPizza extends Pizza{
@Overridepublic void prepare() {setName(" 伦敦的胡椒pizza ");System.out.println(" 伦敦的胡椒pizza 准备原材料 ");}
}package com.example.demo.factorymethod.pizzastore.order;
public class PizzaStore {
public static void main(String[] args) {// 创建北京口味的各种Pizzanew BJOrderPizza();}
}
```## 抽象工厂模式基本介绍1)抽象工厂模式 : 定义了一个**interface**用于创建相关或有依赖关系的对象簇,而无需指明具体的类2)抽象工厂模式可以将**简单工厂模式**和**工厂方法模式**进行整合3)从设计层面看,抽象工厂模式就是对简单工厂模式的改进(或者称为进一步的抽象)。4)将工厂抽象成两层,AbsFactory(抽象工厂)和具体实现的工厂子类。程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。5)类图![在这里插入图片描述](https://img-blog.csdnimg.cn/20200303092755922.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_70)```package com.example.demo.absfactory.pizzastore.order;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
/** * 一个抽象工厂模式的抽象层(接口) * @author Administrator * */public interface AbsFactory {/** * 让下面的工厂子类来 具体实现 * @param orderType * @return */Pizza createPizza(String orderType);}package com.example.demo.absfactory.pizzastore.order;
import com.example.demo.absfactory.pizzastore.pizza.BJCheesPizza;import com.example.demo.factorymethod.pizzastore.pizza.BJPepperPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
/** * 这是工厂子类 * @author Administrator * */public class BJFactory implements AbsFactory{
@Overridepublic Pizza createPizza(String orderType) {System.out.println("~使用的是抽象工厂模式~");Pizza pizza = null;if (orderType.equals("cheese")) {pizza = new BJCheesPizza();} else if (orderType.equals("pepper") ) {pizza = new BJPepperPizza();}return pizza;}
}package com.example.demo.absfactory.pizzastore.order;
import com.example.demo.absfactory.pizzastore.pizza.LDCheesePizza;import com.example.demo.absfactory.pizzastore.pizza.LDPepperPizza;import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDFactory implements AbsFactory{
@Overridepublic 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;}
}package com.example.demo.absfactory.pizzastore.order;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class OrderPizza {private AbsFactory factory;/** * 构造器 * @param factory */public OrderPizza(AbsFactory factory) {setFactory(factory);}private void setFactory(AbsFactory factory) {Pizza pizza = null;// 用户输入String orderTypeString = "";this.factory = factory;do {orderTypeString = getType();pizza = factory.createPizza(orderTypeString);if (pizza != null) {pizza.prepare();pizza.bake();pizza.cut();pizza.box();} else {System.out.println("~订购失败~");break;}} while (true);}
/** * 写一个方法,可以获取客户希望订购的披萨种类 * @return */private String getType() {try {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));System.out.println("input pizza type : ");String string = bufferedReader.readLine();return string;} catch(IOException e) {e.printStackTrace();return "";}}}package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJCheesPizza extends Pizza{
@Overridepublic void prepare() {setName("北京的奶酪pizza");System.out.println(" 北京的奶酪pizza 准备原材料 ");}
}package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class BJPepperPizza extends Pizza{
@Overridepublic void prepare() {setName(" 北京的胡椒pizza ");System.out.println(" 北京的胡椒pizza 准备原材料 ");}
}package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDCheesePizza extends Pizza{
@Overridepublic void prepare() {setName(" 伦敦的奶酪pizza ");System.out.println(" 伦敦的奶酪pizza 准备原材料 ");}
}package com.example.demo.absfactory.pizzastore.pizza;
import com.example.demo.simplefactory.pizzastore.pizza.Pizza;
public class LDPepperPizza extends Pizza{
@Overridepublic void prepare() {setName(" 伦敦的胡椒pizza ");System.out.println(" 伦敦的胡椒pizza 准备原材料 ");}
}package com.example.demo.absfactory.pizzastore.order;
public class PizzaStore {public static void main(String[] args) {new OrderPizza(new BJFactory());}}```工厂模式在JDK-Calendar 应用的源码分析1)JDK中的Calendar类中,使用了简单工厂模式```public static Calendar getInstance() { return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT)); }private static Calendar createCalendar(TimeZone zone, Locale aLocale) { CalendarProvider provider = LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale) .getCalendarProvider(); if (provider != null) { try { return provider.getInstance(zone, aLocale); } catch (IllegalArgumentException iae) { // fall back to the default instantiation } }
Calendar cal = null;
if (aLocale.hasExtensions()) { String caltype = aLocale.getUnicodeLocaleType("ca"); if (caltype != null) { switch (caltype) { case "buddhist": cal = new BuddhistCalendar(zone, aLocale); break; case "japanese": cal = new JapaneseImperialCalendar(zone, aLocale); break; case "gregory": cal = new GregorianCalendar(zone, aLocale); break; } } } if (cal == null) { // If no known calendar type is explicitly specified, // perform the traditional way to create a Calendar: // create a BuddhistCalendar for th_TH locale, // a JapaneseImperialCalendar for ja_JP_JP locale, or // a GregorianCalendar for any other locales. // NOTE: The language, country and variant strings are interned. if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") { cal = new BuddhistCalendar(zone, aLocale); } else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja" && aLocale.getCountry() == "JP") { cal = new JapaneseImperialCalendar(zone, aLocale); } else { cal = new GregorianCalendar(zone, aLocale); } } return cal; }```工厂模式小结 : 1)工厂模式的意义将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性。2)三种工厂模式3)设计模式的**依赖抽象**原则创建对象实例时,不要直接new类,而是把这个new类的动作放在一个工厂的方法中,并返回。有的书上说,变量不要直接持有具体类的引用。不要让类继承具体类,而是继承抽象类或者是实现interface(接口)不要覆盖基类中已经实现的方法。