代码改变世界

Head First Object-Oriented Design and Analysis学习笔记(九)

2010-08-03 20:00  Aga.J  阅读(317)  评论(0编辑  收藏  举报

第九章

Iterating and testing

The Software is Still for the Customer

前言:

   看到题目的iterating,就让我想起RUP中的迭代开发,这一章同样告诉我们,软件项目需要迭代,同时题目中也说到了test,而文章中就是在迭代过程中进行了测试,看了这也符合了RUP的过程质量保证。

案例分析:

案例描述:

经过一段时间的分析后,客户并没有看到他们想看到的东西。单靠设计阶段的产物,并不能满足客户的要求---看到实际运行的东西。

问题提出:

1 我们怎样来满足现阶段客户的要求

问题解决:

1 使用迭代的方式来开发我们的程序,在前面的分析基础之上,继续iterate deeper,继续分析和设计,但是这次是针对每个独立的部分。

  有两种深入迭代的方法:

(1)       Feature driven development

选择一个特定的feature也就是系统的一个特定的功能,然后plan,analyze,develop直到它功能比较完全比较完整

要注意,每次只能是一个feature,然后迭代,直到所有feature都完成(注:这里的迭代和RUP的迭代其实是不同的,Rup的迭代要求每个迭代都有一个可运行的产物,是系统的雏形,而不是跟这里的迭代一样,每个迭代完成后只是系统的一个小部分完成)

(2)       Use case driven development(RUP所提倡的)

这种方式关注的是一个特定的事件流或者用例,take a complete path through the application, with a clear start and end。其实也就是完成一个用例的代码编写。

同样,用例驱动开发方式的迭代也是指一个一个用例的完成,直到所有用例都完成了

 了解这两种开发方式的区别,才能更深刻的认识他们:

(1)       feature driven

它是一种开发粒度很小的开发方式,适用于系统中有许多不同的feature并且他们之间联系不大的情况

(2)       use case driven

它是一种面向big picture的开发方式,适用于系统中processes和scenario比较多的情况。例如交易系统这种会有很多事件流程的系统

 

问题提出:

1 文章使用的是feature driven,那么该怎样来进行iterate呢

 

问题解决:

1 选取一个feature(文章选取的是Unit),然后根据我们前面所学的分析方法,即textual analysis,分析得到和该feature有关的需求。

 

针对每一项,我们根据前面所学的开发过程(1 满足客户,2 使用oo法则使得程序更灵活,3 使用pattern来健壮我们的程序)

  所以接下来文章做的是通过一个test(这个过程也可以说是test driven development)来展示给客户Unit类的基本功能已经实现。

  完成test后,接下来就是应用oo法则来修改我们的unit类,文章又给出两种解决方案,一种是emphasizing commonality,也就是把共性抽取出来放到基类,不管它是不是易变的,一种是emphasizing encapsulation,也就是把所有易变的都封住起来,不管它是不是属于共性,两种解决方案都各有各的好处和坏处,在这种情况下,需要我们针对我们的项目和利用项目经验来做出判断。

  文章选择的是第一种(大多数人都会选择这种,看起来比较合理),然后又引入了test,所有又把所有功能test了一次。

 

问题提出:

1 由前面的开发过程,文章告诉我们,我们一直都是按照一种叫progranmming by contract是方式在进行。例如我们的unit类中,如果unit不存在,我们会返回一个null,我们相信我们的类使用者会检查null。然后就导出了defensive programming这种编程方式,以及他们之间的对比

问题解决:

1 defensive prigramming是一种防御式编程,对于每个不期望的结果,都会抛出一个异常,而不是返回null或者其他的比较友好但是容易被使用者遗忘的方式。同样道理,类的使用者可能也会采取这一的编程方式,他们会对每一个对象进行错误检查,防止传null,关于defensive programming和programming by contract的更多比较,书里面通过对话的形式给了出来,这里就不多说,因为觉得这两种方式也不是硬性规定,而且各有利弊,自己衡量好使用多大的尺度来编程就行。

Important Point

1 you write great software iteratively. Work on the big picture , and then iterate over pieces of the app until it’s complete.

2 don’t forget to test for incorrect usage of the software, too. You will catch errors early, and make your customers very happy.

3 test driven development focuses on getting the behavior of your classes right.

4 good software is built iteratively, analyze, design and then iterate again, working on smaller and smaller parts of your app.

5 when you are programming by contract, you are working with client code to agree on how you will handle problem situations

When you are programming defensively, you are making sure the client gets a safe response, no matter what the client wants to have happen

6

(1)Break your apps up into smaller chunks of functio2nality

  Break up into use cases or features and then solving a part of the problem

(2)But you can still break things up further

  Break things up further, and begin to iterate,do more analysis and design

(3)Your decisions can iterate down ,too

7 use case driven development focuses on one scenario in a use case in your application at a time

8 software development is always iterative, you look at the big picture, and then iterate down to smaller pieces of functionality

9 本章在代码编写中使用了“名值相对”的方法,更多分析在pdf的注释里。

小结:这一章读起来没前面的精彩,大多数内容是已经学过的,但是怎么去应用,在什么场合下应用就是个问题了。这一点需要不断实际训练才能做到。