简单工厂模式
基本介绍:
1.简单工厂模式是属于创建型模式。简单工厂模式是由一个工厂决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最实用的模式
2.简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)
3.在软件开发中,当我们会用到大量的创建某种,某类或者某批对象时,就是使用到工厂模式。
案例:披萨订购
传统方法的代码:
package com.fan.factory.simplefactory.pizzastore;
//将Pizza类做成抽象类
public abstract class Pizza {
protected String name; //名字
//准备原材料,不同的Pizza不一样,因此,我们做成抽象方法
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.fan.factory.simplefactory.pizzastore;
public class GreekPizza extends Pizza{
@Override
public void prepare() {
System.out.println("给希腊披萨准备材料~~");
}
}
package com.fan.factory.simplefactory.pizzastore;
public class CheesePizza extends Pizza{
@Override
public void prepare() {
System.out.println("给制作奶酪披萨准备原材料~~");
}
}
package com.fan.factory.simplefactory.pizzastore;
import java.io.BufferedReader;
import java.io.InputStreamReader;
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.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) {
return "";
}
}
}
package com.fan.factory.simplefactory.pizzastore;
//相当于一个客户端,发出订购
public class PizzaStore {
public static void main(String[] args) {
new OrderPizza();
}
}
传统方式的优缺点
1.优点是比较好理解,简单易操作
2.缺点是违反了设计模式ocp原则,即对拓展开放,对修改关闭。即当我们给类增加新功能的时候,尽量不要修改代码,或者尽可能少的修改代码。
3.比如我们这时增加一个Pizza的种类(Pepper披萨),这就需要做很多类的修改
改进的思路分析
分析:把创建Pizza对象封装在一个勒种,这样我们有新的Pizza种类时,只需要修改该类即可,其他有创建到Pizza对象的代码就不需要修改了----》简单工厂模式
改进后的代码
package com.fan.factory.simplefactory.pizzastore.order;
//相当于一个客户端,发出订购
public class PizzaStore {
public static void main(String[] args) {
new OrderPizza(new SimpleFactory());
System.out.println("~~退出程序~~");
}
}
package com.fan.factory.simplefactory.pizzastore.order;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.fan.factory.simplefactory.pizzastore.pizza.Pizza;
public class OrderPizza {
//构造器
public OrderPizza(SimpleFactory simpleFactory){
setFactory(simpleFactory);
}
//定义一个简单工厂对象
SimpleFactory simpleFactory;
Pizza pizza =null;
public void setFactory(SimpleFactory simpleFactory){
String orderType = "";
this.simpleFactory = simpleFactory; //设置简单工厂对象
do{
orderType = getType(); //用户输入的
pizza = this.simpleFactory.createPizza(orderType);
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) {
return "";
}
}
}
package com.fan.factory.simplefactory.pizzastore.order;
import com.fan.factory.simplefactory.pizzastore.pizza.CheesePizza;
import com.fan.factory.simplefactory.pizzastore.pizza.GreekPizza;
import com.fan.factory.simplefactory.pizzastore.pizza.PepperPizza;
import com.fan.factory.simplefactory.pizzastore.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.fan.factory.simplefactory.pizzastore.pizza;
//将Pizza类做成抽象类
public abstract class Pizza {
protected String name; //名字
//准备原材料,不同的Pizza不一样,因此,我们做成抽象方法
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.fan.factory.simplefactory.pizzastore.pizza;
public class CheesePizza extends Pizza{
@Override
public void prepare() {
System.out.println("给制作奶酪披萨准备原材料~~");
}
}
package com.fan.factory.simplefactory.pizzastore.pizza;
public class CheesePizza extends Pizza{
@Override
public void prepare() {
System.out.println("给制作奶酪披萨准备原材料~~");
}
}
package com.fan.factory.simplefactory.pizzastore.pizza;
public class GreekPizza extends Pizza{
@Override
public void prepare() {
System.out.println("给希腊披萨准备材料~~");
}
}
package com.fan.factory.simplefactory.pizzastore.pizza;
public class PepperPizza extends Pizza{
@Override
public void prepare() {
System.out.println("正在为胡椒披萨准备原材料");
}
}