23种设计模式之工厂模式
工厂模式分为简单工厂模式,方法工厂模式,以及抽象工厂模式
简单工厂模式
披萨类
----
package com.pm.factory.simplefactory.pizzastore2.pizza;
//将披萨类做成抽象的
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;
}
}
奶酪披萨类
----
public class CheesePizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
System.out.println("给制作奶酪披萨 准备原材料");
}
}
希腊披萨类
----
public class GreekPizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
System.out.println("给希腊披萨准备 原材料");
}
}
胡椒披萨类
----
public class PepperPizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
System.out.println(" 给胡椒披萨准备原材料");
}
}
定义工厂类
----
package com.pm.factory.simplefactory.pizzastore2.order;
import com.pm.factory.simplefactory.pizzastore2.pizza.CheesePizza;
import com.pm.factory.simplefactory.pizzastore2.pizza.GreekPizza;
import com.pm.factory.simplefactory.pizzastore2.pizza.PepperPizza;
import com.pm.factory.simplefactory.pizzastore2.pizza.Pizza;
//简单工厂类
public class SimpleFactory {
//根据orderType 返回对应的Pizza对象
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.pm.factory.simplefactory.pizzastore2.order;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.pm.factory.simplefactory.pizzastore2.pizza.Pizza;
public class OrderPizza {
//定义一个简单工厂对象
SimpleFactory simpleFactory;
Pizza pizza = null;
//构造器OrderPizza加载的时候调用,调用setFactory
public OrderPizza(SimpleFactory simpleFactory) {
setFactory(simpleFactory);
}
public void setFactory(SimpleFactory simpleFactory) {
String orderType = "";//用户输入的
this.simpleFactory = simpleFactory;//设置简单工厂对象
do {
orderType = getType();
this.simpleFactory.createPizza(orderType);
//输出pizza
if(pizza != null) {//订购成功
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println(" 订购披萨失败 ");
break;
}
}while(true);
}
// 写一个方法,可以获取订购客户的披萨种类
private String getType() {
try {
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza 种类:");
String str = strin.readLine();
return str;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "";
}
}
}
启动类
----
package com.pm.factory.simplefactory.pizzastore2.order;
/**
*
* @author Administrator
* 简单工厂模式(静态工厂模式)
* 优点是比较好理解,简单易操作
* 简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出
* 哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式
* 简单工厂模式:定义了一个创建对象的类,这个类来封装实例化对象的行为(代码)
* 在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式。
*/
//披萨店,相当于一个客户端,发出披萨订购
public class PizzaStore {
public static void main(String[] args) {
//使用简单工厂模式
new OrderPizza(new SimpleFactory());
System.out.println("~退出程序~");
}
}
这种方式优点便于理解,但是缺点也显而易见,违反了开闭原则(对扩展开发,对修改关闭),因为如果你要新增一个披萨的话则意味着要去修改工厂类,这种方式耦合性太强
方式二:方法工厂模式
披萨类
----
package com.pm.factory.factorymethod.pizzastore.pizza;
//将披萨类做成抽象的
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;
}
}
北京披萨
----
public class BJCheesePizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的奶酪pizza");
System.out.println("北京的奶酪pizza 准备原材料");
}
}
public class BJPeperPizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的胡椒pizza");
System.out.println("北京的胡椒pizza 准备原材料");
}
}
伦敦披萨
----
public class LDCheesePizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦的奶酪pizza");
System.out.println(" 伦敦的奶酪pizza 准备原材料");
}
}
public class LDPeperPizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦的胡椒pizza");
System.out.println("伦敦的胡椒pizza 准备原材料");
}
}
生产披萨类
----
package com.pm.factory.factorymethod.pizzastore.order;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.pm.factory.factorymethod.pizzastore.pizza.Pizza;
public abstract class OrderPizza {
//定义一个抽象方法,createPizza,让各个工厂子类自己实现
abstract Pizza createPizza(String orderType);
// 构造器
public OrderPizza() {
Pizza pizza = null;
String orderType;// 订购披萨的类型
do {
orderType = getType();
pizza = createPizza(orderType);//抽象方法,由工厂子类完成
//输出pizza 制作过程
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 种类:");
String str = strin.readLine();
return str;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "";
}
}
}
北京披萨工厂类
----
package com.pm.factory.factorymethod.pizzastore.order;
import com.pm.factory.factorymethod.pizzastore.pizza.BJCheesePizza;
import com.pm.factory.factorymethod.pizzastore.pizza.BJPeperPizza;
import com.pm.factory.factorymethod.pizzastore.pizza.Pizza;
public class BJOrderPizza extends OrderPizza{
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if(orderType.equals("cheese")) {
pizza = new BJCheesePizza();
}else if(orderType.equals("pepper")) {
pizza = new BJPeperPizza();
}
// TODO Auto-generated method stub
return pizza;
}
}
伦敦披萨工厂类
----
package com.pm.factory.factorymethod.pizzastore.order;
import com.pm.factory.factorymethod.pizzastore.pizza.LDCheesePizza;
import com.pm.factory.factorymethod.pizzastore.pizza.LDPeperPizza;
import com.pm.factory.factorymethod.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 LDPeperPizza();
}
// TODO Auto-generated method stub
return pizza;
}
}
披萨的创建交给两个工厂子类来完成,这种方式耦合性弱,符合开闭原则
抽象工厂模式
披萨类
----
package com.pm.factory.absfactory.pizzastore.pizza;
//将披萨类做成抽象的
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;
}
}
披萨产品类
----
public class LDPeperPizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦的胡椒pizza");
System.out.println("伦敦的胡椒pizza 准备原材料");
}
}
public class LDCheesePizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦的奶酪pizza");
System.out.println(" 伦敦的奶酪pizza 准备原材料");
}
}
public class BJPeperPizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的胡椒pizza");
System.out.println("北京的胡椒pizza 准备原材料");
}
}
public class BJCheesePizza extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的奶酪pizza");
System.out.println("北京的奶酪pizza 准备原材料");
}
}
抽象工厂类
----
import com.pm.factory.absfactory.pizzastore.pizza.Pizza;
//一个抽象工厂模式的抽象层(接口)
public interface AbsFactory {
//让下面的工厂子类 来具体实现
public Pizza createPizza(String orderType);
}
产品工厂制造类
----
//这是一个工厂子类
public class BJFactory implements AbsFactory{
@Override
public Pizza createPizza(String orderType) {
// TODO Auto-generated method stub
Pizza pizza = null;
if(orderType.equals("cheese")) {
pizza = new BJCheesePizza();
}else if(orderType.equals("pepper")) {
pizza = new BJPeperPizza();
}
return pizza;
}
}
public class LDFactory implements AbsFactory{
@Override
public Pizza createPizza(String orderType) {
// TODO Auto-generated method stub
Pizza pizza = null;
if(orderType.equals("cheese")) {
pizza = new BJCheesePizza();
}else if(orderType.equals("pepper")) {
pizza = new BJPeperPizza();
}
return pizza;
}
}
public class OrderPizza {
AbsFactory absFactory;
public OrderPizza(AbsFactory absFactory) {
setFactory(absFactory);
}
private void setFactory(AbsFactory absFactory) {
System.out.println("~使用的是抽象工厂模式~");
Pizza pizza = null;
String orderType = "";//用户输入
this.absFactory = absFactory;
do {
orderType = getType();
//factory 可能是北京的工厂子类,也可能是伦敦的工厂子类
pizza = absFactory.createPizza(orderType);
if(pizza != null) {//订购ok
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("订购失败");
break;
}
}while(true);
}
// 写一个方法,可以获取订购客户的披萨种类
private String getType() {
try {
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza 种类:");
String str = strin.readLine();
return str;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "";
}
}
}
启动类
----
public class PizzaStore {
public static void main(String[] args) {
// TODO Auto-generated method stub
new OrderPizza(new BJFactory());
}
}
抽象工厂类,在原有的抽象方法类更加抽象将工厂作为一个接口通过要制造披萨的两个子工厂进行实现,通过创建不同工厂的实例生产不同的披萨,这种方式耦合性弱,方便维护以及扩展,也符合开闭原则的思想
实际上在我们编写代码时就是要遵循方便维护解耦以及方便扩展的思想来进行操作,实际开发中可能会忽略此类问题