黑马程序员 交通灯系统
来源:互联网 发布:linux原生迅雷 编辑:程序博客网 时间:2024/06/02 23:32
------- android培训、java培训、期待与您交流! ----------
1,张老师在一开始为了提高同学们的面向对象的设计思维能力,便引出了两个例子:
例一:两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子
张老师的是这样设计的:
首先有这样几个类
Stone//石头
StoneKnife//石刀,这个类里面有一个publicMaterial cut(Tree tree)方法
Material//材料
Tree//树
Chair//椅子
然后张老师想到石头磨出刀后石头自己就应该没了,材料做成椅子后也应该没了,张老师觉得这样的思想如果要设计到对象所对应的类里面就比较难以实现,所以就又设计了两个工厂类
KnifeFactory//这个类里面有个createKnife(Stonestone1,Stone stone2)方法
ChairFactory//这个类里面有个makeChair(Materialmaterial)方法
个人觉得张老师的设计这两个工厂类是没有必要的,类多了就过于复杂了,况且他这样设计似乎也没有达到让对象自己把自己变没了的目的,下面我给出我的设计,个人觉得比张老师的要好点,也许我的还没有他设计的好,仅供交流,呵呵……
- public class CharMake {
- public static void main(String[] args) {
- StoneKnife stoneKnife = new Stone().produceStoneKnife(new Stone());// 磨刀
- Wood wood = stoneKnife.cutTree(new Tree(20));// 砍树
- wood.makeChar();// 做成椅子
- }
- }
- /* 定义一个Stone类来封装石头 */
- class Stone {
- private int life = 1;// 表示石头的生命,磨出一把石刀后就不存在了,即生命变为0
- public StoneKnife produceStoneKnife(Stone stone) {
- if (1 == this.life & 1 == stone.life) {
- this.life = 0;
- stone.life = 0;// 石刀麻成后,两块石头就不存在了,所以生命为0
- System.out.println("两块石头磨成一把石刀");
- return new StoneKnife();// 返回生产出来的石刀
- }
- System.out.println("制刀失败");
- return null;
- }
- }
- /* 定义一个StoneKnife类来封装石刀 */
- class StoneKnife {
- private int life = 100;// 表示石刀的生命,
- public Wood cutTree(Tree tree) {
- if (tree.getLife() > 0 & life > tree.getLife()) {
- int treeLife = tree.getLife();
- life -= treeLife;// 每砍完一棵树,石刀就根据树的大小而减少相应的生命值
- tree.setLife(0);// 树被砍了后生命变为0
- System.out.println("石刀可以砍树,砍成木材");
- return new Wood(treeLife);// 树的生命转化为木材的生命
- }
- System.out.println("砍树失败");
- return null;
- }
- }
- /* 定义一个Tree类来封装树 */
- class Tree {
- private int life;// 树的生命
- public Tree(int life) {
- this.life = life;
- }
- public int getLife() {
- return life;
- }
- public void setLife(int life) {
- this.life = life;
- }
- }
- /* 定义一个Wood类来封装木材 */
- class Wood {
- private int life;// 木材的生命
- public Wood(int life) {
- this.life = life;
- }
- public Chair makeChar() {
- if (life >= Chair.wood) {
- life -= Chair.wood;// 木材做出来一把椅子后,就减少相应的生命值
- System.out.println("木材可以做成椅子");
- return new Chair();
- }
- System.out.println("制作椅子失败");
- return null;
- }
- public int getLife() {
- return life;
- }
- public void setLife(int life) {
- this.life = life;
- }
- }
- /* 定义一个Chair类来封装椅子 */
- class Chair {
- public static int wood = 2;// 制作一把椅子需要消耗的木材资源
- }
例二:球从一根绳子的一端移动到了另一端
张老师是这样设计 的
- class Rope{
- private Point start;
- private Point end;
- public Rope(Point start, Point end){
- this.start = start;
- this.end = end;
- }
- public Point nextPoint(Point currentPoint){
- /*通过两点一线的数学公式可以计算出当前点的下一个点,这个细节不属于设计阶段要考虑的问题,
- * 如果当前点是终止点,则返回null,如果当前点不是线上的点则抛出异常
- * */
- }
- }
- class Ball{
- private Rope rope;
- private Point currentPoint;
- public Ball(Rope rope,Point startPoint){
- this.rope = rope;
- this.currentPoint = startPoint;
- }
- public void move(){
- currentPoint = rope.nextPoint(currentPoint);
- System.out.println("小球移动到了"+currentPoint);
- }
- }
我是这样设计的
- public class ballMove {
- public static void main(String[] args) {
- new Ball().moveOnRope(new Rope(30));
- }
- }
- /* 定义一个Ball类来代表球 */
- class Ball {
- /* moveOnRope方法模拟球在绳子上移动的过程 */
- public void moveOnRope(Rope rope) {
- System.out.println("出发");
- for (int i = 0; i <= rope.getLength(); i++) {
- System.out.println("已经走了" + i + "米");
- }
- System.out.println("到达另一端");
- }
- }
- /* 定义一个Rope类来代表绳子 */
- class Rope {
- private int length;
- public Rope(int length) {
- this.length = length;
- }
- public int getLength() {
- return length;
- }
- }
我觉得我和张老师的不同就在于他把绳子设计在了一个二维空间里,而我则是把绳子设计在了一个一维空间里
下面是我自己模拟的这个交通灯系统
- package com.isoftstone.interview.traffic;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class MainClass {
- public static void main(String[] args) {
- new Controller();
- }
- }
- class Controller {
- /*
- * 一个十字路口就是两条路相交,所以这里创建两条路 一条东西干道,一条南北干道 初始状态为东西干道的绿灯亮,南北干道的红灯亮
- */
- private Road roadEW = new Road(true, "东西干道");
- private Road roadSN = new Road(false, "南北干道");
- public Controller() {
- System.out.println("东西干道 绿灯亮,南北干道红灯亮");// 打印初始状态
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- try {
- Thread.sleep(30000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- roadEW.changeLight();
- roadSN.changeLight();
- }
- }
- }).start();// 每隔30秒换次信号灯
- }
- }
- class Road {
- /* 每条道上的车的去向只有3个,即向左转,直行,向右转 */
- private static final String[] goTo = { "left", "front", "right" };
- /*
- * 每条道上都有一个信号等,本来这里可以是静态的,但是为了体现面向对象的设计思想, 我就没有把它定义为静态的
- */
- private boolean light;
- /* 保存路的名字,这主要是为了方便测试,在打印时可以打印出是哪条路在通行 */
- private String name;
- /**
- *
- * @param light
- * 道路上路灯的初始状态
- * @param name
- * 道路的名字
- */
- public Road(boolean light, String name) {
- this.light = light;
- this.name = name;
- /* 当道路一创建,道路上就开始行驶车辆了 */
- this.removeThread(left, "左边");
- this.removeThread(right, "右边");
- this.addThread(left);
- this.addThread(right);
- }
- public void changeLight() {
- light = true == light ? false : true;// 路灯转换
- if (light) {
- System.out.println(this.name + "绿灯亮,");
- } else {
- System.out.println(this.name + "红灯亮,");
- }
- }
- public void removeThread(final List<car> list, final String listname) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (0 != list.size()) {// 如果道上的车辆数不为0
- if (Road.this.light
- | list.get(0).getGoTo().equals("right")) {
- /* 道路上亮的是绿灯或者最前面的那辆车是向右转就旅行 */
- System.out.println(Road.this.name + listname
- + "使过一辆向" + list.get(0).getGoTo()
- + "方向行驶的车");
- list.remove(0);
- }
- }
- }
- }
- }).start();
- }
- public void addThread(final List<car> list) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- /* 增加车辆 */
- list.add(new Car(goTo[new Random().nextInt(3)]));
- }
- }
- }).start();
- }
- /* 每条道路都是双向道,所以我们这里就用两个集合来存储这两个道上的车辆 */
- private List<car> left = new ArrayList<car>();
- private List<car> right = new ArrayList<car>();
- }
- class Car {
- private String goTo;// 储存车辆的去向,左转,直行或者右转
- public Car(String goTo) {
- this.goTo = goTo;
- }
- /* 获取车辆的去向 */
- public String getGoTo() {
- return goTo;
- }
- }
- </car></car></car></car></car></car>
- 黑马程序员 交通灯系统
- 黑马程序员-交通灯系统
- 黑马程序员-交通灯系统
- 黑马程序员->交通灯系统
- 黑马程序员 交通灯系统
- 【黑马程序员】交通灯系统
- 黑马程序员-交通灯系统
- 黑马程序员 交通灯系统
- 【黑马程序员】交通灯系统
- 黑马程序员--交通灯系统
- 【黑马程序员】交通灯系统
- 黑马程序员--交通灯系统
- 黑马程序员 交通灯系统
- 黑马程序员--交通灯系统
- 黑马程序员--交通灯系统
- 黑马程序员-交通灯系统
- 《黑马程序员》交通灯管理系统
- 黑马程序员----交通灯管理系统
- Android 设计模式 之 观察者模式
- java-内部类
- cisco 查看 模块 是单 多 模
- StringEscapeUtils类的转义与反转义方法
- 删除Android4.0的状态栏
- 黑马程序员 交通灯系统
- 高低点比较、突破策略 using sas
- (3期)传递字符,查找数据库相关表及字段值
- poj1703解题报告
- 搞定KDB
- javascript中的getElementById
- 黑马程序员 银行业务调度
- 代码排版
- Linux与windows的软/硬链接