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

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

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

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

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

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


当前回答

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

头部优先的设计模式

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

其他回答

它经常被重复,但完全正确——理解你的数据。

对于面向对象编程,你的类应该描述重要的信息片段以及它们如何交互。

如果您有一个心智模型,它很好地描述了数据的行为和生命周期,那么您将很容易布局您的类。

这句话其实是“确切地知道你要做什么”的延伸。

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

从维基百科:

(来源:bluej.org)

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

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

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

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

(来源:altinoren.com)

只是引用http://www.fysh.org/~katie/computing/methodologies.txt

并且RUP的核心是一个您必须使用面向对象设计的小区域 人才……如果你没有它们,就像有一个方法论 100米跑。

“第一步:写关于快跑的故事。 第二步:画一张赛马场平面图。 第三步:去买紧身的莱卡短裤。 第四步:跑得非常、非常、非常快。 第五步:先跨线

第四步才是最难的。但如果你特别强调 在第1 2 3 5次,有可能没有人会注意到,然后你就可以 卖这个方法可能会赚很多钱 那些认为成为百米运动员有什么“秘密”的运动员

斯科特·戴维斯补充说:

Make absolutely sure you know what your program is all about before you start. What is your program? What will it not do? What problem is it trying to solve? Your first set of use cases shouldn't be a laundry list of everything the program will eventually do. Start with the smallest set of use cases you can come up with that still captures the essence of what your program is for. For this web site, for example, the core use cases might be log in, ask a question, answer a question, and view questions and answers. Nothing about reputation, voting, or the community wiki, just the raw essence of what you're shooting for. As you come up with potential classes, don't think of them only in terms of what noun they represent, but what responsibilities they have. I've found this to be the biggest aid in figuring out how classes relate to each other during program execution. It's easy to come up with relationships like "a dog is an animal" or "a puppy has one mother." It's usually harder to figure out relationships describing run-time interactions between objects. You're program's algorithms are at least as important as your objects, and they're much easier to design if you've spelled out what each class's job is. Once you've got that minimal set of use cases and objects, start coding. Get something that actually runs as soon as possible, even though it doesn't do much and probably looks like crap. It's a starting point, and will force you to answer questions you might gloss over on paper. Now go back and pick more use cases, write up how they'll work, modify your class model, and write more code. Just like your first cut, take on as little at a time as you can while still adding something meaningful. Rinse and repeat.

这只是我的个人意见。希望对大家有用。

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.