文章目录
- 外观模式
- 1.喝茶
- 2.战斗游戏
外观模式
1.喝茶
接口类:
复制代码
1
2
3
4
5
6package com.gsg.fightinggame; public interface Shape { void draw(); }
Tea类:
复制代码
1
2
3
4
5
6
7
8
9
10package com.gsg.siptea; public class Tea implements Shape { @Override public void draw() { System.out.println("买了一包茶叶"); } }
Water类:
复制代码
1
2
3
4
5
6
7
8
9
10package com.gsg.siptea; public class Water implements Shape { @Override public void draw() { System.out.println("烧了一壶水"); } }
Tool类:
复制代码
1
2
3
4
5
6
7
8
9
10package com.gsg.siptea; public class Drink implements Shape { @Override public void draw() { System.out.println("喝一杯好茶"); } }
调用类:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package com.gsg.siptea; public class ShapeMaker { private Shape Tea; private Shape Water; private Shape Drink; public ShapeMaker() { Tea = new Tea(); Water = new Water(); Drink = new Drink(); } public void drawTea(){ Tea.draw(); } public void drawWater(){ Water.draw(); } public void drawDrink(){ Drink.draw(); }}
测试类:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12package com.gsg.siptea; public class TeaDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); System.out.println("Kungfu tea店"); shapeMaker.drawTea(); shapeMaker.drawWater(); shapeMaker.drawDrink(); } }
2.战斗游戏
接口类:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package com.gsg.fightinggame; public interface Shape { void draw(); } Clothes类: package com.gsg.fightinggame; public class Clothes implements Shape { @Override public void draw() { System.out.println("穿上战袍"); } }
Weapons类:
复制代码
1
2
3
4
5
6
7
8
9
10package com.gsg.fightinggame; public class Weapons implements Shape { @Override public void draw() { System.out.println("拿上武器"); } }
Tool类:
复制代码
1
2
3
4
5
6
7
8
9
10package com.gsg.fightinggame; public class Tool implements Shape { @Override public void draw() { System.out.println("骑上战马"); } }
调用类:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37package com.gsg.fightinggame; public class ShapeMaker { private Shape Clothes; private Shape Weapons; private Shape Tool; public ShapeMaker() { Clothes = new Clothes(); Weapons = new Weapons(); Tool = new Tool(); } public void drawClothes(){ Clothes.draw(); } public void drawWeapons(){ Weapons.draw(); } public void drawTool(){ Tool.draw(); } } 测试类: package com.gsg.fightinggame; public class GameDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); System.out.println("替父从军 花木兰"); shapeMaker.drawClothes(); shapeMaker.drawWeapons(); shapeMaker.drawTool(); System.out.println("巾帼不让须眉 红颜更胜儿郎"); }}
最后
以上就是淡淡太阳最近收集整理的关于外观模式案例之喝茶与战斗游戏的全部内容,更多相关外观模式案例之喝茶与战斗游戏内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复