工厂模式
工厂模式应该是我们工作中使用最多的一个模式了,在spring中也是大量使用了工厂模式,比如我们常用的bean工厂的建设。我们用一个披萨建造的模块来学习一下工厂模式
我们先来看一下不用工厂模式所实现的代码
package com.company.factory;
public abstract class Pizza {
protected String name;
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+"披萨");
}
public void setName(String name) {
this.name = name;
}
}
package com.company.factory;
public class GreekPizza extends Pizza{
@Override
public void prepare() {
System.out.println("希腊披萨准备原材料");
}
}
package com.company.factory;
public class CheesePizza extends Pizza{
@Override
public void prepare() {
System.out.println("制作奶酪披萨,准备原材料");
}
}
package com.company.factory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderPizza {
public OrderPizza() throws IOException {
Pizza pizza = null;
do {
String pizzaType = getType();
if (pizzaType.equals("cheese") ){
pizza = new CheesePizza();
pizza.setName("希腊披萨");
}else if (pizzaType.equals("greek")){
pizza = new GreekPizza();
pizza.setName("奶酪披萨");
}else {
break;
}
pizza.prepare();
pizza.cut();
pizza.bake();
pizza.box();
}while (true);
}
private String getType() throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入披萨的种类:");
String str = bufferedReader.readLine();
return str;
}
}
package com.company.factory;
import java.io.IOException;
public class PizzaClient {
public static void main(String[] args) throws IOException {
new OrderPizza();
}
}
工厂模式实现
上面是的我们的传统的一个实现,下面我们实现的是简单工厂模式,大致就是把工厂类抽象出来
package com.company.FactoryMethod;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public abstract class PizzaOrderFactory {
abstract Pizza createPizza(String orderType);
public PizzaOrderFactory(){
Pizza pizza = null;
String orderType = "";
do {
orderType = getType();
pizza = createPizza(orderType);
if (pizza != null){
pizza.prepare();
pizza.bake();
pizza.box();
pizza.cut();
}else {
break;
}
}while (true);
}
private String getType(){
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入披萨的种类:");
String str = null;
try {
str = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
package com.company.FactoryMethod;
import com.company.newfactory.CheesePizza;
import com.company.newfactory.GreekPizza;
import com.company.newfactory.PepperPizza;
public class BJCreateOrder extends PizzaOrderFactory {
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if (orderType.equals("cheese")){
pizza = new BJCheesePizza();
pizza.setName("北京希腊披萨");
}else if (orderType.equals("greek")){
pizza = new BJGreekPizza();
pizza.setName("北京奶酪披萨");
}else if (orderType.equals("prepare")){
pizza = new BJPepperPizza();
pizza.setName("北京胡椒披萨");
}else {
System.out.println("订购失败");
}
return pizza;
}
}
package com.company.FactoryMethod;
public class TJCreateOrder extends PizzaOrderFactory {
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if (orderType.equals("cheese")){
pizza = new TJCheesePizza();
pizza.setName("天津希腊披萨");
}else if (orderType.equals("greek")){
pizza = new TJGreekPizza();
pizza.setName("天津奶酪披萨");
}else if (orderType.equals("prepare")){
pizza = new TJPepperPizza();
pizza.setName("天津胡椒披萨");
}else {
System.out.println("订购失败");
}
return pizza;
}
}
贴一下我们的包结构
本篇也只是记录工厂的方式方法,没有做具体的扩展详细。工厂模式就记录这么多了