外观模式编程要求:喝茶是广东人的一种生活方式,一杯好茶需要以下五步:1、准备优质凉水;2、准备茶叶(菊花 or 铁观音 or 普洱 or 西湖龙井); 3、烧热水;4、清洗茶具;5、冲泡才能完成。欧阳锋、黄药师、洪七公三人点了各自不一样的茶叶种类就等着服务员泡茶喝了。
时间: 2023-12-28 11:49:24 浏览: 72
行业分类-设备装置-一种节能洗袜子机.zip
以下是外观模式编程的实现:
首先,我们需要定义一个泡茶的外观类 TeaFacade,该类需要包含茶的五个步骤。其中,准备优质凉水和清洗茶具可以放在构造方法中执行,而选择茶叶和烧热水可以在泡茶方法中执行。TeaFacade 的泡茶方法可以接收一个参数,表示客人选择的茶叶种类。根据茶叶种类的不同,调用相应的泡茶方法进行泡茶。
接下来,我们需要定义四个具体的泡茶类,分别对应四种茶叶:ChrysanthemumTea、TieGuanYinTea、PuErTea 和 LongjingTea。这些类需要实现泡茶的方法,并且调用相关的步骤完成泡茶过程。
最后,我们需要定义三个客人类,分别对应欧阳锋、黄药师和洪七公。这些客人类需要调用 TeaFacade 的泡茶方法,并传入自己选择的茶叶种类。
请参考以下代码实现:
```java
// 外观类,封装茶的五个步骤
public class TeaFacade {
private Water water;
private Tea tea;
private Kettle kettle;
private Teapot teapot;
// 构造方法,准备优质凉水和清洗茶具
public TeaFacade() {
water = new Water();
kettle = new Kettle();
teapot = new Teapot();
water.prepare();
kettle.boil();
teapot.clean();
}
// 泡茶方法,根据茶叶种类选择不同的泡茶方法
public void makeTea(String teaType) {
switch (teaType) {
case "菊花":
tea = new ChrysanthemumTea();
break;
case "铁观音":
tea = new TieGuanYinTea();
break;
case "普洱":
tea = new PuErTea();
break;
case "西湖龙井":
tea = new LongjingTea();
break;
default:
throw new IllegalArgumentException("无效的茶叶种类");
}
tea.brew(teapot, water);
System.out.println("泡好了一杯" + teaType + "茶");
}
}
// 泡茶接口,定义泡茶方法
public interface Tea {
void brew(Teapot teapot, Water water);
}
// 菊花茶类,实现泡茶接口
public class ChrysanthemumTea implements Tea {
@Override
public void brew(Teapot teapot, Water water) {
teapot.putTea(new Chrysanthemum());
teapot.putWater(water.getWater(100));
teapot.steep(3);
}
}
// 铁观音茶类,实现泡茶接口
public class TieGuanYinTea implements Tea {
@Override
public void brew(Teapot teapot, Water water) {
teapot.putTea(new TieGuanYin());
teapot.putWater(water.getWater(100));
teapot.steep(5);
}
}
// 普洱茶类,实现泡茶接口
public class PuErTea implements Tea {
@Override
public void brew(Teapot teapot, Water water) {
teapot.putTea(new PuEr());
teapot.putWater(water.getWater(100));
teapot.steep(7);
}
}
// 龙井茶类,实现泡茶接口
public class LongjingTea implements Tea {
@Override
public void brew(Teapot teapot, Water water) {
teapot.putTea(new Longjing());
teapot.putWater(water.getWater(100));
teapot.steep(2);
}
}
// 水类,提供 prepare 方法获取优质凉水
public class Water {
public void prepare() {
System.out.println("准备优质凉水");
}
public Water getWater(int ml) {
System.out.println("取出" + ml + "ml水");
return this;
}
}
// 热水壶类,提供 boil 方法烧开水
public class Kettle {
public void boil() {
System.out.println("烧热水");
}
}
// 茶壶类,提供 clean 方法清洗茶具,putTea 方法放入茶叶,putWater 方法放入水,steep 方法冲泡
public class Teapot {
public void clean() {
System.out.println("清洗茶具");
}
public void putTea(TeaLeaf teaLeaf) {
System.out.println("放入" + teaLeaf.getName() + "茶叶");
}
public void putWater(Water water) {
System.out.println("放入水");
}
public void steep(int seconds) {
System.out.println("冲泡" + seconds + "秒");
}
}
// 茶叶接口,定义 getName 方法获取茶叶名称
public interface TeaLeaf {
String getName();
}
// 菊花类,实现茶叶接口
public class Chrysanthemum implements TeaLeaf {
@Override
public String getName() {
return "菊花";
}
}
// 铁观音类,实现茶叶接口
public class TieGuanYin implements TeaLeaf {
@Override
public String getName() {
return "铁观音";
}
}
// 普洱类,实现茶叶接口
public class PuEr implements TeaLeaf {
@Override
public String getName() {
return "普洱";
}
}
// 龙井类,实现茶叶接口
public class Longjing implements TeaLeaf {
@Override
public String getName() {
return "西湖龙井";
}
}
// 客人类,调用 TeaFacade 的泡茶方法
public class Customer {
private TeaFacade teaFacade;
public Customer(TeaFacade teaFacade) {
this.teaFacade = teaFacade;
}
public void drink(String teaType) {
teaFacade.makeTea(teaType);
}
}
// 测试类
public class Test {
public static void main(String[] args) {
TeaFacade teaFacade = new TeaFacade();
Customer ouyangfeng = new Customer(teaFacade);
ouyangfeng.drink("菊花");
Customer huangyaoshi = new Customer(teaFacade);
huangyaoshi.drink("铁观音");
Customer hongqiugong = new Customer(teaFacade);
hongqiugong.drink("西湖龙井");
}
}
```
阅读全文