Class Diagram Design: Assigning Responsibilities

Ivan Marsic • Rutgers University 88 • Structurer • Bridge Note that objects almost never play an exclusive role; several roles are usually imparted to different degree in each object. Object Communication Patterns Communication pattern is a message-sending relation imposed on a set of objects. As with any relation, it can be one-to-one or one-to-many and it can be deterministic or random. Some of these are illustrated in Figure 2-14. Object-oriented design, particularly design patterns, is further elaborated in Chapter 5 below.

2.4.3 Why Software Engineering Is Difficult 2

Another key cause is the lack of analytical methods for software design. Software engineers are aiming at optimal designs, but quantitative criteria for optimal software design are largely unknown. Optimality criteria appear to be mainly based upon judgment and experience.

2.5 Software Architecture

Bottom-up design approaches at the local level of objects, reviewed in the previous section, are insufficient to achieve optimal designs, particularly for large systems. A complementary approach are system-level macro-level, global design approaches which help us to “see the forest for the trees.” These approaches partition the system into logical units or follow some global organizational patterns. A A B B P P S 2 S 2 S 1 S 1 S N S N A A B B a b c Figure 2-14: Example object communication patterns. a One-to-one direct messages. b One-to-many untargeted messages. c Via a shared data element. Chapter 2 • Object-Oriented Software Engineering 89 Objects through their relationships form confederations, which are composed of potentially many objects and often have a complex behavior. The synergy of the cooperative efforts among the members creates a new, higher-level conceptual entity. Organizations are partitioned in departments—design, manufacturing, human resources, marketing, etc. Of course, partitioning makes sense for certain size of the organization; partitioning a small organization in departments and divisions does not make much sense. Similarly, our “virtual organization” of objects should be partitioned into software packages. Each package contains a set of logically related classes. System architects may partition an application into subsystems early in design. But subsystems can be discovered later, as the complexity of the system unfolds. Flow Control An important feature of most programming languages is the conditional. This is a statement that causes another statement to execute only if a particular condition is true. One can use simple “sentences” to advise the machine, “Do these fifteen things one after the other; if by then you still haven’t achieved such-and-such, start all over again at Step 5.” Equally, one can readily symbolize a complex conditional command such as: “If at that particular point of runtime, this happens, then do so-and-so; but if that happens, then do such-and-such; if anything else happens, whatever it is, then do thus-and-so.” Note that in the case of IF-THEN-ELSE, the occasion for action is precisely specified. One can also set up “daemons” that spend their lifetime on the lookout for a certain type of event, and do what they have to do whenever a happening of that type occurs. A more flexible IF-THEN-ELSE is to say, “If this happens, use that method to choose an appropriate procedure from this list of procedures,” where the contents of the list in question can vary as the program runs. IF-THEN-ELSE partitions the set of all possible situations in two or more cases. The partition may turn out to be too rigid, there may be some exception cases that were not anticipated and User Interface Layer User Interaction User Authentication Management of Sensors and Devices Archiving Communication w. Police Station Domain Layer Application Logic Technical Services Layer Figure 2-15: Software packages for the case study system. The system has a layered architecture, with three layers indicated. Ivan Marsic • Rutgers University 90 now need to be accounted for. Possibly even by the user A key issue is, How to let the user to “rewire” the pathsflows within the program if a need arises?

2.6 Implementation

This section shows how the above system might be implemented. The reader may wish to review the Java programming refresher in Appendix A below before proceeding. One thing that programmers often neglect is that the code must be elegant and readable. This is not for the sake of the computer which will run the code, but for the sake of humans who will read, maintain, and improve on the original code. I believe that writing good comments is at least as difficult as writing good code. It may be even more important, since comments describe the developer’s intention, while the code expresses only what the developer did. The code that lacks aesthetics and features poor writing style in comments is likely to be a poor quality code. 11 Assume we have an embedded processor with a keypad, wired to other hardware components of the system, as shown in Figure 2-16. The embedded processor accepts “commands” from the computer via a RS-232 serial port and simply passes them on the corresponding device. The many intricacies of serial communication are omitted and the interested reader is directed to the bibliography review at the end of this chapter. The embedded processor may in an advanced design become a full-featured computer, communicating with the main computer via a local area network LAN. 11 On a related note, writing user messages is as important. The reader may find that the following funny story is applicable to software products way beyond Microsoft’s: “There was once a young man who wanted to become a great writer and to write stuff that millions of people would read and react to on an emotional level, cry, howl in pain and anger, so now he works for Microsoft, writing error messages.” [ Source: A Prairie Home Companion, February 3, 2007. Online at: http:prairiehome.publicradio.orgprograms20070203scriptsshowjokes.shtml ] Computer RS-232 Interface cable Keypad and Embedded processor Light bulb Switch Alarm bell Photosensor Figure 2-16: Hardware components for the system implementation.