Responsibilities and Rewards: Specifying Design Patterns
Total Page:16
File Type:pdf, Size:1020Kb
Responsibilities and Rewards: Specifying Design Patterns Neelam Soundarajan and Jason O. Hallstrom Computer and Information Science Ohio State University, Columbus, OH 43210 e-mail: neelam, hallstro @cis.ohio-state.edu Abstract Second, when a new designer joins the design team, he or she will be able, given prior knowledge and understanding Design patterns provide guidance to system designers on of the specific patterns involved, to get a quicker and more how to structure individual classes or groups of classes, as thorough grasp of how the system is structured, why it be- well as constraints on the interactions among these classes, haves in particular ways, etc. to enable them to implement flexible and reliable systems. But if we are to fully realize these benefits, we must have Patterns are usually described informally. While such in- precise ways to reason about these design patterns that we formal descriptions are useful and even essential, if we want can apply to understand and predict the behavior of systems to be sure that designers precisely and unambiguously un- built using them. Our goal in this paper is to investigate such derstand the requirements that must be met when applying reasoning techniques. In our approach, the specification of a given pattern, and be able to reliably predict the behav- a pattern will consist of two components. The responsibili- iors the resulting system will exhibit, we also need formal ties component will consist of the conditions that a designer characterizations of the patterns. must ensure are satisfied with regards to the structures of the In this paper, we develop an approach to formalizing de- classes, the behaviors of particular methods of the classes, sign patterns. The requirements that a designer must meet and the interactions among various classes. The rewards with respect to the structures of the classes, as well as with component will specify the particular patterns of behavior respect to the behaviors exhibited by the relevant methods, that the resulting system will be guaranteed to exhibit if all are captured in the responsibilities component of the pat- the requirements contained in the responsibilities compo- tern’s specification; the benefits that will result by applying nent are satisfied. the pattern, in terms of specific behaviors that the result- In specifying a pattern, we will start by listing the roles ing system will be guaranteed to exhibit, are captured in the that make up the pattern. For example, in the case of the rewards component. One important aspect of many design Observer pattern [5], which we will use as the running ex- patterns is their flexibility; our approach is designed to en- ample throughout the paper, there are two roles, Subject sure that this flexibility is retained in the formalization of and Observer. This information is already clear from the the pattern. We illustrate the approach by applying it to a standard UML representation of the pattern (Fig. 1). Also standard design pattern. clear from the diagram is the fact that the Notify() method of Subject will invoke the Update() method on each of the observers1. What is not clear is when Notify() will be 1 Introduction called and by whom. This question is addressed in the com- mentary (under “Collaborations”) in the description of the Design patterns [1, 2, 5, 8, 15] are widely regarded as Observer pattern in [5], “. subject notifies its observers one of the most powerful tools in designing OO systems. whenever a change occurs that could make its observers’ Specific patterns provide precise guidance on how to struc- state inconsistent with its own.” But it is not clear how the ture individual classes or groups of classes, as well as the 1We use names starting with uppercase letters such as Subject for interactions among these classes, to implement flexible and roles; and the corresponding lowercase names such as subject to refer reliable solutions in specific contexts. Using design patterns to the individual objects that play these roles. We also use names starting helps system designers in two ways: First, design patterns with uppercase letters for patterns, classes, and methods; member variables represent the collective wisdom and experience of the com- will have names starting with lowercase letters. In some cases the name of a pattern is also used for one of the roles in that pattern, as in the case of munity, so the application of the relevant patterns should the Observer role of the Observer pattern. In such cases, the context will lead to higher quality systems with predictable behavior. make clear whether we are talking about the role or the pattern. Proceedings of the 26th International Conference on Software Engineering (ICSE’04) 0270-5257/04 $20.00 © 2004 IEEE subject will know when its state has become inconsistent Most previous work on patterns has focused on identify- with that of one or more observers. Indeed, what does it ing patterns and specifying them informally, usually in the mean to say that the subject state has become inconsistent style introduced by [5]. There has been some work related with that of an observer? How is this to be determined? to formal aspects of patterns; these have dealt with such Perhaps more to the point from the point of view of a de- things as modeling patterns using temporal action systems, signer, what exactly are the requirements that the designer categories, etc. To our knowledge, there has not been much must ensure are met in order to apply the pattern as in- work that has tried to specify patterns by formally defin- tended? And what exactly can the designer expect in re- ing the exact program properties that the use of the pattern turn for meeting these requirements? For the Observer pat- will ensure, or the requirements that a system adopting a tern, the return is that if the pattern is applied as intended, given pattern should meet. We will briefly address indi- the view of each observer will remain consistent with the vidual items of related work later in the paper but one im- subject’s state – again consistent in what sense? As we will portant notion that we should mention here is Reenskaug’s see, the kind of formal specifications that we develop in this [13] role models. Role models allow us to focus on the in- paper will provide precise answers to these questions. teractions among the various objects participating in what area of concern There is an inherent risk in formalizing design patterns he calls an . For example, an area of con- [14] in that flexibility, which is the hallmark of many de- cern for a system could be the question of how a group of observer subject sign patterns, may be lost. For example, in the case of objects keep track of the changes in a . Observer, if we adopt one definition for the notion of in- An important point here is that a given object playing the Observer consistency, the pattern may not be usable in systems that role of an may have many other aspects to it that have a different notion of this concept. As we will see, our are not relevant to this area of concern. Although Reen- approach, while requiring us to provide a precise character- skaug’s work is not concerned with formal specifications, ization of the pattern, also enables us to retain the flexibility his ideas on role models have heavily influenced our ap- contained in the pattern. proach to specifying patterns formally. As we will see, the major part of the pattern’s specification is the precise speci- There is a similarity between our approach to specifying fication of the various roles in that pattern. design patterns and the standard design-by-contract (DBC) In the next section, we develop the basic ideas of our ap- approach [9, 10, 11] using pre/post-conditions and invari- proach. We analyze the different components that must be ants to specify classes and methods. In DBC, the meth- included in the specification of a pattern and some possi- ods of each class have a precisely defined pre-condition and ble variations. In Section 3, we apply our approach to the post-condition, and the class as a whole has an invariant as- Observer pattern and develop a specification for this pat- sociated with it. Whenever invoking a method of a class, tern. In Section 4 we consider related work. In the final it is the caller’s responsibility to make sure that the corre- section, we summarize our approach, reiterate the advan- sponding pre-condition is satisfied. In return, the caller is tages of having formal specifications of patterns, and con- assured that when control returns, the post-condition will sider pointers for future work. be satisfied (assuming that the method is implemented cor- rectly). In our formalization of patterns, the specification will specify a set of requirements that the system designer 2 An Approach to Specifying Patterns wishing to use this pattern must meet; in return, the designer is assured that the invariant associated with the pattern will Consider the Observer pattern [5] in Fig. 1. This pat- be satisfied (as long as the ‘pattern instance’ –a notion to tern is intended to be used when one or more (“observer”) be defined shortly– continues to exist). In a sense, pat- objects are interested in the state of a (“subject”) ob- terns are a natural extension of classes; while classes fo- ject. When an object wishes to become an observer of a cus on how individual objects should behave, patterns are subject,itinvokestheAttach() method of that object; and concerned with interactions among a group of objects and when it is no longer interested in that subject, it invokes its relations between their respective states.