Java设计原则1—城堡游戏之消除代码复制
2017-01-18 11:18
417 查看
首先看一下城堡游戏的源码
华丽分割线————————————————————————————————–
以上为城堡游戏的源码。是否发现了一些设计问题?
首先,会发现下面这段代码在Game类中出现了两次,无疑这会使代码看起来并不优雅,重要的是:在程序中存在相似甚至相同的代码块,是非常低级的代码质量问题。
代码复制存在的问题是,如果需要修改一个副本,那么就必须同时修改所有其他的副本,否则就 存在不一致的问题。这增加了维护程序员的工作量,而且存在造成错误的潜在危险。很可能发 生的一种情况是,维护程序员看到一个副本被修改好了,就以为所有要修改的地方都已经改好 了。因为没有任何明显迹象可以表明另外还有一份一样的副本代码存在,所以很可能会遗漏还 没被修改的地方。
我们从消除代码复制开始。消除代码复制的两个基本手段,就是函数和父类。
这里用函数来解决城堡游戏中重复代码的问题:
删除重复的代码,在重复的地方调用showPrompt() 函数即可。
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; } }
华丽分割线————————————————————————————————–
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(); } }
以上为城堡游戏的源码。是否发现了一些设计问题?
首先,会发现下面这段代码在Game类中出现了两次,无疑这会使代码看起来并不优雅,重要的是:在程序中存在相似甚至相同的代码块,是非常低级的代码质量问题。
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 ");
代码复制存在的问题是,如果需要修改一个副本,那么就必须同时修改所有其他的副本,否则就 存在不一致的问题。这增加了维护程序员的工作量,而且存在造成错误的潜在危险。很可能发 生的一种情况是,维护程序员看到一个副本被修改好了,就以为所有要修改的地方都已经改好 了。因为没有任何明显迹象可以表明另外还有一份一样的副本代码存在,所以很可能会遗漏还 没被修改的地方。
我们从消除代码复制开始。消除代码复制的两个基本手段,就是函数和父类。
这里用函数来解决城堡游戏中重复代码的问题:
public 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 ");
}
删除重复的代码,在重复的地方调用showPrompt() 函数即可。
相关文章推荐
- 18.3.7 第5周 设计原则 5.1城堡游戏+5.2消除代码赋值+5.3封装
- Java设计原则2—城堡游戏之封装
- Java设计原则3—城堡游戏之可扩展性
- 游戏与代码设计(Java)目录
- Java 吃金币游戏设计与制作,下载版后补,代码没问题
- Java中2.5D游戏的设计与实现(3)—八方走法实现原理及相关代码
- Java2.5D游戏的设计与实现(3)—八方走法实现原理及相关代码 推荐
- 对JAVA代码设计六大原则的理解
- JAVA进阶 面向对象程序设计——第5周 设计原则(耦合和聚合,代码结构设计,程序维护拓展发展)
- Java中2.5D游戏的设计与实现(3)—八方走法实现原理及相关代码
- Java中2.5D游戏的设计与实现(3)—八方走法实现原理及相关代码
- Android与Java代码设计六大原则
- 跟我学Java Swing之游戏设计(2)
- 游戏代码部分-------game包------KeyHandler.java
- Java中2.5D游戏(斜45度角)的设计与实现(1)
- JAVA与模式--设计原则笔记
- JAVA与模式--设计原则笔记
- java小游戏基础设计参考代码
- javabean+jsp猜游戏的代码
- [代码]Deflexion游戏,还有设计文档