OOD - Object Oriented Design 面向对象设计实例
来源:互联网 发布:淘宝上企业店铺靠谱吗 编辑:程序博客网 时间:2024/05/19 02:16
OOD 面向对象设计有很大的几率出现在小公司的面试上,在youtube上找了一个很好的教程来和大家分享一下。
问题: OOD 思想设计两人投掷硬币的游戏。
步骤一: 准备documentation
游戏介绍:随机选择一个玩家,令该玩家预测硬币的正反面,另一个玩家获得和第一个玩家相反的选项。开始投掷硬币,预测正确的赢。
Actors: 1. 玩家A2. 玩家B 3. 硬币 4. 掷硬币游戏
根据以上信息我们可以画出object model. 这个决定我们要创建多少个实体类。
根据游戏规则以及流程,我们可以画出类图。
步骤二:代码实现
Player class.
public class Player {private String Name;private String CoinOption;public String[] coinValue = {"Heads","Tails"}; Player(String name){this.Name = name;}public String getCoinOption(){return this.CoinOption;}public void setCoinOption(String opponentFlip){this.CoinOption = (opponentFlip.equals("Heads"))?"Tails":"Heads";}public String getRandCoinOption(){int randNum = (Math.random()<0.5)?0:1;this.CoinOption = this.coinValue[randNum];return this.CoinOption;}public void didPlayerWin(String winningFlip){if(this.CoinOption.equals(winningFlip)){System.out.println(this.Name + " won with flip of " + this.CoinOption);}elseSystem.out.println(this.Name + " lost " + this.CoinOption);}}
Coin Class
public class Coin {private String coinOption = "";public String[] coinValue = {"Heads","Tails"};Coin(){int randNum = (Math.random() < 0.5)?0:1;this.coinOption = this.coinValue[randNum];}public String getCoinOption(){return this.coinOption;}}
CoinGame Class
public class CoinGame { Player[] players = new Player[2]; Coin theCoin = new Coin(); CoinGame(String p1Name, String p2Name){ players[0] = new Player(p1Name); players[1] = new Player(p2Name); } public void startGame(){ int randNum = Math.random()<0.5?0:1; String playersPick = players[randNum].getRandCoinOption(); int opponentsIndex = (randNum == 1)?0:1; players[opponentsIndex].setCoinOption(playersPick); String winningFlip = theCoin.getCoinOption(); players[0].didPlayerWin(winningFlip); players[1].didPlayerWin(winningFlip); }}
main 测试类
public class main {public static void main(String[] args){CoinGame theCoinGame = new CoinGame("Huazhe", "meigtong");String userAnswer;do{theCoinGame.startGame();System.out.println("Play again?y/n");Scanner input = new Scanner(System.in);userAnswer = input.nextLine();}while(userAnswer.equals("y"));}}
学习心得:个人感觉 OOD 体现的更多的是对问题由大到小进行分割的思想,将大的问题进行简化成一部分模块( object ), 然后对各个模块之间的交流进行组合已达到解决问题的最终目的。
0 0
- OOD - Object Oriented Design 面向对象设计实例
- Object Oriented Design面向对象设计
- Object-Oriented Game Design(面向对象游戏设计)
- Object-Oriented Game Design (面向对象的游戏设计)
- Object Oriented Design Principles 面向对象设计原则
- OOD(object oriented design)
- Objective-C——面向对象设计——OOD(Object Oriented Designs)
- CC150 chapter 8 OOD object-oriented design
- ood--面向对象设计
- 面向对象设计-OOD
- Object-Oriented Design Heuristics(OOD启思录)值得一看!
- [翻译] Effective C++, 3rd Edition, Chapter 6. Inheritance(继承)和 Object-Oriented Design(面向对象设计)
- 面向对象(Object Oriented,OO)
- 面向对象(Object Oriented,OO)
- 面向对象(Object Oriented)掠影
- OO(Object Oriented,面向对象)
- 面向对象设计原则(Object-Oriented Principle)
- 【设计模式】Object Oriented面向对象思想剖析
- ConcurrentHashMap原理分析
- 预定义宏 ANSI C
- Linux内核模块分析(module_init宏)
- IOS App发布异常问题汇总
- NYOJ-16-矩形嵌套
- OOD - Object Oriented Design 面向对象设计实例
- Codeforces Round #340 (Div. 2) 617E XOR and Favorite Number 莫队算法
- 深夜杂记
- Codeforces Round #353 (Div. 2) 模拟+贪心+乱搞
- 将ubuntu系统制作成iso镜像文件
- Unity Webplayer 两行代码屏蔽右键菜单
- Android Studio 2.1 之小设置提高写代码效率
- JPEG图像格式Jphide隐写
- Android中的Context,你造吗