我正在处理一个大型项目(对我来说),它将有许多类,需要可扩展,但我不确定如何规划我的程序以及类需要如何交互。
几个学期前我上了一门OOD课程,从中学到了很多东西;比如编写UML,并将需求文档转换为对象和类。我们也学过序列图但不知怎么的我错过了这节课,我没能记住它们。
在之前的项目中,我尝试使用从课程中学到的方法,但通常在我说“是的,这看起来像我想要的东西”时,我就会以代码结束,我不想再挖掘出新的功能。
我有一本Steve McConnell的《Code Complete》,我经常在这里和其他地方听到它的神奇之处。我读了关于设计的章节,似乎没有得到我想要的信息。我知道他说这不是一个固定的过程,它主要是基于启发式,但我似乎不能把他所有的信息都应用到我的项目中。
那么,在高级设计阶段(在开始编程之前),你要做些什么来确定你需要什么类(特别是那些不基于任何“现实世界对象”的类)以及它们如何相互交互?
我特别感兴趣的是你使用的方法是什么?你遵循什么样的过程,通常会产生一个良好的,干净的设计,将接近最终产品?
我用于初始设计(得到类图)的步骤是:
Requirements gathering. Talk to the client and factor out the use cases to define what functionality the software should have.
Compose a narrative of the individual use cases.
Go through the narrative and highlight nouns (person, place, thing), as candidate classes and verbs (actions), as methods / behaviors.
Discard duplicate nouns and factor out common functionality.
Create a class diagram. If you're a Java developer, NetBeans 6.7 from Sun has a UML module that allows for diagramming as well as round-trip engineering and it's FREE. Eclipse (an open source Java IDE), also has a modeling framework, but I have no experience with it. You may also want to try out ArgoUML, an open source tool.
Apply OOD principles to organize your classes (factor out common functionality, build hierarchies, etc.)
大型项目的问题在于您无法监督组件之间的所有交互。因此,降低项目的复杂性是很重要的。类图和序列图对于这个设计阶段来说太详细了。
首先试着从更高的抽象层次来思考。考虑主要组件及其职责(它们与其他组件的接口),查看一些架构模式以获得灵感(不,不是设计模式,这些太低级了!MVC和多层是架构模式的例子)。
对于相当大的项目,这样的视图应该有大约3-5个组件。
只有这样,你才能放大到某个组件,并尝试设计它。现在我们到了设计模式和类图的层次。试着把注意力集中在项目的这一部分,如果你发现你需要向其他组件之一添加一个职责,就把它添加到你的文档/待办事项列表中。不要在这个时候浪费时间思考它们的含义,因为它们的变化太快了,当设计更加可靠时再进行检查。
此时您不需要完整地设计每个组件,尽管使用一段代码来实现未实现的组件接口并生成简单但有用的响应可能是明智的。这样,您就可以一次开始开发(和设计)一个组件,并对其进行合理的测试。
当然,当新组件完成后,您应该在继续之前测试它们如何(以及是否)相互集成。
简而言之:
采用面向对象和信息隐藏原理,并将其提升到另一个层次!
PS:
在设计的时候画很多草图,就像真正的建筑一样!
PPS:试着从不同的角度来看待这个问题,跳出固有的思维模式(尽管固有的思维模式可能是正确的),与同行讨论可能会非常有用……午餐时你们有话可聊。
恐怕人们不愿意听到这样的回答。无论如何,让我陈述我的观点。
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++优化某些领域。
通过实现语言本身的解释器提供可扩展性。
最后一个特性使软件能够轻松地适应特定用户(包括我自己!)的需求。