Download presentation
Presentation is loading. Please wait.
Published byEugene Garrison Modified over 9 years ago
1
1 Design Patterns G.Ramesh Babu
2
© Bennett, McRobb and Farmer 2002 2 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
3
© Bennett, McRobb and Farmer 2002 3 Patterns vs. Frameworks n Frameworks are partially completed software systems that may be targeted at a specified type of application n However patterns –are more abstract and general than frameworks –cannot be directly implemented in a particular software environment –are more primitive than frameworks
4
© Bennett, McRobb and Farmer 2002 4 Catalogues & Languages n 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
5
© Bennett, McRobb and Farmer 2002 5 Key Principles n Key principles that underlie patterns –abstraction –encapsulation –information hiding –modularization –separation of concerns
6
© Bennett, McRobb and Farmer 2002 6 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)
7
© Bennett, McRobb and Farmer 2002 7 Non-functional Properties n Buschmann et al. (1996) identify the important non-functional properties of a software architecture n changeability n interoperability n efficiency n reliability n testability n reusability
8
© Bennett, McRobb and Farmer 2002 8 Pattern Template n Name—meaningful that reflects the knowledge embodied by the pattern n Problem—description of the problem that the pattern addresses (the intent of the pattern). n Context—represents the circumstances or precon- ditions under which it can occur. n Forces—embodied in a pattern are the constraints or issues that must be addressed by the solution n Solution—description of the static and dynamic relationships among the components of the pattern
9
© Bennett, McRobb and Farmer 2002 9 Other Aspects of Templates n 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
10
© Bennett, McRobb and Farmer 2002 10 Other aspects of Templates 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) n A list of aliases for the pattern (‘also known as’ or AKA) n Sample program code and implementation details (commonly used languages include C++, Java and Smalltalk)
11
© Bennett, McRobb and Farmer 2002 11 GOF Design Patterns n Catalogue of 23 design patterns presented by Gamma et al. (1995) patterns known as Gang of Four—hence GOF Patterns n Classifies patterns as creational, structural or behavioural n Typically address issues concerning changeability, and involve several different aspects: maintainability, extensibility, restructuring and portability
12
© Bennett, McRobb and Farmer 2002 12 Creational Patterns n 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
13
© Bennett, McRobb and Farmer 2002 13 Creational Patterns: Singleton n How does one ensure that only one instance of the company class is created? Company companyName companyAddress companyRegistrationNumber getCompanyDetails( )
14
© Bennett, McRobb and Farmer 2002 14 Creational Patterns: Singleton n Solution—restrict access to the constructor! Company - companyInstance - companyName - companyAddress - companyRegistrationNumber + getCompanyInstance( ) + getCompanyDetails( ) Class-scope (or static) attribute (or static) operation - Company( ) Private constructor The use of class-scope operations allows global access Class-scope
15
© Bennett, McRobb and Farmer 2002 15 Creational Patterns: Singleton Company - companyInstance - companyName - companyAddress + getCompanyInstance( ) + getCompanyDetails( ) - Company( ) + getCompanyDetails( ):String - UkCompany( ):UkCompany - companyRegistrationNumber UKCompany + getCompanyDetails( ):String - USACompany( ):USACompany - companyRegistrationNumber USACompany + getCompanyDetails( ):String - FrenchCompany( ):FrenchCompany - companyRegistrationNumber FrenchCompany The attribute companyRegistrationNumber has been moved to the subclasses where it is defined differently in each. The operation getCompanyDetails() has been moved to the subclasses where it is polymorphically redefined in each. Different subclasses of Company can be instantiated as needed, depending on run-time circumstances
16
© Bennett, McRobb and Farmer 2002 16 Creational Patterns: Singleton + getInstance( ) Singleton - uniqueInstance - singletonData + getSingletonData( ) + singletonOperation( ) - Singleton( ) Holds object identifier for the Singleton instance Returns object identifier for the unique instance Private constructor — only accessible via getInstance() General form of Singleton pattern
17
© Bennett, McRobb and Farmer 2002 17 Structural Patterns n Concerned with the way in which classes and objects are organized n Offer effective ways of using object- oriented constructs such as inheritance, aggregation and composition to satisfy particular requirements
18
© Bennett, McRobb and Farmer 2002 18 Structural Patterns: Composite MediaClip play( ) VideoClip play( ) SoundClip play( ) How can we present the same interface for a media clip whether it is composite or not?
19
© Bennett, McRobb and Farmer 2002 19 Structural Patterns: Composite Delegates to the play() operation in the components. play() is polymorphically redefined VideoClip play( ) SoundClip play( ) AdSequence play( ) addClip( ) removeClip( ) getChild( ) * * 1 How can we incorporate composite structures?
20
© Bennett, McRobb and Farmer 2002 20 Composite applied to Agate Collection of MediaClip object identifiers for all m in mediaClipCollection m.play() Delegates to the play() operation in the components. play() is polymorphically redefined AdSequence mediaClipCollection play( ) addClip( ) removeClip( ) getChild( ) changeSequence( ) * 1 MediaClip play( ) addClip( ) removeClip( ) getChild( ) VideoClip play( ) SoundClip play( ) Advert {Ordered}
21
© Bennett, McRobb and Farmer 2002 21 Composite Pattern General Form anOperation( ) addComponent( ) removeComponent( ) getChild( ) Collection of Component object identifiers for all c in componentCollection c.anOperation() Delegates to the anOperation() operation in the components. anOperation() is polymorphically redefined Composite componentCollection anOperation( ) addComponent( ) removeComponent( ) getChild( ) * 1 Component Leaf anOperation( ) OtherLeaf anOperation( ) Client {Ordered}
22
© Bennett, McRobb and Farmer 2002 22 Behavioural Patterns n 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
23
© Bennett, McRobb and Farmer 2002 23 Behavioural Patterns: State 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 n Operations have case statements giving this alternative behaviour n The class factored into separate components —one for each of its states
24
© Bennett, McRobb and Farmer 2002 24 Behavioural Patterns: State Contains the object identifier of the current state object CampaignState addAdvert( ) calcCosts( ) Commissioned addAdvert( ) calcCosts( ) Campaign currentStateIdentifier addAdvert( ) changeState( ) calcCosts( ) Active addAdvert( ) calcCosts( ) Completed addAdvert( ) calcCosts( ) Paid addAdvert( ) calcCosts( ) State pattern applied to the class Campaign
25
© Bennett, McRobb and Farmer 2002 25 Behavioural Patterns: State A:Campaign B D:Campaign C:Campaign :Commissioned :Active :Completed :Paid E:Campaign F Some State pattern objects for Agate – note that there are 6 Campaign objects sharing the four State objects.
26
© Bennett, McRobb and Farmer 2002 26 General form of State Pattern State operation( ) ConcreteStateA operation( ) ConcreteStateB operation( ) Context operation( )
27
© Bennett, McRobb and Farmer 2002 27 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
28
© Bennett, McRobb and Farmer 2002 28 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?
29
© Bennett, McRobb and Farmer 2002 29 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
30
© Bennett, McRobb and Farmer 2002 30 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
31
© Bennett, McRobb and Farmer 2002 31 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
32
© Bennett, McRobb and Farmer 2002 32 References n Gamma et al. (1995) (For full bibliographic details, see Bennett, McRobb and Farmer)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.