Coupling of Design Patterns: Common Practices and Their Benefits
Total Page:16
File Type:pdf, Size:1020Kb
TO APPEAR IN Proc. Computer Software & Applications Conf. (COMPSAC 2001),OCT. 2001. 1 Coupling of Design Patterns: Common Practices and Their Bene®ts William B. McNatt James M. Bieman Computer Science Department Colorado State University Fort Collins, CO 80525 USA McNatt: +1-303-971-2271, Bieman: +1-970-491-7096 [email protected], [email protected] Abstract roles in both patterns. A change in parts of one pattern can affect the behavior of the other pattern. Object-oriented (OO) design patterns de®ne collections of interconnected classes that serve a particular purpose. A design pattern is a structural unit in a system built out of pat- terns, not unlike the way a function is a structural unit in a procedural program or a class is a structural unit in an OO system designed without patterns. When designers treat pat- terns as structural units, they become concerned with issues such as coupling and cohesion at a new level of abstraction. We examine the notion of pattern coupling to classify how designs may include coupled patterns. We ®nd many exam- ples of coupled patterns; this coupling may be ªtightº or ªlooseº, and provides both bene®ts and costs. We qualita- tively assess the goodness of pattern coupling in terms of ef- fects on maintainability, factorability, and reusability when patterns are coupled in various ways. Keywords: Design patterns, object-oriented design, design quality, coupling. 1. Introduction Design patterns are architectural units, just as classes are Figure 1. Overlapping patterns: Abstract Fac- implementation units. We can view a system as a collection tory and Visitor. of interacting patterns and independent classes. Pattern in- stance attributes are potentially measurable in a manner sim- ilar to class attributes. Thus, we can examine notions such as pattern coupling. Although we have not found the notion of pattern cou- Pattern coupling results from connections between pat- pling discussed in the literature, we found numerous exam- terns. Common classes can connect two patterns. They play ples of coupled patterns. In this paper, we study the 23 pat- roles in more than one pattern by referencing common ob- terns described in Gamma et al [8]. The design pattern liter- jects, and by using methods in another pattern. Figure 1 ature includes many descriptions of actual implementations shows a UML class model of two coupled patterns from the that make use of patterns and pattern languages. Published well-known book by Gamma et al [8]. In this example, an examples include coupled patterns that are in practical use instance of an Abstract Factory pattern is coupled with an today. Review of the pattern coupling techniques used in instance of a Visitor pattern through shared classes. Inter- practice can reveal characteristics of this coupling, and help face ASTNode, classes AST, StmtNode, and DeclNode play us understand when and how to couple patterns. 2. Approach Singleton: ªEnsure a class only has one instance, and provide a global point of access to it.º We examined the available literature describing speci®c Structural Patterns. design pattern applications to ®nd examples of pattern cou- pling using the following process: Adapter: ªConvert the interface of a class into another interface clients expect.º 1. Survey recent literature concerning design pattern ap- plications for examples of interconnected patterns. Bridge: ªDecouple an abstraction from its implemen- tation so that the two can vary independently.º 2. Compile a list of all example instances of Gamma et al Composite: ªCompose objects into tree structures to design patterns found in the literature. represent part-whole hierarchies.º 3. Create a sub-list of all groups of patterns used as pairs Decorator: ªAttach additional responsibilities to an or multiple sets of interacting Gamma et al patterns. object dynamically.º 4. Group the list into categories of coupling types (tightly Facade: ªProvide a uni®ed interface to a set of inter- versus loosely coupled) and interaction types (intersec- faces in a subsystem.º tion, composition, or embedding) with a rationale for Flyweight: ªUse sharing to support large numbers of each grouping. ®ne-grained objects ef®ciently.º 5. Evaluate the identi®ed instances of coupled patterns in Proxy: ªProvide a surrogate or placeholder for an- terms of desirable software qualities (maintainability, other object to control access to it.º factorability, reusability, and ease of implementation) Behavioral Patterns. with rationale for each grouping. Chain of Responsibility: ªAvoid coupling the sender 6. Analyze the data to better understand the pattern cou- of a request to its receiver ... Chain the receiving pling techniques that, in general, tend to contribute to objects and pass the request along the chain until design ªgoodnessº and which ones should be used only an object handles it.º to satisfy speci®c application needs. Command: ªEncapsulate a request as an object, We included only those coupled pattern groups that consist thereby letting you parameterize clients with dif- exclusively of patterns de®ned in Gamma et al. ferent requests...º Interpreter: Given a language, de®ne a representation 3. Study Design Patterns for its grammar along with an interpreter.º Iterator: Provide a way to access the elements of an The study pattern set consists of the 23 Gamma et al aggregate object sequentially without exposing OO patterns. The patterns are categorized into their three its underlying representation.º main groups and listed below with descriptions quoted from Mediator: ªDe®ne an object that encapsulates how a Gamma et al [8]: set of objects interact.º Creational Patterns. Memento: ªCapture and externalize an object's inter- nal state so that the object can be restored to this Factory Method: ªDe®ne an interface for creating an state later.º object, but let subclasses decide which class to in- stantiate.º Observer: ªDe®ne a one-to-many dependency be- tween objects so that when one object changes Abstract Factory: ªProvide an interface for creating state, all its dependents are noti®ed and updated.º families of related or dependent objects without State: Allow an object to alter its behavior when its specifying their concrete classes.º internal state changes.º Builder: ªSeparate the construction of a complex ob- Strategy: ªDe®ne a family of algorithms, encapsulate ject from its representation so that the same con- each one, and make them interchangeable.º struction process can create different representa- tions.º Template: ªDe®ne the skeleton of an algorithm in an operation, deferring some steps to subclasses.º Prototype: ªSpecify the kinds of objects to create us- ing a prototypical instance, and create new ob- Visitor: ªRepresent an operation to be performed on jects by copying this prototype.º the elements of an object structure.º 2 These patterns represent all of the patterns that we targeted Industry Case Studies of new designs. Four studies in this investigation. Although other patterns are docu- examine new or recent development projects which mented in the collection of papers used in this study, we did deliberately makes use of patterns during the design not include them in this study. Thus, the pattern combina- phase [5, 14, 16, 17]. tions discovered are limited to those that can be derived from pairs or multiples of patterns from Gamma et al [8]. 5. Pattern Occurrences 4. Papers Included in the Study The occurrence of patterns in the selected papers was also quite evenly distributed among the Gamma et al patterns. Several criteria were applied to the set of papers to sup- Table 1 lists pattern frequencies. Some patterns appear more port consistent analyses of pattern examples: frequently, but all patterns are used to some extent. Cer- 1. Each paper must have contained at least 1 pattern from tain patterns appear to be more desirable based on their fre- Gamma et al [8] to be included in the study. quent usage. Most of the more popular patterns support in- terface abstraction and reusability. One example is the Ob- 2. Papers were not excluded from the study if they did not server pattern. However, the popularity of some of the pat- contain coupled pattern combinations to add to the total terns may be due to the simplicity of the pattern. For exam- data set. This allowed a realistic depiction of the actual ple, the Singleton pattern is simple, but useful, and is often prevalence of coupling in practice. embedded in other more complex patterns. 3. Individual pattern recognition was based on the actual Our data includes far more instances of Behavioral pat- documentation using the name of the pattern. terns than either Creational or Structural patterns. However, there are approximately twice as many Creational patterns 4. We identify pattern combinations either from actual di- in the Gamma et al book than the other two pattern types. rect references in the research papers or from their indi- The frequency totals actually correspond very well with the rect recognition after a review of the pattern structures quantity of pattern types in each subtype. It appears that pat- presented in the papers. tern usage is much more dependent on the individual pattern type than its particular subtype grouping. 5. The classi®cation of the patterns into groups accord- ing to coupling type (loose vs tight) and interaction type (embedded, interactive, and composite) was ac- 6. Pattern Coupling complished via paper inspection by a single reviewer without subsequent independent validation. We identi®ed all coupled patterns. Such coupled patterns The criteria, especially Criteria 1, allowed us to reject consist of all pattern pairs or multiples that are connected. many papers as sources for data for this study. Approxi- We classi®ed the sets of connected patterns in terms of loose mately one third of the papers that we found describing pat- vs.