您的位置:首页 > 其它

「自顶向下,逐步求精」

2017-11-28 17:46 211 查看
A top-down approach is essentially the breaking down of a system to gain insight into its compositional sub-systems in a reverse engineering fashion.

–Wikipedia

自顶向下设计是一种分析问题的方法,可以被应用在面向过程的编程中。简单的说,自顶向下就是将一个大问题先分解为若干个小问题,在将每个小问题继续分解,直到不能再分解或者可以轻易实现为止。应用在计算机领域的编程过程中,则是将主程序分为若干个模块,实现了这些模块,就可以实现整个程序,然后再用同样的方法对待每一个模块,这样,实现每一个小模块就意味着实现最终的大模块。



「自顶向下」,将问题不断分解为小的模块

至于「逐步求精」,你可以把它理解为将各个小模块实现和优化的过程。

下面我们用生活中常见的洗衣机的例子来说明这个方法。我们的任务是实现洗衣机的洗衣程序。

假设以下的模块已经可以实现了:

waterinswitch(openclose) // open 打开上水开关, close关闭

wateroutswitch(openclose) // open 打开排水开关, close关闭

getwatervolume() //返回洗衣机内部水的高度

motorrun(direction) // 电机转动。 left左转, right右转, stop停

timecounter() // 返回当前时间计数,以秒为单位

halt(returncode) //停机, success 成功 failure 失败

Level 1

首先先将整个洗衣程序分解为以下几个大步骤:

water in
soak
wash
water out
rinse
dewater
waterout
halt


Level 2

然后利用上面已经实现的模块,将上面各个步骤的实现用伪代码表示出来:

water in:
WHILE getwatervolume() < standard_watervolume
waterinswitch(open)
IF timecounter() > max_waterin_time
halt(failure)
ENDWHILE
waterinswitch(close)

soak:
WHILE timecounter() < standard_soaking_time
ENDWHILE

wash:
WHILE timecounter() < standard_washing_time
WHILE timecounter() < each_direction_time
motorrun(left)
ENDWHILE
motorrun(stop)
WHILE timecounter() < each_direction_time
motorrun(right)
ENDWHILE
motorrun(stop)
ENDWHILE

waterout:
WHILE getwatervolume() > 0
wateroutswitch(open)
IF timecounter() > max_waterout_time
halt(failure)
ENDWHILE
wateroutswitch(close)

rinse:
FOR each time in standard_rinsing_number_times
WHILE getwatervolume() < standard_watervolume
waterinswitch(open)
IF timecounter() > max_waterin_time
halt(failure)
ENDWHILE
waterinswitch(close)
WHILE timecounter() < standard_rinsing_time
WHILE timecounter() < each_direction_time
motorrun(left)
ENDWHILE
motorrun(stop)
WHILE timecounter() < each_direction_time
motorrun(right)
ENDWHILE
motorrun(stop)
ENDWHILE
WHILE getwatervolume() > 0
wateroutswitch(open)
IF timecounter() > max_waterout_time
halt(failure)
ENDWHILE
wateroutswitch(close)
ENDFOR

dewatering:
WHILE timecounter() < standard_dewatering_time
WHILE timecounter() < each_direction_time
motorrun(left)
ENDWHILE
motorrun(stop)
WHILE timecounter() < each_direction_time
motorrun(right)
ENDWHILE
motorrun(stop)
ENDWHILE

waterout:
WHILE getwatervolume() > 0
wateroutswitch(open)
IF timecounter() > max_waterout_time
halt(failure)
ENDWHILE
wateroutswitch(close)

halt(success)


逐步求精

我们知道现代洗衣机一般有许多不同的洗衣模式,比如快速洗衣相比于标准洗衣可能有更少的水量,更短的洗涤时间,更少的漂洗次数等等。也就是说,我们应该提取出不同模式中相同的功能模块,通过输入不同的参数而实现不同的模式。

例如:

wait(time){
WHILE timecounter() < time
ENDWHILE
}

waterin(volume, time){
WHILE getwatervolume() < volume
waterinswitch(open)
IF timecounter() > time
halt(failure)
ENDWHILE
waterinswitch(close)
}

waterout(time){
WHILE getwatervolume() >0
wateroutswitch(open)
IF timecounter() > time
halt(failure)
ENDWHILE
wateroutswitch(close)
}

wash(time1, time2){
WHILE timecounter() < time1
WHILE timecounter() < time2
motorrun(left)
ENDWHILE
motorrun(stop)
WHILE timecounter() < time2
motorrun(right)
ENDWHILE
motorrun(stop)
ENDWHILE
}

rinse(time1, time2, times, volume, time3){
FOR each time in times
CALL waterin(volume, time3)
CALL wash(time1,time2)
CALL waterout(time3)
ENDFOR
}

dewater(time1,t
4000
ime2){
CALL wash(time1,time2)
}


可以看到,有的模块内部也引用了别的模块。

其实「自顶向下,逐步求精」不仅可以应用在编程中,它更是一种思维方式。比如著名的乔希·维茨金 (Josh Waitzkin)在他的书《学习之道》中介绍了他之所以能在多个不同的领域达到世界顶级水平的秘诀就是这种「自顶向下,逐步求精」的思维方式。比如国际象棋,先把马在各种情况下应该怎么走,怎么把一个兵走好等研究透,然后通过不断的练习将这些技能作为一个模块包装起来,用时只要调用这个模块即可。



同时是国际象棋大师和太极拳大师的乔希·维茨金

更详细的介绍可以参考这篇文章

总之,不管作为一个程序员,一个 IT 工作者,还是一个学习者,一个不断成长的个体,我想我们都有必要掌握「自顶向下,逐步求精」的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: