JAVA工厂设计模式——肯德基订餐

本文深入解析了抽象工厂模式,一种创建型设计模式,允许在不指定具体类的情况下创建一系列相关或依赖的对象。通过实例展示了如何在Java中实现该模式,包括创建抽象接口、具体产品类和工厂类,以及如何在客户端代码中使用这些组件。
摘要由CSDN通过智能技术生成

一 模式定义

  • 抽象工厂模式提供了一个接口,用于创建相关或者依赖对象的家族,而不需要指定具体实现类。
  • 抽象工厂模式允许客户使用抽象接口来创建一组相关的产品,客户类和工厂类分开,客户需要任何产品的时候,只需要向工厂请求即可,客户无须修改就可以获得新产品。

类图如下:

 

具体代码如下:

 

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);
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值