• 面向对象设计的技巧[Object Oriented Design Tips]1


    Stay close to problem domain

    Design is a process of modeling the problem domain into programming constructs. Object oriented design simplifies the design process by maintaining a one-to-one mapping between problem domain objects and software objects. To succeed in object oriented design, keep your design as close as possible to problem domain objects. The interactions between your objects should mirror interactions between corresponding problem domain objects.

    Problem domain objects is basically an object that can be found in the problem itself. For example, when developing a text editor real-world objects would be, Paragraph, Sentence, Word, ScrollBar, TextSelection etc. While developing a call processing module, the objects might be Call, Ringer, ToneDetector, Subscriber etc. 

    靠近问题领域

    设计是一个把问题领域建模到程序结构的过程。面向对象的设计通过维持一个一对一的问题领域和软件对象之间的映射,让设计过程简单化。要成功的进行面向对象的设计,应当让你的设计尽可能的靠近问题领域对象。你设计的对象之间的相互关系应该与问题领域对象之间的相互关系一致。

    问题领域对象本质上就是可以在问题本身中找到的对象。例如,当你开发一个文本编辑器的时候,真实世界的对象可能是段落、句子、词、滚动条、文本选择器等;让你开发一个呼叫过程模块时,对象可能是呼叫者、闹铃、音频探测器、被呼叫者等。

    ----------------------------------------------------------

    Object discovery vs. object invention

    The first step in object oriented analysis is to discover the objects that can be directly identified from the problem itself. In many cases objects can be identified  from the requirements. Objects discovered from the problem statement are extremely important. These objects will be the core objects in the design.

    The next stage in object design is to "invent" objects. These objects are needed to "glue" together objects that have been identified during object discovery. Invented objects generally do not correspond to anything tangible in the problem domain. They are inventions of programmers to simplify design.

    Consider the following statement from the requirements:

    The circuit controller shall support digital and analog circuits. The circuit controller shall contain 32 DSPs. When the circuit controller receives a request to setup a circuit, it shall allocate a DSP to the circuit.

    We discover the following objects from the requirement:

    • CircuitController
    • DigitalCircuit
    • AnalogCircuit
    • DSP

    We invent the following objects based on our knowledge of the manager design pattern:

    • DSPManager: Manages the 32 DSPs on the circuit controller
    • CircuitManager: Manages the digital and analog circuits

    We invent a Circuit base class for DigitalCircuit and AnalogCircuit by filtering properties that are common to DigitalCircuit and AnalogCircuit objects.

    The relationship between the classes also follows from the requirement. CircuitController class contains DSPManager and CircuitManager classes. The CircuitManager contains an array of Circuit class pointers. The DSPManager contains an array of DSP objects.

    发现对象 vs 创造对象

    面向对象设计中的第一步就是在问题本身中发现对象。在大多数情况下,对象可以直接在需求中提取出来。在问题的描述中发现对象是非常重要的,往往这些对象就是设计中的核心对象。

    对象设计的下一步就是“创造”对象。这些创造的对象需要和提取出来的对象“粘和”起来。被创造出来的对象和问题领域本身并没有直接的关联,这些对象是程序员创造出来,以便简化设计用的。

    考虑下面在需求中的一句话:

    电路控制器应该支持数字信号和模拟信号。电路控制器应该处理32位的DSP。当电话控制器接收一个建立电路的请求之后,它应该为这个电路分配一个DSP

    我们在这段需求中可以发现下面几个对象:

    • CircuitController
    • DigitalCircuit
    • AnalogCircuit
    • DSP

    然后我们根据管理设计模式的知识创造下面的对象:

    • DSPManager:在电路控制器中管理32位的DSP
    • CircuitManager:管理数字电路和模拟电路。

    我们为DigitalCircuitAnalogCircuit创造一个基类Circuit,这个基类拥有DigitalCircuitAnalogCircuit中共有的属性。

    这些类的联系也符合我们的需求:

    CircuitController拥有DSPManagerCircuitManager类。CircuitManager拥有一组Circuit类的引用;同时DSPManager也拥有一组DSP对象。

  • 相关阅读:
    JAVA 大数相加 POJ 1503
    Gao the Grid ZOJ 3647 数三角形
    为Layouts中的页面应用站点母版页的方法
    跨页面传值
    SharePoint Designer + InfoPath 无代码工作流设计实例
    自定义moss主题样式
    修改sharepoint文档库文件类型显示图标
    Workflow成功案例展示BingoSoft
    Walkthrough: 应用EventHandle开发的一个发邮件的小项目
    InfoPath + Workflow + MOSS
  • 原文地址:https://www.cnblogs.com/Dabay/p/778594.html
Copyright © 2020-2023  润新知