ackage castle;
import java.util.HashMap;
public class Room {
private String description;
HashMap<String,Room> exits = new HashMap<String,Room>();
public Room(String description) {
this.description = description;
}
public void setExit(String dir,Room room) {
exits.put(dir, room);
}
public String getExitdesc() {
StringBuffer sb = new StringBuffer();
for(String dir :exits.keySet()) {
sb.append(dir);
sb.append(" ");
}
return sb.toString();
}
public Room getExit(String dir) {
return exits.get(dir);
}
public String toString() {
return ""+description;
}
}
package castle;
import java.util.Scanner;
public class Game {
private Room currentRoom;
public Game()
{
createRooms();
}
private void createRooms()
{
Room outside, lobby, pub, study, bedroom;
// 制造房间
outside = new Room("城堡外");
lobby = new Room("大堂");
pub = new Room("小酒吧");
study = new Room("书房");
bedroom = new Room("卧室");
// 初始化房间的出口
outside.setExit("east",lobby);
outside.setExit("south",study);
outside.setExit("west",pub);
lobby.setExit("east", outside);
study.setExit("north",outside);
study.setExit("south",bedroom);
bedroom.setExit("west",study);
pub.setExit("down",lobby);
currentRoom = outside; // 从城堡门外开始
}
private void printWelcome() {
System.out.println();
System.out.println("欢迎来到城堡!");
System.out.println("这是一个超级无聊的游戏。");
System.out.println("如果需要帮助,请输入 'help' 。");
System.out.println();
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
System.out.print(currentRoom.getExitdesc());
}
// 以下为用户命令
private void printHelp()
{
System.out.print("迷路了吗?你可以做的命令有:go bye help");
System.out.println("如:\tgo east");
}
private void goRoom(String direction)
{
Room nextRoom = null;
if(direction.equals("north")) {
nextRoom = currentRoom.getExit("north");
}
if(direction.equals("east")) {
nextRoom = currentRoom.getExit("east");
}
if(direction.equals("south")) {
nextRoom = currentRoom.getExit("south");
}
if(direction.equals("west")) {
nextRoom = currentRoom.getExit("west");
}
if (nextRoom == null) {
System.out.println("那里没有门!");
}
else {
currentRoom = nextRoom;
System.out.println("你在" + currentRoom);
System.out.print("出口有: ");
System.out.print(currentRoom.getExitdesc());
System.out.println();
}
}
//public void showPrompt(){
//
//
//
//
//
//
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Game game = new Game();
game.printWelcome();
while ( true ) {
String line = in.nextLine();
String[] words = line.split(" ");
if ( words[0].equals("help") ) {
game.printHelp();
} else if (words[0].equals("go") ) {
game.goRoom(words[1]);
} else if ( words[0].equals("bye") ) {
break;
}
}
System.out.println("感谢您的光临。再见!");
in.close();
}
}
消除代码复制
- 代码复制是不良设计的一种表现
封装
适用于将来的需要(可以在将来使用)
- 可以运行的代码!=良好的代码
- 对代码做维护的时候最能看出代码的质量
- 如果想要增加一个方向,如down或up
用封装来降低耦合
- Room类和Game类都有大量的代码和出口有相关
- 尤其是Game类中大量的使用了Room类的成员变量
- 类和类之间的关系称作耦合
- 耦合越低越好,保持距离是形成良好的代码的关键
可扩展性
用借口来实现聚合
-
给Room类实现的新方法,吧方向的细节彻底隐藏在Room类内部了
-
今后的方向如何实现就和外部无关的了
-
Room 的方向是用成员变量表示的,增加或减少方向就要改变代码
-
如果用Hash表来表示,那么方向就不是“硬编码”的了
框架加数据
以框架+数据来提高可扩展性
- 命令的解析是否可以脱离if-else
- 定义了一个handler 来处理命令
- 用hash表来保存命令和Handler之间的关系
网友评论