概述
文章目录
- 外观模式
- 1.喝茶
- 2.战斗游戏
外观模式
1.喝茶
接口类:
package com.gsg.fightinggame;
public interface Shape {
void draw();
}
Tea类:
package com.gsg.siptea;
public class Tea implements Shape {
@Override
public void draw() {
System.out.println("买了一包茶叶");
}
}
Water类:
package com.gsg.siptea;
public class Water implements Shape {
@Override
public void draw() {
System.out.println("烧了一壶水");
}
}
Tool类:
package com.gsg.siptea;
public class Drink implements Shape {
@Override
public void draw() {
System.out.println("喝一杯好茶");
}
}
调用类:
package 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();
}}
测试类:
package 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.战斗游戏
接口类:
package 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类:
package com.gsg.fightinggame;
public class Weapons implements Shape {
@Override
public void draw() {
System.out.println("拿上武器");
}
}
Tool类:
package com.gsg.fightinggame;
public class Tool implements Shape {
@Override
public void draw() {
System.out.println("骑上战马");
}
}
调用类:
package 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("巾帼不让须眉 红颜更胜儿郎");
}}
最后
以上就是淡淡太阳为你收集整理的外观模式案例之喝茶与战斗游戏的全部内容,希望文章能够帮你解决外观模式案例之喝茶与战斗游戏所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复