Java设计模式——工厂模式(简单工厂模式实例)

1、基本介绍

  • 简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式中最简单实用的模式
  • 简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为
  • 在软件开发中,当用到大量的创建某种、某类或某批对象时,就会使用到工厂模式。

2、使用简单工厂模式

Java设计模式——工厂模式(简单工厂模式实例)_第1张图片

抽象Pizza类

package com.weirdo.factory.simplefactory.pizzastore.pizza;

//将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;
	}
}

奶酪披萨准备原料

package com.weirdo.factory.simplefactory.pizzastore.pizza;

public class CheesePizza extends Pizza {
     

	@Override
	public void prepare() {
     
		// TODO Auto-generated method stub
		System.out.println(" 给奶酪披萨 准备原材料 ");
	}
}

希腊披萨准备原料

package com.weirdo.factory.simplefactory.pizzastore.pizza;

public class GreekPizza extends Pizza {
     

	@Override
	public void prepare() {
     
		// TODO Auto-generated method stub
		System.out.println(" 给希腊披萨 准备原材料 ");
	}
}

胡椒披萨准备原料

package com.weirdo.factory.simplefactory.pizzastore.pizza;

public class PepperPizza extends Pizza {
     

	@Override
	public void prepare() {
     
		// TODO Auto-generated method stub
		System.out.println(" 给胡椒披萨 准备原材料 ");
	}
}

简单工厂类

package com.weirdo.factory.simplefactory.pizzastore.order;

import com.weirdo.factory.simplefactory.pizzastore.pizza.CheesePizza;
import com.weirdo.factory.simplefactory.pizzastore.pizza.GreekPizza;
import com.weirdo.factory.simplefactory.pizzastore.pizza.PepperPizza;
import com.weirdo.factory.simplefactory.pizzastore.pizza.Pizza;

//简单工厂类
public class SimpleFactory {
     

	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.weirdo.factory.simplefactory.pizzastore.order;
import com.weirdo.factory.simplefactory.pizzastore.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderPizza {
     

	//定义一个简单工厂对象
	SimpleFactory simpleFactory;
	Pizza pizza = null;
	
	//构造器
	public OrderPizza(SimpleFactory simpleFactory) {
     
		setFactory(simpleFactory);
	}

	public void setFactory(SimpleFactory simpleFactory) {
     
		String orderType = ""; //订购披萨的类型
		
		this.simpleFactory = simpleFactory; //设置简单工厂对象
		
		do {
     
			orderType = getType(); 
			pizza = 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 (IOException e) {
     
			e.printStackTrace();
			return "";
		}
	}
}

Pizza店类(客户端)

package com.weirdo.factory.simplefactory.pizzastore.order;

//相当于一个客户端,发出订购
public class PizzaStore {
     

	public static void main(String[] args) {
     
		SimpleFactory simpleFactory = new SimpleFactory();
		new OrderPizza(simpleFactory);
	}
}

开始订购,输入披萨种类

Java设计模式——工厂模式(简单工厂模式实例)_第2张图片

简单工厂模式也叫静态工厂模式,在工厂类中新增createPizza2方法

	//简单工厂模式也叫静态工厂模式
	public static Pizza createPizza2(String orderType) {
     

		Pizza pizza = null;

		System.out.println("使用简单工厂模式2");
		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;
	}

新增订购披萨类2

package com.weirdo.factory.simplefactory.pizzastore.order;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.weirdo.factory.simplefactory.pizzastore.pizza.Pizza;

public class OrderPizza2 {
     

	Pizza pizza = null;
	String orderType = "";
	// 构造器
	public OrderPizza2() {
     
		
		do {
     
			orderType = getType();
			pizza = SimpleFactory.createPizza2(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 (IOException e) {
     
			e.printStackTrace();
			return "";
		}
	}
}

客户端再次订购披萨

package com.weirdo.factory.simplefactory.pizzastore.order;

//相当于一个客户端,发出订购
public class PizzaStore {
     
	public static void main(String[] args) {
     
		new OrderPizza2();
	}
}

Java设计模式——工厂模式(简单工厂模式实例)_第3张图片

你可能感兴趣的:(Java设计模式,java,设计模式,工厂模式,简单工厂模式)