设计模式之工厂方法模式

实验通过实现工厂方法模式改进了简单的咖啡店系统,创建了SweetCoffeeStore和BlackCoffeeStore,分别用于制作甜味和苦味的咖啡。系统能够根据顾客订单动态决定制作哪种类型的咖啡,如SweetEspressoCoffee和BlackLatteCoffee。实验结果展示了不同咖啡店如何制作不同口味的咖啡。
摘要由CSDN通过智能技术生成

工厂方法模式实验(2学时)

实验目的

  • 理解简单工厂、工厂方法模式的概念
  • 掌握工厂方法模式的用法

实验工具

  • 计算机,安装JDK
  • vscode或Eclipse

实验题目

  • 参考给定制作咖啡的简单工厂例子,将其改成工厂方法模式实现

实验步骤

  1. 保留例子中的Coffee抽象类、LatteCoffeeEspressoCoffee类不变;

  2. 将原来的CoffeeStore变成抽象类,并去掉其中的SimpleCoffeeFactory相关信息,加入抽象的工厂方法用于构造各种咖啡:

    protected abstract Coffee createCoffee(String type);
    
  3. 添加两个类SweetCoffeeStoreBlackCoffeeStore均扩展自CoffeeStore抽象类,并分别实现createCoffee方法,使得构造出的咖啡分别为甜味和苦味(因此需要新建几个咖啡类,分别表示甜味和苦味的LatteEspresso咖啡),内容可参考SimpleCoffeeFactory类中的方法;

  4. 测试用的主函数可采用如下函数:

    public static void main(String[] args) {
        // TODOAuto-generated method stub
        CoffeeStore sweetStore= new SweetCoffeeStore();
        CoffeeStore blackStore= new BlackCoffeeStore();
        Coffee coffee= null;
        coffee = sweetStore.orderCoffee("espresso");
        System.out.println("Iordered a cup of " + coffee.getName() + " coffee");
        System.out.println(coffee);
        System.out.println();
        coffee = sweetStore.orderCoffee("latte");
        System.out.println("Uordered a cup of " + coffee.getName() + " coffee");
        System.out.println(coffee);
        System.out.println();
        coffee = blackStore.orderCoffee("espresso");
        System.out.println("Iordered a cup of " + coffee.getName() + " coffee");
        System.out.println(coffee);
        System.out.println();
        coffee = blackStore.orderCoffee("latte");
        System.out.println("Uordered a cup of " + coffee.getName() + " coffee");
        System.out.println(coffee);
        System.out.println();
    }
    

实验内容

  • 关键类的源程序清单:

    // CoffeeStore.java(变成抽象类)
     public abstract class CoffeeStore {
     protected abstract Coffee createCoffee(String type);
     public Coffee orderCoffee(String type) {  
         Coffee coffee=createCoffee(type);
         coffee.prepare();
         coffee.brew();
         coffee.pour();
         coffee.addSugar();
         return coffee;
         }
     }
    // SweetCoffeeStore.java
    public class SweetCoffeeStore  extends CoffeeStore{
      @Override
      protected Coffee createCoffee(String type) {
      	// TODO 自动生成的方法存根		
      switch (type) {
      case "latte":
        return new SweetLatteCoffee();
      	case "espresso":
         return new SweetEspressoCoffee();
      	default:
             return new NullCoffee();
               }
      	 }
      	 }
    }
    // BlackCoffeeStore.java
    public class BlackCoffeeStore extends CoffeeStore{
      @Override
      protected Coffee createCoffee(String type) {
      	// TODO 自动生成的方法存根
      	 switch (type) {
           case "latte":
               return new BlackLatteCoffee();
      	case "espresso":
          	 return new BlackEspressoCoffee();
      	default:
               return new NullCoffee();
               }
      	 }
         }  
         //SweetEspressoCoffee.java
    public class SweetEspressoCoffee extends Coffee {
      SweetEspressoCoffee(){
        name = "SweetEspresso";
        hasSugar =false;
        }
        }
        //SweetLatteCoffee.java
    public class SweetLatteCoffee extends Coffee {
    SweetLatteCoffee(){
      name = "SweetLatte";
      hasSugar =true;
      }
      }
      //BlackEspressoCoffee.java
      public class BlackEspressoCoffee extends Coffee {
        BlackEspressoCoffee(){
      name = "BlackEspresso";
      hasSugar =false;
       }
      }
      //BlackLatteCoffee.java
      public class BlackLatteCoffee extends Coffee {
      BlackLatteCoffee(){
      name = "BlackLatte";
      hasSugar =true;
      }
      }    
    
    
  • 相关类的类图:

«abstract» CoffeeStore +abstract createCoffee +orderCoffee(String type) +quack() SweetCoffeeStore +createCoffee(String type) BlackCoffeeStore +createCoffee(String type) SweetLatteCoffee +SweetLatteCoffee() +addSugar() SweetEspressoCoffee +SweetEspressoCoffee() +addSugar() BlackEspressoCoffee +BlackEspressoCoffee() +addSugar() +BlackLatteCoffee() +addSugar() BlackLatteCoffee «abstract» coffee +String name +boolean hasSugar +getName() +prepare() +brew() +pour() +addSugar() +toString() NullCoffee +NullCoffee()
  • 实验结果:
Preparing coffee beans...
Brewing coffee...
Pouring coffee into the cup....
Adding sugar...
Iordered a cup of SweetEspresso coffeeffee
---- SweetEspresso ----
without sugar.


Preparing coffee beans...
Brewing coffee...
Pouring coffee into the cup....
Adding sugar...
Uordered a cup of SweetLatte coffeee
---- SweetLatte ----
with sugar.


Preparing coffee beans...
Brewing coffee...
Pouring coffee into the cup....
Adding sugar...
Iordered a cup of BlackEspresso coffeeffee
---- BlackEspresso ----
without sugar.


Preparing coffee beans...
Brewing coffee...
Pouring coffee into the cup....
Adding sugar...
Uordered a cup of BlackLatte coffeee
---- BlackLatte ----
with sugar.
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值