模式定义:
1.简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
2.抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。根据里氏替换原则,任何接受父类型的地方,都应当能够接受子类型。因此,实际上系统所需要的,仅仅是类型与这些抽象产品角色相同的一些实例,而不是这些抽象产品的实例。换言之,也就是这些抽象产品的具体子类的实例。工厂类负责创建抽象产品的具体子类的实例。
KFC点餐系统:在本程序中,我采用了简单工厂模式和抽象工厂模式,简单工厂模式用来实现单点食物,抽象工厂模式用来实现点套餐。主要的功能有点餐,使用优惠,打印小票。
主要的类有Food(食物基类),Staple(主食),Drink(饮料),Chips(薯条),Hanburger(汉堡),Chicken(炸鸡),Coffee(咖啡),Coke(可乐),Juice(果汁),一个简单工厂类(SinpleFactory)和一个抽象工厂类(AbstractFactory)。
UML类图:
全部代码
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//食品基类
class Food{
public Integer food_id;
public String food_name;
public Double food_price;
}
//主食类
class Staple extends Food{
//食品重量
public float weight;
}
//饮料类
class Drink extends Food{
//饮料容量
public float capacity;
}
//汉堡类
class Hamburger extends Staple{
public Hamburger(){
this.food_id = 1;
this.food_name = "Hamburger";
this.food_price = 7.0;
this.weight = 20f;
}
}
//薯条类
class Chips extends Staple{
public Chips(){
this.food_id = 2;
this.food_name = "Chips";
this.food_price = 8.0;
this.weight = 10f;
}
}
//炸鸡类
class Chicken extends Staple{
public Chicken(){
this.food_id = 3;
this.food_name = "Chicken";
this.food_price = 12.0;
this.weight = 40f;
}
}
//可乐类
class Coke extends Drink{
public Coke(){
this.food_id = 4;
this.food_name = "Coke";
this.food_price = 4.0;
this.capacity = 200f;
}
}
//咖啡类
class Coffee extends Drink{
public Coffee(){
this.food_id = 5;
this.food_name = "Coffee";
this.food_price = 5.0;
this.capacity = 200f;
}
}
//果汁类
class Juice extends Drink{
public Juice(){
this.food_id = 6;
this.food_name = "Juice";
this.food_price = 6.0;
this.capacity = 200f;
}
}
//简单工厂模式
class SinpleFactory{
public Food getFood(Integer id){
Food fd = new Food();
switch (id){
case 1:
fd = new Hamburger();
break;
case 2:
fd = new Chips();
break;
case 3:
fd = new Chicken();
break;
case 4:
fd = new Coke();
break;
case 5:
fd = new Coffee();
break;
case 6:
fd = new Juice();
break;
default:
System.out.println("输入错误");
}
return fd;
}
}
interface AbstractFactory{
List<Food> getCombo();
}
//套餐一
class Combo_1 implements AbstractFactory{
@Override
public List<Food> getCombo() {
List<Food> foodOne = new ArrayList<Food>();
Food ham = new Hamburger();
Food juc = new Juice();
//套餐内优惠
ham.food_price = ham.food_price - 2;
juc.food_price = juc.food_price - 1;
foodOne.add(ham);
foodOne.add(juc);
return foodOne;
}
}
//套餐二
class Combo_2 implements AbstractFactory{
@Override
public List<Food> getCombo() {
List<Food> foodTwo = new ArrayList<Food>();
Food chi = new Chips();
Food juc = new Juice();
//套餐内优惠
chi.food_price = chi.food_price - 2;
juc.food_price = juc.food_price - 1;
foodTwo.add(chi);
foodTwo.add(juc);
return foodTwo;
}
}
public class KFC {
public static void main(String[] argc) throws Exception{
System.out.println("请输入食物编号点餐或者套餐号(输入0结束):");
System.out.println("食品:1.汉堡 2.薯条 3.炸鸡");
System.out.println("饮料:4.可乐 5.咖啡 6.果汁");
System.out.println("或者选择套餐:7.汉堡加果汁 8.薯条加果汁");
double money1 = 0.0;
double money2 = 0.0;
int a = 1;
Scanner in = new Scanner(System.in);
List<Food> f1 = new ArrayList<Food>();
List<Food> f2 = new ArrayList<Food>();
while (true) {
a = in.nextInt();
if (a == 0) {
break;
}
if (a > 0 && a < 7) {
SinpleFactory sf = new SinpleFactory();
f1.add(sf.getFood(a));
} else if (a == 7) {
AbstractFactory c1 = new Combo_1();
f2.addAll(c1.getCombo());
} else if (a == 8) {
AbstractFactory c2 = new Combo_2();
f2.addAll(c2.getCombo());
} else {
System.out.println("输入错误");
}
}
//
//打印小票
File file = null;
FileWriter fw = null;
file = new File("小票.txt");
if(file.exists()){
file.delete();
}
if (!file.exists()){
file.createNewFile();
}
fw = new FileWriter(file,true);
//if (!f1.isEmpty()) {
for (int i = 0; i < f1.size(); i++) {
money1 += f1.get(i).food_price;
System.out.println(f1.get(i).food_name + ":" + f1.get(i).food_price);
/*if (!file.exists()){
file.createNewFile();
}*/
//fw = new FileWriter(file,true);
fw.write(f1.get(i).food_name+f1.get(i).food_price+"元"+"\r\n");
fw.flush();
}
//}
//if (!f2.isEmpty()) {
for (int i = 0; i < f2.size(); i++) {
money2 += f2.get(i).food_price;
System.out.println(f2.get(i).food_name + ":" + f2.get(i).food_price);
/*if (!file.exists()) {
file.createNewFile();
}*/
//fw = new FileWriter(file,true);
fw.write(f2.get(i).food_name+f2.get(i).food_price+"元"+"\r\n");
fw.flush();
}
//}
double sum = money1 + money2;
sum = discount(sum);
System.out.println("总计:" + sum);
fw.write("总计:"+sum);
fw.close();
}
//优惠方法
public static double discount ( double sum){
System.out.println("是否有优惠券:Y/N");
Scanner judge = new Scanner(System.in);
String s = judge.next();
int a = 0;
if (s.equals("Y") || s.equals("y")) {
System.out.println("请输入优惠的金额:");
Scanner discounts = new Scanner(System.in);
a = discounts.nextInt();
}
return sum -= a;
}
}
实现截图: