patterns roberto damiani mendes. roteiro definition; definition; architecture patterns; architecture...

49
Patterns Patterns Roberto Damiani Mendes Roberto Damiani Mendes

Upload: edwina-walters

Post on 17-Jan-2016

246 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

PatternsPatterns

Roberto Damiani MendesRoberto Damiani Mendes

Page 2: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

RoteiroRoteiro

• Definition;Definition;

• Architecture Patterns;Architecture Patterns;

• Design Patterns;Design Patterns;

• GRASP Patterns;GRASP Patterns;

Page 3: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

MOTIVATIONMOTIVATION

• Reuse;Reuse;

• Common Language;Common Language;

Page 4: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

DefinitionDefinition

• Chirstopher Alexander says: Chirstopher Alexander says: “Each patterns describes a “Each patterns describes a problem which occurs over and problem which occurs over and over again in our enviroment, and over again in our enviroment, and then describes their core of the then describes their core of the solution to that problem, in such solution to that problem, in such way that you can use this solution way that you can use this solution a million times over, without even a million times over, without even doing it the same way twice”.doing it the same way twice”.

GOFGOF

Page 5: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

DefinitionDefinition

Patterns help you build on collective Patterns help you build on collective experience of experience of skilledskilled software enginer. software enginer. They capture They capture existingexisting, , well-provenwell-proven experience em software development experience em software development and help to promote and help to promote goodgood design design practisepractise. Every patterns deals with a . Every patterns deals with a specific, specific, recurringrecurring problem in the problem in the design or implementation of a sofware design or implementation of a sofware system. Patterns can be used to system. Patterns can be used to contruct software architectures with contruct software architectures with specific properties [Buschmann]. specific properties [Buschmann].

Page 6: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Essential ElementsEssential Elements

• A patterns has four essential A patterns has four essential elementselements

1.1. Patterns name;Patterns name;

2.2. Problem;Problem;

3.3. Solution;Solution;

4.4. Consequences.Consequences.

Page 7: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Pattern NamePattern Name

• It’s a handler we can use to It’s a handler we can use to describe a design problem, its describe a design problem, its soltutions, and consequences in a soltutions, and consequences in a word or two.word or two.

• Finding good names has been one Finding good names has been one of the hardest parts of developing of the hardest parts of developing our catalog. our catalog.

GOFGOF

Page 8: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

ProblemProblem

• It describes when to apply the It describes when to apply the pattern. It explains the problem pattern. It explains the problem and its context.and its context.

Page 9: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

SolutionSolution

• It describes the elements that It describes the elements that make up the design, their make up the design, their relationships, responsibilities, and relationships, responsibilities, and collaborations.collaborations.

Page 10: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

CONSEQUENCESCONSEQUENCES

The consequences are the The consequences are the resultsresults and and trade-offstrade-offs of applying the of applying the patterns.patterns.

Page 11: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

ARCHITECTURAL ARCHITECTURAL PATTERNSPATTERNS

• Express fundamental structural Express fundamental structural organization schemas for organization schemas for software systems. They provide software systems. They provide a set of predefined subsystems, a set of predefined subsystems, specific their resposibilities, and specific their resposibilities, and include rules and guidelines for include rules and guidelines for organizing the relationships organizing the relationships between them [Buschmann].between them [Buschmann].

• Architectural patterns represent Architectural patterns represent the highest-level patterns.the highest-level patterns.

Page 12: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Pattern: Layers (Problem)Pattern: Layers (Problem)

• Source code changes are rippling throughout the Source code changes are rippling throughout the system – many part of systems are highly coupled;system – many part of systems are highly coupled;

• Application logic is intertwined with user interface , Application logic is intertwined with user interface , and so can not be reused with a different interface, and so can not be reused with a different interface, nor distributed to another processing node;nor distributed to another processing node;

• Potentially general technical services or business Potentially general technical services or business logic is intertnwined with more application-specfic logic is intertnwined with more application-specfic logic, and so can not reused, distributed to another logic, and so can not reused, distributed to another node, or easily replaced with different node, or easily replaced with different implementation;implementation;

• There is high coupling across different areas There is high coupling across different areas concern. It is thus difficult to divide the work along concern. It is thus difficult to divide the work along clear bound aries for different developers;clear bound aries for different developers;

• Due to the high coupling and mixing of concerns, it Due to the high coupling and mixing of concerns, it is laborious and costly to evolve the applicattion’s is laborious and costly to evolve the applicattion’s functionality, scale up the system, or update it to functionality, scale up the system, or update it to use new technologies.use new technologies.

Page 13: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Pattern: Layers (Solution)Pattern: Layers (Solution)

• Organize the large-scale logical Organize the large-scale logical structure of a system into discrete structure of a system into discrete layers of distinct, related layers of distinct, related responsibilities, with a clean, responsibilities, with a clean, cohesive separation of concerns such cohesive separation of concerns such that the “lower” layers are low-level that the “lower” layers are low-level and general services and higher and general services and higher layers are more applications specific;layers are more applications specific;

• Collaboration and coupling, is from Collaboration and coupling, is from higher to lower layers; lower-to-higher to lower layers; lower-to-higher layer coupling is avoided.higher layer coupling is avoided.

Page 14: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Pattern: LayersPattern: Layers

Page 15: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Inter-Layer and Inter Inter-Layer and Inter Package CouplingPackage Coupling

Page 16: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Inter-Layer and Inter Inter-Layer and Inter Package CouplingPackage Coupling

Page 17: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

Inter-Layer and Inter-Inter-Layer and Inter-Package Interaction Package Interaction

ScenariosScenarios

Page 18: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

DESIGN PATTERNSDESIGN PATTERNS

• Design Patterns are medium-Design Patterns are medium-scale patterns. They are smaller scale patterns. They are smaller in scale than architectural in scale than architectural patterns, but are at a higher patterns, but are at a higher level than the programming level than the programming language-specific idiom.language-specific idiom.

Page 19: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

CLASSIFICATIONCLASSIFICATION

• PurposePurpose: should reflect what a : should reflect what a pattern does.pattern does.– CreationalCreational ->concern the process ->concern the process

of object creation.of object creation.– StructuralStructural ->category deal with the ->category deal with the

composition of classes or objects;composition of classes or objects;– BahaviouralBahavioural ->characterize the ->characterize the

ways in which classes or objects ways in which classes or objects interact and distribute interact and distribute responsibilities.responsibilities.

Page 20: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

CLASSIFICATIONCLASSIFICATION

• ScopeScope: specify whether a : specify whether a pattern applies primarily to pattern applies primarily to classes or to object.classes or to object.

– ObjectObject -> deal with object -> deal with object relationships;relationships;

– ClassClass -> deal with relationships -> deal with relationships between classes and their between classes and their subclasses.subclasses.

Page 21: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

CLASSIFICATIONCLASSIFICATION

GOFGOF

Page 22: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

ADAPTERADAPTER

• Convert the interface of a class Convert the interface of a class into another interface clients into another interface clients expect. Adapter lets classes expect. Adapter lets classes work together that couldn’t work together that couldn’t otherwise because of otherwise because of incompatible interfaces.incompatible interfaces.

Page 23: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

STRUCTURESTRUCTURE

Page 24: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

EXAMPLEEXAMPLE

Page 25: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

FACADEFACADE

• Provide a unified interface to a Provide a unified interface to a set of interfaces in a subsystem. set of interfaces in a subsystem. Facade defines a higher-level Facade defines a higher-level interface that makes the interface that makes the subsystem easier to usesubsystem easier to use

Page 26: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

STRUCTURESTRUCTURE

Page 27: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

EXAMPLEEXAMPLE

Page 28: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

COMPOSITECOMPOSITE

• Composite lets clients treat Composite lets clients treat individual objects and individual objects and compositions of objects compositions of objects uniformlyuniformly

Page 29: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

STRUCTURE STRUCTURE

Page 30: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

EXAMPLEEXAMPLE

Page 31: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

DESIGN OBJECTDESIGN OBJECT

• After After identifyingidentifying your your requirements and creating a requirements and creating a domain model, then domain model, then addadd methods to the software classes, methods to the software classes, and and definedefine the messaging the messaging between the objects to fulfill the between the objects to fulfill the requeriments. [Larman]requeriments. [Larman]

Page 32: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

DESIGN OBJECTDESIGN OBJECT

• Decide Decide whatwhat method belong method belong wherewhere, and , and howhow the objects the objects should interact, is terribly should interact, is terribly important and anything important and anything but but trivialtrivial.[Larman].[Larman]

Page 33: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

GRASPGRASPGGeneral eneral RResponsibilitity esponsibilitity AAssignment ssignment SSoftware oftware

PPatternsatterns

• The GRASP Patterns are a learning The GRASP Patterns are a learning aid to help one aid to help one understandunderstand essencial object design, and apply essencial object design, and apply design reasoning in a methodical, design reasoning in a methodical, rational, explainable way. rational, explainable way.

• This approach to undestanding This approach to undestanding and using design principles is and using design principles is based on based on patterns of assigning patterns of assigning responsibilities.responsibilities.

Page 34: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

RESPONSIBILITYRESPONSIBILITY• The UML define a responsibility The UML define a responsibility

as “ a contract or obligation of a as “ a contract or obligation of a classifier”[2];classifier”[2];

• They are assigned to classes of They are assigned to classes of objects during object design;objects during object design;

• The skilfull assignment of The skilfull assignment of responsibilities is extremely responsibilities is extremely important in object design;important in object design;

• Determining and assignment of Determining and assignment of responsibilities often occurs responsibilities often occurs during the creation of during the creation of interaction diagrams, and interaction diagrams, and certainly during programmingcertainly during programming

Page 35: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

RESPONSIBILITY x RESPONSIBILITY x METHODMETHOD

• Responsibility is not same thing Responsibility is not same thing as a method, but method are as a method, but method are implemented to fulfill implemented to fulfill responsibilities.responsibilities.

Page 36: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

GRASP PATTERNSGRASP PATTERNS

• They describe fundamental They describe fundamental principles of object design and principles of object design and responsibility assignments, responsibility assignments, expressed as patterns.expressed as patterns.

GRASP Patterns:GRASP Patterns:1.1. Information Expert;Information Expert;2.2. Creator;Creator;3.3. High Cohesion;High Cohesion;4.4. Low Coupling;Low Coupling;5.5. Controller;Controller;6.6. Polymorphism;Polymorphism;7.7. Fabrication;Fabrication;8.8. Indirection;Indirection;9.9. Protected Variactions;Protected Variactions;

Page 37: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

INFORMATION EXPERTINFORMATION EXPERT

ProblemProblem: What is a principle of : What is a principle of assingment responsibilities to assingment responsibilities to objects?objects?

SolutionSolution: Assign a responsibility : Assign a responsibility to the information expert. The to the information expert. The class that has the class that has the informationinformation necessary to fulfill the necessary to fulfill the resposibility.resposibility.

Page 38: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

CREATORCREATORProblemProblem: Who should be responsible for : Who should be responsible for

creating a new instance os some creating a new instance os some class?class?

SolutionSolution:Assign class B the resposibility :Assign class B the resposibility to create an instance of class A if one to create an instance of class A if one or more of the following is true:or more of the following is true:

• B B aggregatesaggregates A objects; A objects;

• B B cotainscotains A objects; A objects;

• B B records instancesrecords instances of A objects; of A objects;

• B B closely usesclosely uses A object; A object;

• B B has the initializing data that will be has the initializing data that will be passedpassed to A when it is created(thus B to A when it is created(thus B is an Expert with respect to creating A)is an Expert with respect to creating A)

Page 39: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

LOW COUPLINGLOW COUPLING

ProblemProblem: How to support low : How to support low dependency, low change impact, dependency, low change impact, and increase reuse?and increase reuse?

SolutionSolution: Assign a resposibility so : Assign a resposibility so that coupling reamains low.that coupling reamains low.

Page 40: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

HIGH COHESIONHIGH COHESION

ProblemProblem:How to keep complexity :How to keep complexity manageable?manageable?

SolutionSolution: Assign a responsibility : Assign a responsibility so that cohesion remains high;so that cohesion remains high;

Page 41: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

CONTROLLERCONTROLLER

•ProblemProblem: : WhoWho should be should be responsible for handling an input responsible for handling an input system event?system event?

Page 42: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

CONTROLLERCONTROLLER

SolutionSolution:Assign the responsibility :Assign the responsibility for receiving or handling a for receiving or handling a system event message to a system event message to a class representing one of the class representing one of the following choices:following choices:

• Represents the overall system, Represents the overall system, device, or subsystem(facade device, or subsystem(facade controller);controller);

• Represents a use case scenario Represents a use case scenario within which the system event within which the system event occurs.occurs.

Page 43: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

POLYMORPHISMPOLYMORPHISM

PROBLEMPROBLEM: How to handle : How to handle alternativesalternatives based on type? How based on type? How create pluggable software create pluggable software components?components?

SolutionSolution: When related alternatives : When related alternatives or behaviors vary by type(class), or behaviors vary by type(class), assign responsibility for the assign responsibility for the bahavior – using bahavior – using polymorphic polymorphic operationsoperations – to the types for which – to the types for which the behavior varies.the behavior varies.

Page 44: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

FABRICATIONFABRICATION

ProblemProblem: : WhatWhat object should have the object should have the responsibility when you do not want to responsibility when you do not want to violateviolate High Cohesion and Low High Cohesion and Low Coupling, or other goals, but solutions Coupling, or other goals, but solutions offered by Expert (for example) are offered by Expert (for example) are not not appropriateappropriate??

SolutionSolution:Assign an highly cohesive set of :Assign an highly cohesive set of responsibilities to an artificial or responsibilities to an artificial or convenience class that does not convenience class that does not represent a problem domain concept – represent a problem domain concept – something made up, to support high something made up, to support high cohesion, low coupling, and reuse.cohesion, low coupling, and reuse.

Page 45: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

INDIRECTIONINDIRECTION

ProblemProblem: Where to assign a : Where to assign a responsibility, to avoid direct responsibility, to avoid direct coupling between two (or more) coupling between two (or more) things? How to de-couple objects so things? How to de-couple objects so that low coupling is supported and that low coupling is supported and reuse potencial remains higher?reuse potencial remains higher?

SolutionSolution: Assign the responsibility to : Assign the responsibility to an intermediate objetc to mediate an intermediate objetc to mediate between other componets or services between other componets or services so that they are not directly coupled.so that they are not directly coupled.

Page 46: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

PROTECTED VARIATIONSPROTECTED VARIATIONS

ProblemProblem: How to design objects, : How to design objects, subsystems, and systems so subsystems, and systems so that the variations or instanbility that the variations or instanbility in these elements does not have in these elements does not have an undesirable impact on other an undesirable impact on other elements?elements?

SolutionSolution: Identify point of : Identify point of predicted variation or instability; predicted variation or instability; assign responsibilities to create assign responsibilities to create a stable interface around them.a stable interface around them.

Page 47: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

IDIOMSIDIOMS

• Idioms are low-level patterns Idioms are low-level patterns specific to a programming specific to a programming language.language.

• An idiom describes how to An idiom describes how to implement particular aspects of implement particular aspects of components or the relationships components or the relationships between them with the features between them with the features of the given languageof the given language

Page 48: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP
Page 49: Patterns Roberto Damiani Mendes. Roteiro Definition; Definition; Architecture Patterns; Architecture Patterns; Design Patterns; Design Patterns; GRASP

REFERENCESREFERENCES

• [3] Larman, G., Applying UML [3] Larman, G., Applying UML and PATTERNS – Second Edition.and PATTERNS – Second Edition.