从java城堡游戏 学习设计原则
概述
学习java语言已经有三天时间了,因为此前有C/C++语言的基础,对面向对象这些概念还比较清楚,学习java基础要轻松一点。虽然是刚刚接触,但是能明显感受到java语言写起来要比C++轻松不少,而且语言的生态也明显更好。
这次的学习案例来自翁恺老师的MOOC面向对象程序设计——java语言,B站也有这套课程的完整视频,传送门。本次要讨论的主题是设计原则,这个主题很大,不过用来说明问题的小程序的代码量并不大,是精心设计过的,认真学完还是会有很大收获。
城堡游戏v1.0
案例是一个文字版的城堡游戏,有Room和Game两个类组成,跑起来没有任何问题,简单运行一下就可以了解程序的功能。但是能运行的代码与一份高质量的代码还有这很大的差距,先阅读一下源码,并找出你认为存在问题的地方吧。
//Room.java:
package castle;
public class Room {
public String description;
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
public Room(String description)
{
this.description = description;
}
public void setExits(Room north, Room east, Room south, Room west)
{
if(north != null)
northExit = north;
if(east != null)
eastExit = east;
if(south != null)
southExit = south;
if(west != null)
westExit = west;
}
@Override
public String toString()
{
return description;
}
}
//Game.java
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.setExits(null, lobby, study, pub);
lobby.setExits(null, null, null, outside);
pub.setExits(null, outside, null, null);
study.setExits(outside, bedroom, null, null);
bedroom.setExits(null, null, null, study);
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("出口有:");
if(currentRoom.northExit != null)
System.out.print("north ");
if(currentRoom.eastExit != null)
System.out.print("east ");
if(currentRoom.southExit != null)
System.out.print("south ");
if(currentRoom.westExit != null)
System.out.print("west ");
System.out.println();
}
// 以下为用户命令
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.northExit;
}
if(direction.equals("east")) {
nextRoom = currentRoom.eastExit;
}
if(direction.equals("south")) {
nextRoom = currentRoom.southExit;
}
if(direction.equals("west")) {
nextRoom = currentRoom.westExit;
}
if (nextRoom == null) {
System.out.println("那里没有门!");
}
else {
currentRoom = nextRoom;
System.out.println("你在" + currentRoom);
System.out.print("出口有: ");
if(currentRoom.northExit != null)
System.out.print("north ");
if(currentRoom.eastExit != null)
System.out.print("east ");
if(currentRoom.southExit != null)
System.out.print("south ");
if(currentRoom.westExit != null)
System.out.print("west ");
System.out.println();
}
}
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();
}
}
v1.0中存在的代码设计问题
首先比较明显的问题是代码复制,也就是一段重复的代码反复出现。例如,在Game类中的printWelcome和goRoom这两个方法中都出现了如下代码:
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
if(currentRoom.northExit != null)
System.out.print("north ");
if(currentRoom.eastExit != null)
System.out.print("east ");
if(currentRoom.southExit != null)
System.out.print("south ");
if(currentRoom.westExit != null)
System.out.print("west ");
System.out.println();
因此可以单独写一个showPrompt方法来解决代码复制。
private void showPrompt() {
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
if(currentRoom.northExit != null)
System.out.print("north ");
if(currentRoom.eastExit != null)
System.out.print("east ");
if(currentRoom.southExit != null)
System.out.print("south ");
if(currentRoom.westExit != null)
System.out.print("west ");
System.out.println();
}
第二个问题是类之间的耦合度太高。看一下Room中的成员变量就可以发现这个问题:
public String description;
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
Room这个类的成员变量完全都是public,直接暴露在外面被反复使用,这是一种非常糟糕的设计。解决办法是把成员变量改为private,同时定义好接口,供外部使用。
其中一种思路是对外提供如下的public方法,比如:
public Room getNorthExit() {
return NorthExit;
}
这种方法虽然看似解决了问题,将成员变量从public变成了private,并定义了public方法获取成员变量,但是这是一种无可奈何的设计,因为没有从根本上起到解耦的作用,两个类的设计依旧是紧紧绑在一起,我们应该设计更有意义的接口。
我们从Game中对Room这个类的使用来看看都需要哪些接口:
一个是在我们上面重新写的showPrompt方法中:
private void showPrompt() {
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
if(currentRoom.northExit != null)
System.out.print("north ");
if(currentRoom.eastExit != null)
System.out.print("east ");
if(currentRoom.southExit != null)
System.out.print("south ");
if(currentRoom.westExit != null)
System.out.print("west ");
System.out.println();
}
另一处在goRoom方法中:
private void goRoom(String direction)
{
Room nextRoom = null;
if(direction.equals("north")) {
nextRoom = currentRoom.northExit;
}
if(direction.equals("east")) {
nextRoom = currentRoom.eastExit;
}
if(direction.equals("south")) {
nextRoom = currentRoom.southExit;
}
if(direction.equals("west")) {
nextRoom = currentRoom.westExit;
}
if (nextRoom == null) {
System.out.println("那里没有门!");
}
else {
currentRoom = nextRoom;
System.out.println("你在" + currentRoom);
System.out.print("出口有: ");
if(currentRoom.northExit != null)
System.out.print("north ");
if(currentRoom.eastExit != null)
System.out.print("east ");
if(currentRoom.southExit != null)
System.out.print("south ");
if(currentRoom.westExit != null)
System.out.print("west ");
System.out.println();
}
}
因此,我们应该重新在Room中定义两个方法,把上面两个功能实现一遍,这样在Game类中就可以直接调用Room提供的方法,而不是直接操作Room的成员变量,起到真正的解耦的作用。因此,在Room类中增加两个方法:
public String getExitDesc() {
StringBuffer beBuffer = new StringBuffer();
for(String dir : exits.keySet()) {
beBuffer.append(dir);
beBuffer.append(' ');
}
return beBuffer.toString();
}
这里值得一提的是,如果需要对字符串进行反复修改,可以使用StringBuffer,这样效率更高。如果是使用String,然后在通过“+”去反复做字符串连接,开销会很大。因为String是不会修改字符串的,每次做连接运算都是重新申请了一个新的字符串。
public Room goNext(String direction) {
Room nextRoom = null;
if(direction.equals("north")) {
nextRoom = northExit;
}
if(direction.equals("east")) {
nextRoom = eastExit;
}
if(direction.equals("south")) {
nextRoom = southExit;
}
if(direction.equals("west")) {
nextRoom = westExit;
}
return nextRoom;
}
这样上面提到的Game中的showPrompt和goRoom两个方法就可以轻松使用Room提供的接口了:
private void showPrompt() {
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
System.out.println(currentRoom.getExitDesc());
}
public void goRoom(String direction)
{
Room nextRoom = currentRoom.goNext(direction);
if (nextRoom == null) {
System.out.println("那里没有门!");
}
else {
currentRoom = nextRoom;
showPrompt();
}
}
到此为止,我们已经实现了Room类和Game类的解耦。
城堡游戏v2.0
解耦为我们的代码的扩展性提供了基础,考虑一下如果现在我们想给城堡的房间添加更多的出口,该如何实现呢。先来看看我们目前的实现方式:
public class Room {
private String description;
private Room northExit;
private Room southExit;
private Room eastExit;
private Room westExit;
}
也就是说,每个房间的出口是通过硬编码的方式写死的,如果想添加更多的出口,就意味着定义更多的成员变量。如果希望程序能有更好的可扩展性,就不能使用硬编码,而是使用容器来进行统一管理。这样将来扩展的时候,将扩展的内容直接加入到容器里统一处理就可以了,其他代码都无需修改。
这里使用HashMap来将方向与房间对应起来,将Room的成员变量修改为:
public class Room {
private String description;
private HashMap<String, Room> exits = new HashMap<String,Room>();
}
当然相应的方法也要进行修改,不过修改后更简洁:
public Room goNext(String direction) {
return exits.get(direction);
}
public void setExit(String dir,Room room) {
exits.put(dir, room);
}
public String getExitDesc() {
StringBuffer beBuffer = new StringBuffer();
for(String dir : exits.keySet()) {
beBuffer.append(dir);
beBuffer.append(' ');
}
return beBuffer.toString();
}
到此我们已经实现了对于房间出口的扩展,现在如果想为房间添加除了东南西北以外的出口,比如向上或者向下,直接在Game的初始化代码中加一行代码即可:
bedroom.setExit("up", study);//卧室向上走是书房
study.setExit("down", bedroom);//书房向下走是卧室
城堡游戏v3.0
到这里,我们已经实现了对房间出口的扩展,如果我们还想实现对命令的扩展呢?同样,我们先来看看当前的实现:
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;
}
}
依然是采用了一种硬编码的方式,分析出玩家的指令,然后调用相应的方法来处理。如果要实现对指令的扩展,我们依然不能使用硬编码,而是寻找一种统一的管理方法。在这里,我们可以继续使用HashMap,但是我们需要注意的是,HashMap只能表示对象之间的对应关系,是没法表示字符串到函数这种对应关系的。因此,我们可以为不同的指令都创建一个Handler对象,用HashMap保存这种对应关系,遇到不同的指令,就调用对应的Handler中的方法来解决问题,其实是多态的一种体现。
因此,Game中要增加成员变量:
private HashMap<String, Handler> handlers = new HashMap<String, Handler>();
同时增加每条指令对应的Handler。
最终代码
//Game.java
package castle;
import java.util.HashMap;
import java.util.Scanner;
public class Game {
private Room currentRoom;
private HashMap<String, Handler> handlers = new HashMap<String, Handler>();
public Game()
{
handlers.put("go", new HandlerGo(this));
handlers.put("bye", new HandlerBye(this));
handlers.put("help", new HandlerHelp(this));
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("west", outside);
pub.setExit("east", outside);
study.setExit("north",outside);
study.setExit("east",bedroom);
bedroom.setExit("west", study);
bedroom.setExit("up", study);
study.setExit("down", bedroom);
currentRoom = outside; // 从城堡门外开始
}
private void printWelcome() {
System.out.println();
System.out.println("欢迎来到城堡!");
System.out.println("这是一个超级无聊的游戏。");
System.out.println("如果需要帮助,请输入 'help' 。");
System.out.println();
showPrompt();
}
private void showPrompt() {
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
System.out.println(currentRoom.getExitDesc());
}
// 以下为用户命令
public void goRoom(String direction)
{
Room nextRoom = currentRoom.goNext(direction);
if (nextRoom == null) {
System.out.println("那里没有门!");
}
else {
currentRoom = nextRoom;
showPrompt();
}
}
public void play() {
Scanner in = new Scanner(System.in);
while ( true ) {
String line = in.nextLine();
String[] words = line.split(" ");
Handler handler = handlers.get(words[0]);
String value = "";
if( words.length > 1) {
value = words[1];
}
if(handler != null) {
handler.doCmd(value);
if(handler.isBye()) {
break;
}
}
// if ( words[0].equals("help") ) {
// printHelp();
// } else if (words[0].equals("go") ) {
// goRoom(words[1]);
// } else if ( words[0].equals("bye") ) {
// break;
// }
}
System.out.println("感谢您的光临。再见!");
in.close();
}
public static void main(String[] args) {
Game game = new Game();
game.printWelcome();
game.play();
}
}
//Room.java
package castle;
import java.util.HashMap;
public class Room {
private String description;
private HashMap<String, Room> exits = new HashMap<String,Room>();
public Room(String description)
{
this.description = description;
}
public String getExitDesc() {
StringBuffer beBuffer = new StringBuffer();
for(String dir : exits.keySet()) {
beBuffer.append(dir);
beBuffer.append(' ');
}
return beBuffer.toString();
}
public Room goNext(String direction) {
return exits.get(direction);
}
public void setExit(String dir,Room room) {
exits.put(dir, room);
}
@Override
public String toString()
{
return description;
}
}
//Handler.java
package castle;
public class Handler {
protected Game game;
public Handler(Game game) {
this.game = game;
}
public void doCmd(String word) {
}
public boolean isBye() {
return false;
}
}
//HandlerBye.java
package castle;
public class HandlerBye extends Handler{
public HandlerBye(Game game) {
super(game);
}
@Override
public boolean isBye() {
// TODO Auto-generated method stub
return true;
}
}
//HandlerHelp.java
package castle;
public class HandlerHelp extends Handler{
public HandlerHelp(Game game) {
super(game);
}
@Override
public void doCmd(String word) {
// TODO Auto-generated method stub
System.out.println("迷路了吗?你可以做的命令有:go bye help");
System.out.println("如:\tgo east");
}
}
//HandlerGo.java
package castle;
public class HandlerGo extends Handler {
public HandlerGo(Game game) {
super(game);
}
@Override
public void doCmd(String word) {
// TODO Auto-generated method stub
game.goRoom(word);
}
}
上一篇: 弹性布局(伸缩布局)的使用教程
下一篇: 单例设计模式