工厂方法模式实验(2学时)
实验目的
- 理解简单工厂、工厂方法模式的概念
- 掌握工厂方法模式的用法
实验工具
- 计算机,安装JDK
- vscode或Eclipse
实验题目
- 参考给定制作咖啡的简单工厂例子,将其改成工厂方法模式实现
实验步骤
-
保留例子中的
Coffee
抽象类、LatteCoffee
和EspressoCoffee
类不变; -
将原来的
CoffeeStore
变成抽象类,并去掉其中的SimpleCoffeeFactory
相关信息,加入抽象的工厂方法用于构造各种咖啡:protected abstract Coffee createCoffee(String type);
-
添加两个类
SweetCoffeeStore
和BlackCoffeeStore
均扩展自CoffeeStore
抽象类,并分别实现createCoffee
方法,使得构造出的咖啡分别为甜味和苦味(因此需要新建几个咖啡类,分别表示甜味和苦味的Latte
和Espresso
咖啡),内容可参考SimpleCoffeeFactory
类中的方法; -
测试用的主函数可采用如下函数:
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; } }
-
相关类的类图:
- 实验结果:
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.