Design Patterns Based on Chapter 15 of Bennett
Design Patterns Based on Chapter 15 of Bennett, Mc. Robb and Farmer: Object Oriented Systems Analysis and Design Using UML, (2 nd Edition), Mc. Graw Hill, 2002. 03/12/2001 © Bennett, Mc. Robb and Farmer 2002 1
In This Lecture You Will Learn: · What types of patterns have been identified in software development · How to apply design patterns during software development · The benefits and difficulties that may arise when using patterns © Bennett, Mc. Robb and Farmer 2002 2
Patterns vs. Frameworks n n Frameworks are partially completed software systems that may be targeted at a specified type of application However patterns – are more abstract and general than frameworks – cannot be directly implemented in a particular software environment – are more primitive than frameworks © Bennett, Mc. Robb and Farmer 2002 3
Catalogues & Languages A pattern catalogue is a group of patterns that are related to some extent and may be used together or independently of each other n The patterns in a pattern language are more closely related, and work together to solve problems in a specific domain n © Bennett, Mc. Robb and Farmer 2002 4
Key Principles n Key principles that underlie patterns – abstraction – encapsulation – information hiding – modularization – separation of concerns © Bennett, Mc. Robb and Farmer 2002 5
Key Principles – Coupling and cohesion – Sufficiency – Completeness and primitiveness – Separation of policy and implementation – Separation of interface and implementation – Single point of reference – Divide and conquer Buschmann et al. (1996) © Bennett, Mc. Robb and Farmer 2002 6
Non-functional Properties n Buschmann et al. (1996) identify the important non-functional properties of a software architecture changeability n interoperability n efficiency n reliability n testability n reusability n © Bennett, Mc. Robb and Farmer 2002 7
Pattern Template n n n Name—meaningful that reflects the knowledge embodied by the pattern Problem—description of the problem that the pattern addresses (the intent of the pattern). Context—represents the circumstances or preconditions under which it can occur. Forces—embodied in a pattern are the constraints or issues that must be addressed by the solution Solution—description of the static and dynamic relationships among the components of the pattern © Bennett, Mc. Robb and Farmer 2002 8
Other Aspects of Templates An example of the use of a pattern that serves as a guide to its application n The context that results from the use of the pattern n The rationale that justifies the chosen solution n Related patterns n © Bennett, Mc. Robb and Farmer 2002 9
Other aspects of Templates n n n Known uses of the pattern that validate it (Some suggest that until the problem and its solution have been used successfully at least three times—the rule of three—they should not be considered as a pattern) A list of aliases for the pattern (‘also known as’ or AKA) Sample program code and implementation details (commonly used languages include C++, Java and Smalltalk) © Bennett, Mc. Robb and Farmer 2002 10
GOF Design Patterns n n n Catalogue of 23 design patterns presented by Gamma et al. (1995) patterns known as Gang of Four—hence GOF Patterns Classifies patterns as creational, structural or behavioural Typically address issues concerning changeability, and involve several different aspects: maintainability, extensibility, restructuring and portability © Bennett, Mc. Robb and Farmer 2002 11
Creational Patterns Concerned with the construction of object instances n Separate the operation of an application from how its objects are created n Gives the designer considerable flexibility in configuring all aspects of object creation n © Bennett, Mc. Robb and Farmer 2002 12
Creational Patterns: Singleton n How does one ensure that only one instance of the company class is created? Company company. Name company. Address company. Registration. Number get. Company. Details( ) © Bennett, Mc. Robb and Farmer 2002 13
Creational Patterns: Singleton n Solution—restrict access to the constructor! Company - company. Instance - company. Name - company. Address - company. Registration. Number + get. Company. Instance( ) + get. Company. Details( ) - Company( ) Class-scope (or static) attribute Class-scope (or static) operation Private constructor The use of class-scope operations allows global access © Bennett, Mc. Robb and Farmer 2002 14
Creational Patterns: Singleton Company The operation get. Company. Details() has been moved to the subclasses where it is polymorphically redefined in each. UKCompany - company. Instance - company. Name - company. Address + get. Company. Instance( ) + get. Company. Details( ) - Company( ) The attribute company. Registration. Number has been moved to the subclasses where it is defined differently in each. USACompany French. Company - company. Registration. Number + get. Company. Details( ): String - Uk. Company( ): Uk. Company + get. Company. Details( ): String - USACompany( ): USACompany + get. Company. Details( ): String - French. Company( ): French. Company • Different subclasses of Company can be instantiated as needed, depending on run-time circumstances © Bennett, Mc. Robb and Farmer 2002 15
Creational Patterns: Singleton - unique. Instance - singleton. Data + get. Instance( ) + get. Singleton. Data( ) + singleton. Operation( ) - Singleton( ) Holds object identifier for the Singleton instance Returns object identifier for the unique instance Private constructor — only accessible via get. Instance() General form of Singleton pattern © Bennett, Mc. Robb and Farmer 2002 16
Structural Patterns Concerned with the way in which classes and objects are organized n Offer effective ways of using objectoriented constructs such as inheritance, aggregation and composition to satisfy particular requirements n © Bennett, Mc. Robb and Farmer 2002 17
Structural Patterns: Composite • How can we present the same interface for a media clip whether it is composite or not? Media. Clip play( ) Video. Clip Sound. Clip play( ) © Bennett, Mc. Robb and Farmer 2002 18
Structural Patterns: Composite How can we incorporate composite structures? Ad. Sequence play( ) add. Clip( ) remove. Clip( ) Delegates to the play() operation in the components. get. Child( ) play() is polymorphically redefined 1 * * Video. Clip Sound. Clip play( ) © Bennett, Mc. Robb and Farmer 2002 19
Composite applied to Agate Advert Media. Clip play( ) * {Ordered} add. Clip( ) play()is polymorphically redefined remove. Clip( ) get. Child( ) 1 Video. Clip Sound. Clip Ad. Sequence media. Clip. Collection play( ) add. Clip( ) for all m in media. Clip. Collection m. play() remove. Clip( ) get. Child( ) change. Sequence( ) © Bennett, Mc. Robb and Farmer 2002 Collection of Media. Clip object identifiers Delegates to the play() operation in the components. 20
Composite Pattern General Form Client Component {Ordered} * an. Operation( ) add. Component( ) an. Operation()is polymorphically redefined remove. Component( ) get. Child( ) 1 Leaf Other. Leaf Composite component. Collection an. Operation( ) add. Component( ) for all c in component. Collection c. an. Operation() remove. Component( ) get. Child( ) © Bennett, Mc. Robb and Farmer 2002 Collection of Component object identifiers Delegates to the an. Operation() operation in the components. 21
Behavioural Patterns Address the problems that arise when assigning responsibilities to classes and when designing algorithms n Suggest particular static relationships between objects and classes and also describe how the objects communicate n © Bennett, Mc. Robb and Farmer 2002 22
Behavioural Patterns: State n n n Consider the class Campaign. It has four states—Commissioned, Active, Completed and Paid A Campaign object has different behaviour depending upon which state it occupies Operations have case statements giving this alternative behaviour The class factored into separate components —one for each of its states © Bennett, Mc. Robb and Farmer 2002 23
Behavioural Patterns: State Campaign. State current. State. Identifier add. Advert( ) change. State( ) calc. Costs( ) Contains the object identifier of the current state object Commissioned Active Completed Paid add. Advert( ) calc. Costs( ) State pattern applied to the class Campaign © Bennett, Mc. Robb and Farmer 2002 24
Behavioural Patterns: State A: Campaign C: Campaign D: Campaign : Completed : Commissioned E: Campaign : Active : Paid B: Campaign F: Campaign Some State pattern objects for Agate – note that there are 6 Campaign objects sharing the four State objects. © Bennett, Mc. Robb and Farmer 2002 25
General form of State Pattern Context State operation( ) Concrete. State. A operation( ) Concrete. State. B operation( ) © Bennett, Mc. Robb and Farmer 2002 26
Before Using Patterns n Before using a pattern to resolve the problem ask – Is there a pattern that addresses a similar problem? – Does the pattern trigger an alternative solution that may be more acceptable? – Is there a simpler solution? Patterns should not be used just for the sake of it © Bennett, Mc. Robb and Farmer 2002 27
Before Using Patterns – Is the context of the pattern consistent with that of the problem? – Are the consequences of using the pattern acceptable? – Are there constraints imposed by the software environment that would conflict with the use of the pattern? © Bennett, Mc. Robb and Farmer 2002 28
Using Patterns n After selecting a suitable pattern 1. Read the pattern to get a complete overview 2. Study the Structure, Participants and Collaborations of the pattern in detail 3. Examine the Sample Code to see an example of the pattern in use © Bennett, Mc. Robb and Farmer 2002 29
Using Patterns 4. Choose names for the pattern’s participants (i. e. classes) that are meaningful to the application 5. Define the classes 6. Choose application specific names for the operations 7. Implement operations that perform the responsibilities and collaborations in the pattern © Bennett, Mc. Robb and Farmer 2002 30
Summary In this lecture you have learned about: · What types of patterns have been identified in software development · How to apply design patterns during software development · The benefits and difficulties that may arise when using patterns © Bennett, Mc. Robb and Farmer 2002 31
References n Gamma et al. (1995) (For full bibliographic details, see Bennett, Mc. Robb and Farmer) © Bennett, Mc. Robb and Farmer 2002 32
- Slides: 32