目录
1.简单工厂模式
1.1类图
1.2 代码示例
2.工厂方法模式
2.1 类图
2.2 代码示例
3.抽象工厂模式
3.1 类图
3.2 代码示例
实际应用:
总结:
1.简单工厂模式
定义了一个创建对象的类,由这个类来封装实力化对象的行为。
1.1类图
1.2 代码示例
pizza基类
package org.example.factory.simplefactory.pizzastore.pizza;
import lombok.Data;
@Data
public abstract class Pizza {
String name = "";
// 准备pizza的原材料
public abstract void prepare();
public void bake(){
System.out.println(name +" 烘培 ");
}
public void cut(){
System.out.println(name +" 裁剪 ");
}
public void box(){
System.out.println(name +" 开始打包... ");
}
}
奶酪披萨
package org.example.factory.simplefactory.pizzastore.pizza;
public class ChessPizza extends Pizza {
@Override
public void prepare() {
System.out.println(" 奶酪披萨 准材料 ");
}
}
希腊披萨
package org.example.factory.simplefactory.pizzastore.pizza;
public class GreekPizza extends Pizza {
@Override
public void prepare() {
System.out.println(" 希腊披萨 准备材料");
}
}
简单工厂类
package org.example.factory.simplefactory.pizzastore.order;
import org.example.factory.simplefactory.pizzastore.pizza.ChessPizza;
import org.example.factory.simplefactory.pizzastore.pizza.GreekPizza;
import org.example.factory.simplefactory.pizzastore.pizza.Pizza;
public class SimpleFactory {
public Pizza createPizza(String type){
Pizza pizza;
do{
if (type.equals("chess")) {
pizza = new ChessPizza();
pizza.setName("奶酪");
} else if (type.equals("greek")){
pizza = new GreekPizza();
pizza.setName("希腊");
} else {
break;
}
System.out.println(" 披萨制作完成! ");
return pizza;
}while (true);
return null;
}
}
订单类
package org.example.factory.simplefactory.pizzastore.order;
import org.example.factory.simplefactory.pizzastore.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderPizza {
SimpleFactory simpleFactory;
Pizza pizza;
public OrderPizza(SimpleFactory simpleFactory){
setSimpleFactory(simpleFactory);
}
public void setSimpleFactory(SimpleFactory simpleFactory){
String type = "";
do {
this.simpleFactory = simpleFactory;
type = getType();
pizza = this.simpleFactory.createPizza(type);
if (pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println(" 订购失败,没有这种类型的披萨~ ");
break;
}
} while (true);
}
public String getType(){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" 输入披萨的种类: ");
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
披萨商店主程序入口
package org.example.factory.simplefactory.pizzastore.order;
public class PizzaStore {
public static void main(String[] args) {
new OrderPizza(new SimpleFactory());
System.out.println(" 订单完成! ");
}
}
2.工厂方法模式
定义了一个创建对象的抽象方法,由子类决定要实例化的类,工厂方法模式将对象的实例化推迟到子类中去实现。
2.1 类图
2.2 代码示例
与简单工厂模式相比,在原有的披萨种类基础上添加了地区区分,在代码上修改了订单类,添加了两个订单子类。
订单类
package org.example.factory.factorymethod.pizzastore.order;
import org.example.factory.factorymethod.pizzastore.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderPizza {
Pizza pizza;
public OrderPizza(){
setSimpleFactory();
}
public void setSimpleFactory(){
String loc = "";
String type = "";
do {
loc = getType("地区");
if (loc.equals("bj")) {
type = getType("披萨种类");
pizza = BJOrderPizza.createPizza(type);
} else if (loc.equals("ld")) {
type = getType("披萨种类");
pizza = LDOrderPizza.createPizza(type);
} else {
System.out.println(" 订购失败,没有该地区~ ");
break;
}
if (pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println(" 订购失败,没有这种类型的披萨~ ");
break;
}
} while (true);
}
public String getType(String str){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" 输入"+str+": ");
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
BJ订单类
package org.example.factory.factorymethod.pizzastore.order;
import org.example.factory.factorymethod.pizzastore.pizza.BJApplePizza;
import org.example.factory.factorymethod.pizzastore.pizza.BJCheesePizza;
import org.example.factory.factorymethod.pizzastore.pizza.Pizza;
public class BJOrderPizza {
public static Pizza createPizza(String type){
Pizza pizza = null;
if (type.equals("apple")) {
pizza = new BJApplePizza();
pizza.setName("北京苹果披萨");
} else if (type.equals("cheese")) {
pizza = new BJCheesePizza();
pizza.setName("北京奶酪披萨");
}
return pizza;
}
}
LD订单类
package org.example.factory.factorymethod.pizzastore.order;
import org.example.factory.factorymethod.pizzastore.pizza.BJApplePizza;
import org.example.factory.factorymethod.pizzastore.pizza.BJCheesePizza;
import org.example.factory.factorymethod.pizzastore.pizza.Pizza;
public class LDOrderPizza {
public static org.example.factory.factorymethod.pizzastore.pizza.Pizza createPizza(String type){
Pizza pizza = null;
if (type.equals("apple")) {
pizza = new BJApplePizza();
pizza.setName("伦敦苹果披萨");
} else if (type.equals("cheese")) {
pizza = new BJCheesePizza();
pizza.setName("伦敦奶酪披萨");
}
return pizza;
}
}
3.抽象工厂模式
定义一个interface用于创建相关或有依赖关系的对象簇,而无需指明具体的类;将简单工厂模式和工厂方法模式整合,将工厂抽象出两层,一个是抽象工厂,一个是具体的实现的工厂子类。
3.1 类图
3.2 代码示例
新增抽象工厂类
package org.example.factory.absfactory.pizzastore.order;
import org.example.factory.absfactory.pizzastore.pizza.Pizza;
//抽象工厂类
public interface AbsFactory {
public Pizza createPizza(String type);
}
抽象工厂类的具体实现类
BJ工厂类用于创建对象并返回
package org.example.factory.absfactory.pizzastore.order;
import org.example.factory.absfactory.pizzastore.pizza.BJApplePizza;
import org.example.factory.absfactory.pizzastore.pizza.BJCheesePizza;
import org.example.factory.absfactory.pizzastore.pizza.Pizza;
public class BJFactory implements AbsFactory{
@Override
public Pizza createPizza(String type) {
System.out.println(" 使用的是抽象工厂模式~ ");
Pizza pizza = null;
if (type.equals("apple")) {
pizza = new BJApplePizza();
} else if (type.equals("cheese")) {
pizza = new BJCheesePizza();
}
return pizza;
}
}
LD工厂类
package org.example.factory.absfactory.pizzastore.order;
import org.example.factory.absfactory.pizzastore.pizza.LDApplePizza;
import org.example.factory.absfactory.pizzastore.pizza.LDCheesePizza;
import org.example.factory.absfactory.pizzastore.pizza.Pizza;
public class LDFactory implements AbsFactory{
@Override
public Pizza createPizza(String type) {
System.out.println(" 使用的是抽象工厂模式~ ");
Pizza pizza = null;
if (type.equals("apple")) {
pizza = new LDApplePizza();
} else if (type.equals("cheese")) {
pizza = new LDCheesePizza() ;
}
return pizza;
}
}
订单类
package org.example.factory.absfactory.pizzastore.order;
import org.example.factory.absfactory.pizzastore.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderPizza {
AbsFactory factory;
public OrderPizza(AbsFactory factory){
setFactory(factory);
}
private void setFactory(AbsFactory factory){
Pizza pizza = null;
String type = "";
this.factory = factory;
do {
type = getType("披萨种类");
pizza = factory.createPizza(type);
if (pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println(" 订购失败 ");
break;
}
} while (true);
}
private String getType(String str){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" 输入"+str+": ");
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
实际应用:
在JDK的源码java.util.Calendar中使用到了简单工厂模式:
总结:
设计模式的依赖抽象原则:
创建对象实例时,不要之间new类,而是把这个动作交给工厂的方法并返回;
不要让类继承具体类,而是继承抽象类或实现接口;
不要覆盖基类中已经实现的方法。