工厂模式
完整的代码可以到我的博客里下载
简单工厂模式(静态工厂模式)
业务
实现一个加工生产冰淇淋的业务
UML类图
核心demo
package FactoryPattern.singelFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PizzFactory {
public static Pizza getPizza( ) {
Pizza pizza=null;
String type="";
while(true) {
type=getType();
if(type.equals("Chicken")) {
pizza=new ChickenPizza("鸡肉披萨");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else if(type.equals("Prawn")) {
pizza=new ChickenPizza("鲜虾披萨");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("订购失败!");
break;
}
}
return pizza;
}
public static String getType() {
System.out.println("请输入要订购的Pizza:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name="";
try {
name=br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
}
工厂方法
核心demo
业务顶层的抽象类
package FactoryMethon;
public abstract class OrderPizza {
//创建pizza
abstract Pizza CreatePizza();
}
继承的俩子类
package FactoryMethon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BjOrderPizza extends OrderPizza {
BjOrderPizza (){
}
@Override
public Pizza CreatePizza() {
Pizza pizza=null;
while(true) {
String type=getType();
if(type.equals("chicken")) {
pizza=new BJChichenPizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else if(type.equals("prawn")) {
pizza =new BJParwnPizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("无此产品!");
break;
}
}
return pizza;
}
public static String getType() {
System.out.println("请输入要订购的Pizza:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name="";
try {
name=br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
}
package FactoryMethon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LDOrderPizza extends OrderPizza {
LDOrderPizza (){
}
@Override
public Pizza CreatePizza() {
Pizza pizza=null;
String type=getType();
if(type.equals("chichen")) {
pizza=new LDChichenPizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else if(type.equals("prawn")) {
pizza =new LDParwnPizza2();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("无此产品!");
}
return pizza;
}
public static String getType() {
System.out.println("请输入要订购的Pizza:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name="";
try {
name=br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
}
抽象工厂
核心demo
package AbstarctFactory;
public interface AbstractFactory {
//定义创建披萨的方法
Pizza CreatePizza();
}
package AbstarctFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BjFactory implements AbstractFactory {
@Override
public Pizza CreatePizza() {
Pizza pizza=null;
while(true) {
String type=getType();
if(type.equals("chicken")) {
pizza=new BJChichenPizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else if(type.equals("prawn")) {
pizza =new BJParwnPizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("无此产品!");
break;
}
}
return pizza;
}
public static String getType() {
System.out.println("请输入要订购的Pizza:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name="";
try {
name=br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
}
package AbstarctFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LdFactory implements AbstractFactory {
@Override
public Pizza CreatePizza() {
Pizza pizza = null;
while (true) {
String type = getType();
if (type.equals("chicken")) {
pizza = new BJChichenPizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else if (type.equals("prawn")) {
pizza = new BJParwnPizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println("无此产品!");
break;
}
}
return pizza;
}
public static String getType() {
System.out.println("请输入要订购的Pizza:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = "";
try {
name = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
}