我正在处理一个大型项目(对我来说),它将有许多类,需要可扩展,但我不确定如何规划我的程序以及类需要如何交互。

几个学期前我上了一门OOD课程,从中学到了很多东西;比如编写UML,并将需求文档转换为对象和类。我们也学过序列图但不知怎么的我错过了这节课,我没能记住它们。

在之前的项目中,我尝试使用从课程中学到的方法,但通常在我说“是的,这看起来像我想要的东西”时,我就会以代码结束,我不想再挖掘出新的功能。

我有一本Steve McConnell的《Code Complete》,我经常在这里和其他地方听到它的神奇之处。我读了关于设计的章节,似乎没有得到我想要的信息。我知道他说这不是一个固定的过程,它主要是基于启发式,但我似乎不能把他所有的信息都应用到我的项目中。

那么,在高级设计阶段(在开始编程之前),你要做些什么来确定你需要什么类(特别是那些不基于任何“现实世界对象”的类)以及它们如何相互交互?

我特别感兴趣的是你使用的方法是什么?你遵循什么样的过程,通常会产生一个良好的,干净的设计,将接近最终产品?


当前回答

我建议你使用BlueJ和ActiveWriter来学习和发展对对象的良好理解。推荐的书也是很好的资源。

从维基百科:

(来源:bluej.org)

BlueJ是一个综合开发项目 Java编程环境 语言,主要是为了 教育的目的,也是 适用于小规模软件 发展。

此外,它使用UML,对我来说,这是一个很好的资源来理解建模对象的一些事情。

可选文字 http://www.ryanknu.com/ryan/bluej.png

ActiveWriter是一个建模实体和关系的工具,它还可以生成代码,并且很容易进行更改。它将节省您的时间,并且对于敏捷开发非常适合。

(来源:altinoren.com)

其他回答

During my adventures of designing class structures, I’ve noticed that it’s very helpful to start with writing some pseudo-code. That means: I start with “writing” some general fragments of application’s code on a highest level, play with it, and discover the elements that are appearing – in fact, the elements that I – as a programmer – would like to use. It’s a very good starting point for designing general structure of modules and their interactions. After few iterations the whole structure starts to look more like a full system of classes. It’s a very flexible way to design parts of code. You can call it a programmer-oriented design.

学习设计模式。在过去的两年里,这是我个人在面向对象编程方面的革命。找一本书。我向你推荐这个:

头部优先的设计模式

它使用Java,但可以扩展到任何语言。

学习和掌握设计模式。 接下来,学习领域驱动设计 之后,学习需求收集

我上了几个学期的OOD课程 回来后,我学到了很多;就像 编写UML和翻译 将需求文档转换为对象 和类。我们学过序列 图表也有,但不知怎么的,我错过了 讲座之类的,他们没有 跟紧我。

You know about the step 3. You need to master it. I mean, via a lot of practice to make it become your second nature. That's because the method you learn, is simply against the way we used to have. So you need to really master it. Otherwise, you will always find yourself go back to your original way of doing thing. This is somehow like Test Driven Process, where a lot of java developer give it up after a few tries. Unless they fully master it, otherwise it's just a burden to them Write use cases, especially for alternate course. Alternate course occupy more than 50% of our development time. Normally when your PM assign you a task, for instance, create a login system, he will think it's straight forward, you can take 1 day to finish it off. But he never take into account that you need to consider, 1. what if user key in wrong password, 2. what if user key in wrong password for 3 times, 3. what if user doesn't type in user name and etc. You need to list them out, and show it to your PM, ask him to reschedule the deadline.

老实说,最好是回头看看流程图和序列图。有大量的好网站告诉你如何做到这一点。当我考虑如何将程序分解为类时,我发现这是非常宝贵的,因为我确切地知道程序需要输入、计算和输出什么,并且每一步都可以分解为程序的一个部分。

我在实际项目中使用的成功的技术是责任驱动设计,灵感来自Wirfs-Brock的书。

从最顶层的用户故事开始,与同事一起,在白板上勾勒出它们所暗示的高级交互。这让你对大模块有了初步的了解;重复一两次高级CRC-card(游戏邦注:如play),你应该已经稳定了一个主要组件列表,它们的作用以及它们如何相互作用。

然后,如果任何职责很大或很复杂,那么细化这些模块,直到有足够小且简单的东西成为对象,方法是在模块内执行由更高级别交互确定的每个主要操作的交互。

知道什么时候该停下来是一个判断问题(只有经验才能决定)。