Docsity

Understanding the Relationship between Design Patterns and Frameworks: A Thesis, Essays (university) of Object Oriented Programming

Toggle sidebar
bg1
Applying Design Patterns to the Development o f
Object-Oriented Frameworks
By
Gang Xu
Submitted in partial fulfillment o f the requirem ents
for the degree o f Master of Computer Science
at
Dalhousie University
Halifax, Nova Scotia
March 2005
© Copyright by Gang Xu, 2005
Re pro du ce d with pe rmis sion of the copy rig ht owne r. Furthe r rep rod ucti on proh ibited wit hou t perm issio n.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57

Partial preview of the text

Download Understanding the Relationship between Design Patterns and Frameworks: A Thesis and more Essays (university) Object Oriented Programming in PDF only on Docsity!

Applying Design Patterns to the Development of

Object-Oriented Frameworks

By

Gang Xu

Submitted in partial fulfillment of the requirements for the degree o f Master o f Computer Science

at

Dalhousie University Halifax, Nova Scotia March 2005

© Copyright by Gang Xu, 2005

Library and Archives Canada Published Heritage Branch 395 W ellington StreetOttawa ON K1A 0N Canada

Bibliotheque et Archives Canada Direction du Patrimoine de I'edition 395, rue W ellingtonOttawa ON K1A 0N Canada Your file Votre reference ISBN: 0-494-00927- Our file Notre reference ISBN: 0-494-00927-

NOTICE:

The author has granted a non exclusive license allowing Library and Archives Canada to reproduce, publish, archive, preserve, conserve, communicate to the public by telecommunication or on the Internet, loan, distribute and sell theses worldwide, for commercial or non commercial purposes, in microform, paper, electronic and/or any other formats.

AVIS:

L'auteur a accorde une licence non exclusive permettant a la Bibliotheque et Archives Canada de reproduire, publier, archiver, sauvegarder, conserver, transmettre au public par telecommunication ou par I'lnternet, preter, distribuer et vendre des theses partout dans le monde, a des fins commerciales ou autres, sur support microforme, papier, electronique et/ou autres formats.

The author retains copyright ownership and moral rights in this thesis. Neither the thesis nor substantial extracts from it may be printed or otherwise reproduced without the author's permission.

L'auteur conserve la propriete du droit d'auteur et des droits moraux qui protege cette these. Ni la these ni des extraits substantiels de celle-ci ne doivent etre imprimes ou autrement reproduits sans son autorisation.

In compliance with the Canadian Privacy Act some supporting forms may have been removed from this thesis. While these forms may be included in the document page count, their removal does not represent any loss of content from the thesis.

Conformement a la loi canadienne sur la protection de la vie privee, quelques formulaires secondaires ont ete enleves de cette these. Bien que ces formulaires aient inclus dans la pagination, il n'y aura aucun contenu manquant.

i * i

Canada

List of Figures

  • Chapter 1 Introduction Acknowledgements xi - 1.1 Background - 1.2 Motivation and objective - 1.3 Thesis outline
  • Chapter 2 Design Patterns
    • 2.1 What are design patterns?
    • 2.2 Where did they come from?.
      • 2.3 GoF patterns
    • 2.4 Underlying principles of design patterns
    • 2.5 Problems that design patterns solve
    • 2.6 Benefits o f design patterns
      • 2.7 Summary
  • Chapter 3 Object-Oriented Framework Development - 3.1 What are frameworks? - 3.2 Overview o f widely used frameworks - 3.3 Characteristics of frameworks - 3.4 Frameworks vs. other reuse techniques - 3.4.1 Frameworks vs. design patterns - 3.4.2 Frameworks vs. components - 3.4.3 Frameworks vs. class libraries - 3.5 Hot spots in white-box and black-box frameworks - 3.5.1 Hot spots and frozen spots - 3.5.2 White-box and black-box frameworks
    • 3.6 Hooks and essential framework construction patterns
      • 3.6.1 Unification and Separation patterns
      • 3.6.2 Recursive Combination patterns
    • 3.7 Hooks as name indicators of GoF patterns
    • 3.8 Hot-spot-driven development process
      • 3.8.1 The process
      • 3.8.2 Hot spot cards
    • 3.9 Summary
  • Chapter 4 Case Study: Development of a Framework for Genetic Algorithms
    • 4.1 Introduction to genetic algorithms
    • 4.2 Framework design goals
    • 4.3 Domain analysis
    • 4.4 Hot spots and design of the framework
      • 4.4.1 The list o f hot spots
      • 4.4.2 “Population dynamics” handling...........................................................................
      • 4.4.3 “Termination criteria” handling
      • 4.4.4 “Progress notification” handling...........................................................................
      • 4.4.5 “Individual evaluation” handling...........................................................................
      • 4.4.6 “Fitness scaling” handling
      • 4.4.7 “Individual selection” handling
      • 4.4.8 “Individual encoding” handling
      • 4.4.9 “Crossover operation” handling
      • 4.4.10 “Mutation operation” handling
    • 4.5 Implementation of the framework
      • 4.5.1 Default parameters
      • 4.5.2 Default plug-in components for hot spots
      • 4.5.3 GaStatisticsInfo struct
    • 4.6 An example of instantiating the framework
    • 4.7 Summary
  • Chapter 5 Conclusion
    • 5.1 Insight into this research
    • 5.2 Future w ork
    • 5.3 Conclusion
  • References.................................................................................................................................
  • Appendix-A Figures of Sample GoF Design Patterns
  • Figure 1 A framework looks like a “virtual engine”
  • Figure 2 Specialization of a sample white-box framework
  • Figure 3 Specialization of a sample black-box framework
  • Figure 4 (a) Unification and (b) Separation of template and hook classes
  • Figure 5 Recursive combinations of template and hook classes
  • Figure 6 Hot-spot-driven framework development process
  • Figure 7 Layout of hot spot card
  • Figure 8 CRC card of class G A
  • Figure 9 CRC card of class Population
  • Figure 10 CRC card o f class Individual
  • Figure 11 Domain model for genetic algorithms
  • Figure 12 Hot spots in genetic algorithms
  • Figure 13 Hot spot card for “population dynamics”
  • Figure 14 Design diagram for “population dynamics” hot spot
  • Figure 15 Hot spot card for “termination criteria”
  • Figure 16 Design diagram for “termination criteria” hot spot
  • Figure 17 Hot spot card for “progress notification”
  • Figure 18 Design diagram for “progress notification” hot spot
  • Figure 19 Hot spot card for “individual evaluation”
  • Figure 20 Design diagram for “individual evaluation” hot spot
  • Figure 21 Hot spot card for “fitness scaling”
  • Figure 22 Design diagram for “fitness scaling” hot spot
  • Figure 23 Hot spot card for “individual selection”
  • Figure 24 Design diagram for “individual selection” hot spot
  • Figure 25 Hot spot card for “individual encoding”
  • Figure 26 Design diagram for “individual encoding” hot spot
  • Figure 27 Hot spot card for “crossover operation”
  • Figure 28 Design diagram for “crossover operation” hot spot
  • Figure 29 Hot spot card for “mutation operation”
  • Figure 30 Design diagram for “mutation operation” hot spot
  • Figure 31 The Template Method pattern
  • Figure 32 The Strategy pattern
  • Figure 33 The Observer pattern
  • Figure 34 The Abstract Factory pattern
  • Figure 35 The Prototype pattern

Acknowledgements

This thesis was written under the supervision of Dr. Morven Gentleman, to whom I am grateful for his guidance and help. Throughout the process of my thesis writing, I received many valuable suggestions from him. His advice about thesis organization and writing helped me to manage my research and thesis writing. His ideas about frameworks and genetic algorithms also greatly inspired me. I would also like to thank Dr. Anthony Cox and Dr. Peter Hitchcock, who took their valuable time to review my work, provide detailed comments and challenge my ideas. Finally, I am grateful to my wife and parents, for their everlasting love and support.

xi

1

Chapter 1 Introduction

1.1 Background

The idea of software reuse has been present being since the very beginning of software development history. In its early years, software reuse was mainly concemed with the reuse of code and supported by subroutine libraries. Since the 1980s, object-oriented techniques have been becoming more and more popular because of their powerful support for software reuse in a number of ways. Software reusability is greatly improved by object-oriented features such as data abstraction, encapsulation, inheritance, polymorphism and late binding. Based on object-oriented techniques, design patterns and object-oriented frameworks are two important software reuse technologies.

Design patterns support the reuse of software design. They describe proven successful solutions to commonly recurring design problems in a particular context. They came into being as documentation of expertise from expert software designers. Among hundreds of design patterns [1], the design patterns [2] introduced by Gamma et al. (also called the “Gang of Four” or GoF) are the most widely studied and used by software developers to improve the quality o f their design. The primary benefit that design patterns bring to a software product is flexibility and maintainability.

Object-oriented frameworks [3] (here after “frameworks” for simplicity) are a higher level of software reuse technique that have been becoming increasingly popular in contemporary s oftware development. T hey a re h ighly u seful f or r educing t he c ost a nd improving the quality o f software by reusing proven successful design as well as implementation. Framework development differs from traditional object-oriented software development. Traditional object-oriented development focuses on a single problem in an application domain and usually doesn’t put much emphasis on software extensibility. However, extensibility and flexibility are the key issues in framework development. Framework developers have to thoroughly analyze the whole application

3

1.3 Thesis outline

This thesis is organized as follows: Chapter 2 introduces and studies design patterns, their underlying principles, how they solve problems, and the benefits that they provide. GoF design patterns are primarily discussed in this chapter. Chapter 3 studies the development of object-oriented frameworks. Frameworks and their characteristics, how frameworks are compared with design patterns and other reuse techniques, white-box and black-box frameworks, essential construction patterns of frameworks, how to find GoF patterns for framework design, and hot-spot-driven development process are discussed in this chapter. Chapter 4 describes the development of a framework for genetic algorithms as a case study. I first make a brief introduction to genetic algorithms and then illustrate the whole development process and design considerations. An example of how to specialize the framework demonstrates how application development effort is reduced by the reuse of domain analysis, design and code. Finally, Chapter 5 presents the conclusion, what I have learned from this study, and suggestions for future research directions.

4

Chapter 2 Design Patterns

2.1 What are design patterns?

Christopher Alexander, an architect, first studied patterns in buildings and communities and developed the idea of pattern language to enable people to design their own buildings and communities [4]. Alexander used a pattern to describe a reoccurring problem in construction environment, and the core of the solution to the problem. In such a way, people can reuse the solution over and over without having to work it out for themselves. This kind of mechanism for design reuse can also be applied to the development of object-oriented software. For the reuse of object-oriented designs, we describe design solutions in terms of classes, interfaces and object collaborations instead o f walls, doors and windows. The core of both kinds of design patterns has been proven to be successful solutions to specific problems in a context [2].

Design patterns describe common structures of communicating components for solving general design problems in particular contexts [2]. They generally consist of four essential elements: 1) A pattern name that highly abstracts a design with regards to the problem, the solution and consequences. It enables easier thinking about designs and communications with others. 2) A problem that describes the context for when and where to use the pattern. It may express specific design problems, an inflexible design, or a set of conditions to apply the pattern. 3) A solution that describes classes and objects that compose the design, including their relationships, responsibilities and collaborations. It is generally applicable for many different situations rather than specific for a particular concrete design. 4) A set of consequences that indicate the advantages, drawbacks, and trade-offs of applying a pattern. They a re very valuable for evaluating design solution alternatives and being aware of benefits and costs of a pattern.

A design pattern is neither about a concrete design implementation which can be reused as is nor about a complex domain-specific design for an application or subsystem. Instead,

6

generality for application framework development, I put my research effort to the study of GoF patterns.

Because there are a total of 23 GoF design patterns, classifying them into different families gives us a way to organize them, understand them better and find them faster. Gamma et al. classify their design patterns according to two criteria, purpose and scope (see Table 1 which is adopted from [2]). The purpose criterion classifies GoF patterns according to what they do. The three classifications are creational, structural, and behavioral patterns. Creational patterns deal with the process o f object creation. Structural patterns concern the composition of classes and object. Behavioral patterns emphasize how classes or objects communicate and how to distribute responsibilities. The scope criterion classifies GoF patterns according to their primary applicability to classes or objects. Class patterns are about the relationships of classes and their subclasses. Such relationships are implemented through class inheritance. They are static and cannot be changed at run-time. Object patterns emphasize object relationships, which are changeable at run-time and more dynamic.

Purpose Creational Structural Behavioral

Class Factory Method^ Adapter^ InterpreterTemplate Method

Scope Object

Abstract Factory Builder Prototype Singleton

Adapter Bridge Composite Decorator Facade Proxy

Chain of Responsibility Command Iterator Mediator Memento Flyweight Observer State Strategy Visitor Table 1 Classification of GoF patterns by purpose and scope

7

2.4 Underlying principles of design patterns

The construction of design patterns is based on several fundamental principles such as abstraction, information hiding, indirection, low coupling and high cohesion. Making these principles clear helps us to understand design patterns and how expert designers think, and how to produce a good software design. The following is a discussion about these underlying principles.

  1. Abstraction. Abstraction is a fundamental principle people use to deal with complexity. According to Grady Booch [9], abstraction is the essential characteristics o f an object which make it different from other kinds o f objects, and provides a crisp conceptual boundary in terms of the viewers’ perspective. Abstraction exists in several forms such as entity abstraction, behavior abstraction and coincidental abstraction [10]. Many design patterns, such as the Abstract Factory and the Composite, utilize this principle.

  2. Information hiding. Information hiding is often archived by encapsulation, which is one of the fundamental features of object-oriented programming [11]. By hiding implementation details from clients, we can better handle software complexity and reduce coupling between components. For example, the Proxy and Bridge patterns hide object presentation and implementation information from clients to avoid cascading effect o f changes. Information hiding fosters the non-functional properties of software, such as changeability and reusability.

  3. Polymorphism. Polymorphism is a fundamental principle in software design to allow similar behavior variations [12], Enabled by the abstraction of similar or related services, polymorphism allows different objects to share a common interface but have different implements. With polymorphism, extensions for new behaviors can be easily added and new implementation can be introduced without affecting clients. Many design patterns such as the Adapter, Strategy and Proxy rely on a polymorphism mechanism.

9

benefits us in understanding the role design patterns play in software development. Gamma et al. [2] described the following design problems and how they are solved by design patterns.

1) Bridging the gap between the analysis model and the design model. Objects are the building blocks of object-oriented programs. The decomposition of a system into objects is a hard part of object-oriented design. Object-oriented analysis methodologies, such as use cases, CRC (for Class, Responsibility, and Collaboration) cards [13] and domain modeling, help designers find objects in the real world. However, during the transformation o f an analysis model into a design model, w e s truggle t o find o bjects t hat h ave n o corresponding o nes i n t he r eal world. Design patterns help bridge this gap between the analysis model and the design model.

  1. Determining the size and number of objects. An object-oriented program usually consists of different numbers of objects o f different sizes. Design patterns take this issue into account and provide design solutions for objects at different level of granularity. For example, the Facade pattern considers a complete subsystem as an object while the Flyweight pattern supports high volume of objects at the smallest granularity.

  2. Defining the interfaces of objects. As a fundamental mechanism o f object- oriented programming, interfaces are the only way to access objects. The separation of the definition of an interface from its different implementations allows the substituting o f objects that have identical interfaces at run-time. This is known as polymorphism, one of the features of object technology. Design patterns i dentify t he k ey e lements and t he t ypes o f d ata t ransferred t hrough an interface, and thus, help to determine what to put in the definition o f the interface. Design patterns also describe requirements of the relationship between interfaces.

  3. Defining the implementation of objects. Objects are implemented by defining classes. Classes define objects’ internal data and the implementation of their

10 operations, while class inheritance provides a mechanism to reuse the functionality of existing classes. However, implementation dependency comes with class inheritance. Clients are aware of the classes that implement the objects they are using. This kind of dependency can be reduced by interface inheritance if care is used when inheriting from an abstract class which specifies a shared interface. Design patterns follow this principle and offer different ways to associate an interface with its implementation transparently at instantiation.

  1. Deciding the reuse mechanism to be used. Object-oriented design is hard. Object-oriented design with high flexibility and reusability is even harder. Design patterns take reusability as one o f their fundamental goals and achieve reusability by utilizing object composition and class inheritance mechanisms.

a. Preference for object composition rather than class inheritance. Object composition and class inheritance, respectively referred to as black-box and white-box reuse, are both common object-oriented reuse techniques. Class inheritance is defined by sub-classing statically at compile-time. Users are required to be aware o f the internals of parent class. It is easy to modify the implementation being reused. However, the reused implementation cannot be changed at run-time (except by using a pure abstract parent class) and there lies the implementation dependency of subclasses upon the parent class. Object composition is dynamically defined by keeping references to existing objects at run-time. Composed objects have carefully defined interfaces and users have no knowledge of the internals of objects being composed. Object composition offers benefits including reduced implementation dependencies, better class encapsulation, less classes and less complex class hierarchies. In practice, class inheritance is used to assist object composition by making it convenient to create new kinds of objects which can be composed with old ones.