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

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

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

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

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

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


当前回答

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

头部优先的设计模式

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

其他回答

尝试使用行为驱动开发。要打破你的旧习惯很难,但我发现BDD确实是你在现实世界中开发的最佳选择。

http://behaviour-driven.org/

恐怕人们不愿意听到这样的回答。无论如何,让我陈述我的观点。

OOP should be viewed as one of the paradigms, not as the superior paradigm. OOP is good for solving certain kind of problems, like developing a GUI library. It also fits into the style of software development usually followed by large software companies - an elite team of designers or architects lays down the software design in UML diagrams or some other similar medium and a less enlightened team of developers translate that design into source code. OOP offer little benefit if you are working alone or with a small team of highly talented programmers. Then, it is better to use a language that supports multiple paradigms and will help you to come up with a prototype fast. Python, Ruby, Lisp/Scheme etc are good choices. The prototype is your design. Then you improve on that. Use the paradigm that is best to solve the problem at hand. If needed, optimize hot spots with extensions written in C or some other systems language. By using one of these languages, you also get extensibility for free, not just at the programmer level but also at the user level. Languages like Lisp can dynamically generate and execute code, which means your users can extend the application by writing small code snippets, in the language that the software itself is coded! Or if you choose to write the program in C or C++, consider embedding an interpreter for a small language like Lua. Expose functionalities as plugins written in that language.

我认为,大多数时候OOP和OOD所创造的软件都是过度设计的受害者。

总之,我喜欢的写软件的方式是:

使用动态语言。 用这种语言编写设计(原型)。 如果有必要,使用C/ c++优化某些领域。 通过实现语言本身的解释器提供可扩展性。

最后一个特性使软件能够轻松地适应特定用户(包括我自己!)的需求。

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

我上了几个学期的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.

首先,设计应该来自你的灵魂。你必须用每一根纤维去感受它。在我开始做任何事情之前,我通常会走两三个月,只是在街上走(真的)。和思考。你知道,散步是一种很好的冥想。所以它能让你更好地集中注意力。

其次,只在存在自然对象层次结构的地方使用OOP和类。不要人为地把它“拧”进去。如果没有严格的层次结构存在(就像在大多数业务应用程序中一样)-选择过程式/函数式,或者至少只将对象用作具有隔离访问器的数据容器。

最后一本——试着读一下:《创造性思维的算法》

一个有用的技巧是将你独特的问题描述与你在现实世界中可以找到的东西联系起来。例如,你正在为一个将席卷全球的复杂医疗保健系统建模。有什么例子你可以随时调用建模吗?

确实。观察旁边的药房是如何运作的,或者医生的房间。

把你的领域问题归结为你能理解的问题;一些你能联想到的东西。

然后,一旦领域内的“玩家”开始出现,你开始对你的代码建模,选择“提供者-消费者”建模方法,即你的代码是模型的“提供者”,而你是“消费者”。

与领域相关并在较高层次上理解它是任何设计的关键部分。