「自顶向下,逐步求精」

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,time2){
    CALL wash(time1,time2)
}

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


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

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

总之,不管作为一个程序员,一个 IT 工作者,还是一个学习者,一个不断成长的个体,我想我们都有必要掌握「自顶向下,逐步求精」的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值