Design Patterns - University of...

25
Design Patterns

Transcript of Design Patterns - University of...

Page 1: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns

Page 2: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Software Development Patterns (1)

Patterns: a means for capturing knowledge about problems and successful solutionsFramework: partially completed software system targeted at a particular type of application

Reusable mini-architectureClass extension and operation implementation

Patterns versus frameworksPatterns are more abstract and generalPatterns cannot be directly implemented in a particular software environmentPatterns are more primitive

Page 3: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Software Development Patterns (2)

Collection of patterns: pattern catalogue, pattern language (specific domain, completeness), pattern system (classification scheme and relationships)Key principles underlying patterns

Abstraction, encapsulation, information hiding, modularisation, separation of concerns, coupling and cohesion, sufficiency-completeness and primitiveness, separation of policy and implementation, separation of interface and implementation, single point of reference, divide and conquer

Patterns and non-functional requirementsChangeability, interoperability, efficiency, reliability, testability and reusability

Page 4: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Software Development Patterns (3)

Different kinds of patternsAnalysis patterns: groups of concepts useful in modelling requirements

Example: AccountabilityArchitectural patterns: describe the structure and relationships of major components of a software system

Example: Model-View-ControllerDesign patterns: describe the structure and interaction of smaller software components

Example: SingletonIdioms: patterns that are related to constructs in a specific programming language

Example: Counted pointer in C++

Page 5: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Software Development Patterns (4)

Page 6: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Software Development Patterns (5)

The propagation mechanism

«access» «access»

View A

Controller A Controller B

View B

Model

«access»

«access»

«access»

«access»

«propagate»«propagate»

Page 7: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Software Development Patterns (6)

Page 8: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Software Development Patterns (7)

More patternsBeyond good practice – Anti-patterns: practice that is demonstrably bad including possibly reworked solutions

Example: Mushroom ManagementIsolate developers from users to limit requirement driftSolutions: spiral process development model or involvement of domain experts in the development team

Beyond software developmentArchitecture – AlexanderOrganisational patternsPedagogical patterns

Page 9: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Pattern Templates

Style and structure of pattern descriptionName – meaningfulProblem – intentContext – preconditionsForces – constraintsSolution – static and dynamic relationships among the componentsOther aspects: an example of use, resulting context, rationale of the chosen solution, related patterns, known uses of the pattern (rule of three), aliases, sample code and implementation details

Page 10: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (1)

Gang of Four catalogue classificationScope: class (compile time, static) or object level (runtime, dynamic)Purpose: creational, structural, behaviouralEase of changes by reducing coupling and maximising cohesion

Maintainability – correcting errorsExtensibility – inclusion of new features, removal of unwanted featuresRestructuring – increase flexibilityPortability – executing in different environments (OS, hardware, etc.)

Page 11: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (2)

Creational patterns (construction of instances)Separate object construction from object use

Dynamic or static

Singleton patternEnsures only one instance of a class is created!

Instead of global data, encapsulate the data into an object!Use static operation getInstance()Private constructorCreation on demand!

Extension to accommodate variations

Page 12: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (3)

Page 13: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (4)

Page 14: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (5)

AdvantagesControlled access to the sole instanceNo global variablesThe Singleton class may be subclassedA variation can create a specified number of instances

DisadvantagesPattern introduces additional message passingLimits the application flexibilityDevelopers are tempted to use even when inappropriate

Page 15: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (6)

Structural patterns (organisation of classes and objects)

Inheritance, aggregation, composition

Composite patternRepresent whole-part hierarchies so that both whole and part objects offer the same interface to client objectsSame interface suggests same inheritance hierarchy –polymorphic definition of operations

Page 16: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (7)

Page 17: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (8)

Page 18: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (9)

Behavioural patterns (problems of assigning responsibilities to class and designing algorithms)

Inheritance structures to spread behaviourAggregation structures to build complex behaviour

State patternObjects exhibit different behaviour when their internal stage changes appearing as if the change class at run-timeComplex behaviour is broken down into simpler operation which are allocated to different objects one for each state, and the original object delegates responsibility to the appropriate state objectState transition responsibility either on context or shared

Page 19: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (10)

Advert Preparation

Running Adverts SchedulingconfirmSchedule()

extendCampaign()/modify Budget()

advertsApproved()/authorize()

Active

Survey

Evaluation

surveyComplete()

runSurvey()

Running

Monitoring

campaignCompleted()/prepareFinalStatement()

Page 20: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (11)

Page 21: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (12)

Page 22: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (13)

Page 23: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Design Patterns (14)

Advantages State behaviour is localisedState transitions are made explicitState object can be shared among Context objects

DisadvantagesIf state objects cannot be share among Context objects there is an explosion of objectsProcessing overheads for the creation and deletion of state objectsProcessing overhead from the additional message

Page 24: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

How to use design patterns

Patterns require trainingIssues to consider

Is there a pattern for the problem?Does the pattern trigger a more acceptable solution?Is there a simpler solution?Is the context of the pattern consistent with that of the problem?Are the consequences of using the pattern acceptable?Are any constraints of the software environment in conflict with the use of the pattern?

Pattern application procedureRead the pattern to get a complete overviewStudy the Structure, Participants and Collaborations in detailExamine the Sample CodeChoose names for the participants that are meaningful for the applicationDefine the classesImplement operations that perform the responsibilities and collaboration in the pattern

Pattern mining (pattern writer’s workshop)

Page 25: Design Patterns - University of Strathclydepersonal.cis.strath.ac.uk/sotirios.terzis/classes/52.234_old... · Design Patterns (9) Behavioural patterns (problems of assigning responsibilities

Benefits and Dangers of using patterns

Reuse of generic solutionsReusable design elements

A vocabulary for discussing the problem domainPatterns can limit creativityPatterns may lead to over-designIntroduction of patterns has cost for the organisationIntroduction of patterns requires a reuse culture

More acceptable than code reuseUse with care and planning