一道面试题想到的设计模式(观察者模式)

一道面试题想到的。。

面试题:

A   squad   of   robotic   rovers   are   to   be   landed   by   NASA   on   a   plateau   on   Mars.     This   plateau,   which   is   curiously   rectangular,   must   be   navigated   by   the     rovers   so   that   their   on-board   cameras   can   get   a   complete   view   of   the     surrounding   terrain   to   send   back   to   Earth.     A   rover's   position   and   location   is   represented   by   a   combination   of   x   and   y     co-ordinates   and   a   letter   representing   one   of   the   four   cardinal   compass     points.   The   plateau   is   divided   up   into   a   grid   to   simplify   navigation.   An     example   position   might   be   0,   0,   N,   which   means   the   rover   is   in   the   bottom     left   corner   and   facing   North.     In   order   to   control   a   rover,   NASA   sends   a   simple   string   of   letters.   The     possible   letters   are   'L',   'R'   and   'M'.   'L'   and   'R'   makes   the   rover   spin   90     degrees   left   or   right   respectively,   without   moving   from   its   current   spot.     'M'   means   move   forward   one   grid   point,   and   maintain   the   same   heading.     Assume   that   the   square   directly   North   from   (x,   y)   is   (x,   y+1).         INPUT:     The   first   line   of   input   is   the   upper-right   coordinates   of   the   plateau,   the     lower-left   coordinates   are   assumed   to   be   0,0.     The   rest   of   the   input   is   information   pertaining   to   the   rovers   that   have     been   deployed.   Each   rover   has   two   lines   of   input.   The   first   line   gives   the     rover's   position,   and   the   second   line   is   a   series   of   instructions   telling     the   rover   how   to   explore   the   plateau.     The   position   is   made   up   of   two   integers   and   a   letter   separated   by   spaces,     corresponding   to   the   x   and   y   co-ordinates   and   the   rover's   orientation.         Each   rover   will   be   finished   sequentially,   which   means   that   the   second   rover     won't   start   to   move   until   the   first   one   has   finished   moving.             OUTPUT     The   output   for   each   rover   should   be   its   final   co-ordinates   and   heading.         INPUT   AND   OUTPUT         Test   Input:     5   5     1   2   N     LMLMLMLMM     3   3   E     MMRMMRMRRM     火星探测器     一小队机器人探测器将由NASA送上火星高原,探测器将在这个奇特的矩形高原上行驶。     用它们携带的照相机将周围的全景地势图发回到地球。每个探测器的方向和位置将由一个x,y系坐标图和一个表示地理方向的字母表示出来。为了方便导航,平原将被划分为网格状。位置坐标示例:0,0,N,表示探测器在坐标图的左下角,且面朝北方。为控制探测器,NASA会传送一串简单的字母。可能传送的字母为:'L','R'和'M'。   'L',和'R'分别表示使探测器向左、向右旋转90度,但不离开他所在地点。'M'   表示向前开进一个网格的距离,且保持方向不变。假设以广场(高原)的直北方向为y轴的指向。     输入:首先输入的line是坐标图的右上方,假定左下方顶点的坐标为0,0。剩下的要输入的是被分布好的探测器的信息。每个探测器需要输入wo   lines。第一条line   提供探测器的位置,第二条是关于这个探测器怎样进行高原探测的一系列说明。位置是由两个整数和一个区分方向的字母组成,对应了探测器的(x,y)坐标和方向。每个探测器的移动将按序完成,即后一个探测器不能在前一个探测器完成移动之前开始移动。     输出:每个探测器的输出应该为它行进到的最终位置坐标和方向。输入和输出   测试如下:           期待的输入:5   5     1   2   N   LMLMLMLMM   3   3   E   MMRMMRMRRM   期待的输出     1   3   N   5   1   E 

设计采用观察者模式,如下:

1.被观察者SubjcetNASA.java

package cn.zhangzq.obserer;



public class Client {



	/**

	 * @param args

	 */

	public static void main(String[] args) {

		Rover rover1=new Rover(3,3,'E');

		SubjcetNASA testSubjet=new SubjcetNASA();

		testSubjet.addObserver(rover1);

		String commandLine="3 3 E MMRMMRMRRM";

		testSubjet.sendCommand(commandLine);

	}

}

2观察者Rover.java

package cn.zhangzq.obserer;



import java.util.Observable;

import java.util.Observer;



public class Rover implements Observer {



	private static int coordinateX;



	private static int coordinateY;



	private char direction;



	public Rover(int coordinateX, int coordinateY, char direction) {

		super();

		this.coordinateX = coordinateX;

		this.coordinateY = coordinateY;

		this.direction = direction;



	}



	public void update(Observable o, Object arg) {

		// 1.解析命令

		String[] command = parseCommand(o);

		if ("Y".equals(command[0])) {



			move(command[1]);

			diaPlayRovers();

		}

	}



	private void move(String behavior) {

		for (int i = 0; i < behavior.length(); i++) {

			char temp = behavior.charAt(i);

			parseBehavior(temp);

		}

	}



	private void parseBehavior(char temp) {

		switch (temp) {

		case 'L':

			turnLeft();

			break;

		case 'R':

			turnRight();

			break;

		case 'M':

			moveStep();

			break;

		}

	}



	private void turnRight() {

		switch (this.direction) {

		case 'N':

			this.direction = 'E';

			break;

		case 'W':

			this.direction = 'N';

			break;

		case 'S':

			this.direction = 'W';

			break;

		case 'E':

			this.direction = 'S';

			break;

		}



	}



	private void moveStep() {

		if (this.direction == 'S') {

			coordinateY--;



		}

		if (this.direction == 'W') {

			coordinateX--;



		}

		if (this.direction == 'N') {

			coordinateY++;



		}

		if (this.direction == 'E') {

			coordinateX++;



		}



	}



	private void turnLeft() {

		switch (this.direction) {

		case 'N':

			this.direction = 'W';

			break;

		case 'W':

			this.direction = 'S';

			break;

		case 'S':

			this.direction = 'E';

			break;

		case 'E':

			this.direction = 'N';

			break;

		}

	}



	/**

	 * 

	 * @return [0]:是否接收命令 "Y":接收 ,"N":不接收 ; [1]:具体指令

	 */

	@SuppressWarnings("static-access")

	private String[] parseCommand(Observable o) {

		String commandLine = ((SubjcetNASA) o).getCommandLine();

		String[] command = { "", "" };



		String[] temp = commandLine.split(" ");

		if (Integer.parseInt(temp[0]) == coordinateX

				&& Integer.parseInt(temp[1]) == coordinateY

				&& temp[2].charAt(0) == direction) {

			command[0] = "Y";

		} else {

			command[0] = "N";

		}

		command[1] = temp[3];

		return command;



	}



	private void diaPlayRovers() {

		System.out.println(coordinateX + " " + coordinateY + " " + direction);

	}



}

3.Client.java

package cn.zhangzq.obserer;



public class Client {



	/**

	 * @param args

	 */

	public static void main(String[] args) {

		Rover rover1=new Rover(3,3,'E');

		SubjcetNASA testSubjet=new SubjcetNASA();

		testSubjet.addObserver(rover1);

		String commandLine="3 3 E MMRMMRMRRM";

		testSubjet.sendCommand(commandLine);

	}

}

我这个实现并没有完全按照面试题的要求去做,只是模拟了一下。

Thoughtworks公司面试题——MARS ROVERS问题火星探测器 C# 实现 VS2010工程,带界面展示! 一小队机器人探测器将由NASA送上火星高原,探测器将在这个奇特的矩形高原上行驶。 用它们携带的照相机将周围的全景地势图发回到地球。每个探测器的方向和位置将由一个x,y系坐标图和一个表示地理方向的字母表示出来。为了方便导航,平原将被划分为网格状。位置坐标示例:0,0,N,表示探测器在坐标图的左下角,且面朝北方。为控制探测器,NASA会传送一串简单的字母。可能传送的字母为: 'L ', 'R '和 'M '。 'L ',和 'R '分别表示使探测器向左、向右旋转90度,但不离开他所在地点。 'M ' 表示向前开进一个网格的距离,且保持方向不变。假设以广场(高原)的直北方向为y轴的指向。 输入:首先输入的line是坐标图的右上方,假定左下方顶点的坐标为0,0。剩下的要输入的是被分布好的探测器的信息。每个探测器需要输入wo lines。第一条line 提供探测器的位置,第二条是关于这个探测器怎样进行高原探测的一系列说明。位置是由两个整数和一个区分方向的字母组成,对应了探测器的(x,y)坐标和方向。每个探测器的移动将按序完成,即后一个探测器不能在前一个探测器完成移动之前开始移动。 输出:每个探测器的输出应该为它行进到的最终位置坐标和方向。输入和输出 测试如下: 期待的输入: 5 5 1 2 N LMLMLMLMM 3 3 E MMRMMRMRRM 期待的输出 1 3 N 5 1 E
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值