# 火星车 Tasking 任务分解

## MarsRover 火星漫游者 任务分解练习

## 教学目的

* 反复练习如何用测试框定需求范围
* 反复练习如何拆解合理的任务列表

### 如何任务分解

请回忆一下任务分解的方法

```bash
#1 函数a
输入：
    paramX: TypeX
    bValue: TypeB <- 函数b
    cValue: TypeC <- 函数c
输出：
    aValue: TypeA
    paramY: TypeY -> 函数b
    paramZ: TypeZ -> 函数c

#2 函数b
输入：
    paramY: TypeY
输出：
    bValue: TypeB

#3 函数c
输入：
    paramZ: TypeZ
输出：
    cValue: TypeC
```

以上是任务分解时需要注意的数据结构，我们推荐的方式是画图，手边准备纸笔即可。

![](https://2897586075-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LquCgGNObzQ8HY5Og1P%2F-LqvN8fjJNwcmDOlf1kK%2F-LqvN9WfUUlvmLhsiUTE%2Fdetailed-tasking.png?generation=1570806766481064\&alt=media)

### 火星车操作任务分解

```bash
#1 函数marsrover: 火星漫游车
输入：# 操作指令
    commandString: String
输出：# 火星车位置和朝向
    position: String
```

从输入和输出的角度，我们可以将这个任务  以上述形式描述，如果画图，则如下图：

![](https://2897586075-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LquCgGNObzQ8HY5Og1P%2F-M1kLkaqhDjSaKiTLqNi%2F-M1kLllh2EFM_Uawp3qc%2Fmarsrover-tasking-01.jpg?generation=1583506736954826\&alt=media)

### 以终为始进行分析

我们先回顾一下任务需要的输出：

```bash
1 3 N
```

从界面  上来看是这个样子：

![](https://2897586075-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LquCgGNObzQ8HY5Og1P%2F-M26POn8RzGpt7JmXdbN%2F-M26PPRGRKHIUhexjpVE%2Fmarsrover-sample.jpg?generation=1583893556156602\&alt=media)

最终我们需要输出：火星车到坐标和方向。

以此，我们可以推导出最直接的分解步骤：

![](https://2897586075-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LquCgGNObzQ8HY5Og1P%2F-M1kLkaqhDjSaKiTLqNi%2F-M1kLlljlXmdqKg1XC81%2Fmarsrover-tasking-02.jpg?generation=1583506738030808\&alt=media)

### 进一步细化

对于多次运动，我们进行更进一步的分解：

![](https://2897586075-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LquCgGNObzQ8HY5Og1P%2F-M1kLkaqhDjSaKiTLqNi%2F-M1kLlll01bA3wfhyMGU%2Fmarsrover-tasking-03.jpg?generation=1583506739114804\&alt=media)

### 初步完成任务分解

这样我们初步完成了任务分解，为什么是初步呢，我们需要进一步明确每一个步骤的输入和输出。

### **误区** 忽略数据建模

绝大部分的前端同学，任务分解到这一步就算完成了，但是在实施的时候，很容易遇到各种问题，因为容易陷入误区：忽略数据建模。这里我们从过年图中可以看出，有两个概念是反复出现的：指令，位置和朝向。

原始的输入输出数据，只是用字符串`1 3 N`来表示。很显然这不是一种代码易读的结构化数据，所以对核心数据进行建模，尤其是关键的输入输出数据建模，是非常重要的；这个步骤，对于 JAVA 开发来说，是必需的，但是对很多 Javascript 开发者来说，是一个需要练习的过程。

### 输入输出建模

```javascript
//火星车输入指令结构化对象
type Command = {
    X: Number, //区域最大长度
    Y: Number, //区域最大宽度
    x: Number, //火星车初始X坐标位置
    y: Number, //火星车初始Y坐标位置
    direct: String, //火星车初始朝向
    commands: String //火星车原始移动指令
}
```

```javascript
//火星车状态结构化对象
type Marsrover = {
  x: Number, //火星车X坐标位置
  y: Number, //火星车Y坐标位置
  direct: String, //火星车朝向
}
```

### 增加输入输出处理

最终我们形成了完整的包含输入输出处理的任务分解方案

![](https://2897586075-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LquCgGNObzQ8HY5Og1P%2F-M1kLkaqhDjSaKiTLqNi%2F-M1kLllnn9VvZjTCybw5%2Fmarsrover-tasking-04.jpg?generation=1583506740084056\&alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jimmylv.gitbook.io/tdd-frontend/coding/01-marsrover/07-marsrover-tasking.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
