Chapter 3: Object-Oriented Design
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 3 Ob ject-Oriented Design A cursory explanation of ob ject-oriented programming tends to emphasize the ++ syntactic features of languages suchasC or Delphi, as opp osed to their older, non ob ject-oriented versions, C or Pascal. Thus, an explanation usually turns rather quickly to issues such as classes and inheritance, message passing, and virtual and static metho ds. But such a description will miss the most imp ortant p oint of ob ject-oriented programming, which has nothing to do with syntax. Working in an ob ject-oriented language that is, one that supp orts inheri- tance, message passing, and classes is neither a necessary nor sucient condi- tion for doing ob ject-oriented programming. As we emphasized in Chapters 1 and 2, the most imp ortant asp ect of OOP is the creation of a universe of largely autonomous interacting agents. But how do es one come up with such a system? The answer is a design technique driven by the determination and delegation of resp onsibilities. The technique describ ed in this chapter is termed responsibility- 1 driven design. 3.1 Resp onsibility Implies Noninterference As anyone can attest who can rememb er b eing a child, or who has raised children, resp onsibility is a sword that cuts b oth ways. When you make an ob ject b e it a child or a software system resp onsible for sp eci c actions, you exp ect a certain b ehavior, at least when the rules are observed. But just as imp ortant, resp onsibility implies a degree of indep endence or noninterference. If you tell a child that she is resp onsible for cleaning her ro om, you do not normally stand 1 The past few years have seen a p oli eration of ob ject-oriented design techniques. See the section on further reading at the end of this chapter for p ointers to some of the alternatives. I have selected Resp onsibility-driven design, develop ed by Reb ecca Wirfs- bro ck [Wirfs-Bro ck 1989b , Wirfs-Bro ck 1990 ] b ecause it is one of the simplest, and it facilitates the transition from design to programming. Also in this chapter I intro duce some of the nota- tional techniques made p opular by the Uni ed Mo delling Language, or UML. However, space do es not p ermit a complete intro duction to UML, nor is it necessary for an understanding of subsequent material in the b o ok. 49 50 CHAPTER 3. OBJECT-ORIENTED DESIGN over her and watch while that task is b eing p erformed{that is not the nature of resp onsibility. Instead, you exp ect that, having issued a directive in the correct fashion, the desired outcome will b e pro duced. Similarly, in the owers example from Chapter 1, when Chris gave the request to the Florist to deliver owers to Robin, it was not necessary to stop to think ab out how the request would be serviced. The orist, having taken on the resp onsibility for this service, is free to op erate without interference on the part of the customer Chris. The di erence b etween conventional programming and ob ject-oriented pro- gramming is in many ways the di erence between actively sup ervising a child while she p erforms a task, and delegating to the child resp onsibility for that p erformance. Conventional programming pro ceeds largely by doing something to something else{mo difying a record or up dating an array, for example. Thus, one p ortion of co de in a software system is often intimately tied, by control and data connections, to many other sections of the system. Such dep endencies can come ab out through the use of global variables, through use of p ointer values, or simply through inappropriate use of and dep endence on implementation details of other p ortions of co de. A resp onsibility-driven design attempts to cut these links, or at least make them as unobtrusive as p ossible. This notion might at rst seem no more subtle than the concepts of infor- mation hiding and mo dularity, which are imp ortant to programming even in conventional languages. But resp onsibility-driven design elevates information hiding from a technique to an art. This principle of information hiding b ecomes vitally imp ortant when one moves from programming in the small to program- ming in the large. One of the ma jor b ene ts of ob ject-oriented programming o ccurs when soft- ware subsystems are reused from one pro ject to the next. For example, a sim- ulation manager such as the one we will develop in Chapter 7 mightwork for b oth a simulation of balls on a billiards table and a simulation of sh in a sh tank. This ability to reuse co de implies that the software can have almost no domain-sp eci c comp onents; it must totally delegate resp onsibility for domain- sp eci c b ehavior to application-sp eci c p ortions of the system. The ability to create such reusable co de is not one that is easily learned{it requires exp erience, careful examination of case studies paradigms, in the original sense of the word, and use of a programming language in which such delegation is natural and easy to express. In subsequentchapters, we will present several such examples. 3.2 Programming in the Small and in the Large The di erence b etween the development of individual pro jects and of more siz- able software systems is often describ ed as programming in the small versus programming in the large. Programming in the small characterizes pro jects with the following attributes: Co de is develop ed by a single programmer, or p erhaps by a very small 3.3. WHY BEGIN WITH BEHAVIOR? 51 collection of programmers. A single individual can understand all asp ects of a pro ject, from top to b ottom, b eginning to end. The ma jor problem in the software development pro cess is the design and development of algorithms for dealing with the problem at hand. Programming in the large, on the other hand, characterizes software pro jects with features such as the following: The software system is develop ed by a large team, often consisting of p eople with many di erent skills. There may b e graphic artists, design exp erts, as well as programmers. Individuals involved in the sp eci cation or design of the system may di er from those involved in the co ding of individual com- p onents, who may di er as well from those involved in the integration of various comp onents in the nal pro duct. No single individual can b e con- sidered resp onsible for the entire pro ject, or even necessarily understands all asp ects of the pro ject. The ma jor problem in the software development pro cess is the management of details and the communication of information b etween diverse p ortions of the pro ject. While the b eginning student will usually be acquainted with programming in the small, asp ects of many ob ject-oriented languages are b est understo o d as resp onses to the problems encountered while programming in the large. Thus, some appreciation of the diculties involved in developing large systems is a helpful prerequisite to understanding OOP. 3.3 Why Begin with Behavior? Why b egin the design pro cess with an analysis of b ehavior? The simple answer is that the b ehavior of a system is usually understo o d long b efore any other asp ect. Earlier software development metho dologies those p opular b efore the ad- vent of ob ject-oriented techniques concentrated on ideas suchascharacterizing the basic data structures or the overall structure of function calls, often within the creation of a formal sp eci cation of the desired application. But structural elements of the application can be identi ed only after a considerable amount of problem analysis. Similarly, a formal sp eci cation often ended up as a do cu- ment understo o d by neither programmer nor client. But behavior is something that can b e describ ed almost from the moment an idea is conceived, and often unlike a formal sp eci cation can b e describ ed in terms meaningful to b oth the programmers and the client. Resp onsibility-Driven Design RDD, develop ed by Reb ecca Wirfs-Bro ck, is an ob ject-oriented design technique that is driven by an emphasis on b ehavior at all levels of development. It is but one of many alternative ob ject-oriented 52 CHAPTER 3. OBJECT-ORIENTED DESIGN ' $ P P H P H P P H P H H H H H H Welcome H H H H to the H H P P I IKH P H H H P H H H the H P H H H H H P H H H H H Interactive P H P H H H H H P H H H H Intelligent H H P H H H H H P H H H H H Kitchen P H H H H H H H H H H H Help er H H H H H H H H H H H H Press Return H to b egin H H H H H H & 22222222 222222222 2222222 Figure 3.1: { View of the InteractiveIntelligent Kitchen Help er. design techniques. We will illustrate the application of Resp onsibility-Driven Design with a case study.