一 模式定义
- 抽象工厂模式提供了一个接口,用于创建相关或者依赖对象的家族,而不需要指定具体实现类。
- 抽象工厂模式允许客户使用抽象接口来创建一组相关的产品,客户类和工厂类分开,客户需要任何产品的时候,只需要向工厂请求即可,客户无须修改就可以获得新产品。
类图如下:
具体代码如下:
package com.work4;
import java.io.IOException;
/*
* 餐厅借口
*
*/
interface Restaurant{
public void getMessage(); //订餐信息
public void change(float paymoney); //找零
public float totlePrice(); //合计价格
public void discount(); //折扣
}
/*
* 基础套餐基类
*/
class Basic_Setmeal implements Restaurant{
public float price; //套餐价格
public int num; //订购数目
public String kind; //套餐种类
@Override
public void getMessage() {
System.out.println("您点的套餐为"+this.kind+"套餐,"+"数量为"+this.num+",总共消费"+this.totlePrice()+"元");
}
@Override
public void change(float paymoney) {
System.out.println("顾客付款"+paymoney+",找零"+(paymoney-this.totlePrice())+"元");
}
@Override
public float totlePrice() {
return this.num*this.price;
}
@Override
public void discount() {
System.out.println("是否使用优惠劵\n输入y/n\n");
char s;
try {
s = (char)System.in.read();
if(s == 'y'){
this.price = this.price*0.8f;
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("折后价格为"+this.totlePrice());
}
}
/*
* 普通套餐基类
*/
class Normal_Setmeal implements Restaurant{
public float price;
public int num;
public String kind;
@Override
public void getMessage() {
System.out.println("您点的套餐为"+this.kind+"套餐,"+"数量为"+this.num+",总共消费"+this.totlePrice()+"元");
}
@Override
public void change(float paymoney) {
System.out.println("顾客付款"+paymoney+",找零"+(paymoney-this.totlePrice())+"元");
}
@Override
public float totlePrice() {
return this.num*this.price;
}
@Override
public void discount() {
System.out.println("是否使用优惠劵\n输入y/n\n");
char s;
try {
s = (char)System.in.read();
if(s == 'y'){
this.price = this.price*0.8f;
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("折后价格为"+this.totlePrice());
}
}
class Basic_SetmealA extends Basic_Setmeal{
public Basic_SetmealA(int num){
this.price = 25.5f;
this.kind = "BasicA";
this.num = num;
}
}
class Basic_SetmealB extends Basic_Setmeal{
public Basic_SetmealB(int num){
this.price = 20f;
this.kind = "BasicB";
this.num = num;
}
}
class Normal_SetmealA extends Normal_Setmeal{
public Normal_SetmealA(int num){
this.price = 42.5f;
this.kind = "NormalA";
this.num = num;
}
}
class Normal_SetmealB extends Normal_Setmeal{
public Normal_SetmealB(int num){
this.price = 45.5f;
this.kind = "NormalB";
this.num = num;
}
}
package com.work4;
/*
* 肯德基工厂接口
*/
interface Factory{
public Basic_Setmeal create_Basic_Setmeal(int num,String type); //生产基础套餐
public Normal_Setmeal create_Normal_Setmeal(int num,String type); //生产普通套餐
}
/*
* 肯德基工厂
*/
class KFC_Factory implements Factory{
@Override
public Basic_Setmeal create_Basic_Setmeal(int num,String type) {
switch(type){
case "A": return new Basic_SetmealA(num);
case "B": return new Basic_SetmealB(num);
default : return null;
}
}
@Override
public Normal_Setmeal create_Normal_Setmeal(int num,String type) {
switch(type){
case "A": return new Normal_SetmealA(num);
case "B": return new Normal_SetmealB(num);
default : return null;
}
}
}
package com.work4;
/*
* 客户类
*/
public class Customer {
private Factory IFactory; //抽象工厂
private float paymoney; //客户支付的金额
public Customer(Factory IFactory){
this.IFactory = IFactory;
}
//订购基础套餐
public void getBasic_Setmeal(int num,String type,float paymoney){
Basic_Setmeal basic = IFactory.create_Basic_Setmeal(num, type);
basic.getMessage();
basic.discount();
basic.change(paymoney);
}
//订购普通套餐
public void getNormal_Setmeal(int num,String type,float paymoney){
Normal_Setmeal normal = IFactory.create_Normal_Setmeal(num, type);
normal.getMessage();
normal.discount();
normal.change(paymoney);
}
}
package com.work4;
public class Main {
public static void main(String[] args){
Factory IFactory = new KFC_Factory();
Customer customer = new Customer(IFactory);
customer.getBasic_Setmeal(2, "A",100);
}
}