Course of Software Engineering II Design Patterns Elements





















































- Slides: 53

Course of Software Engineering II Design Patterns: Elements of Reusable Object – Oriented Software Serena Fabbri 1 Design Patterns

Design Pattern n n n n Introduction: why a design pattern? History of design pattern What is a design pattern How we describe design pattern Classification of desing pattern Examples of design pattern Conclusions Bibliography Design Patterns 2

Why a Design Pattern n Reusability: one of Wasserman’s rules(1996)for an efficient and actual SE discipline n Helping new designers to have a more flexible and reusable design n Improving the documentation and maintenance of existing system by furnishing an explicit specification of class and object interactions and their intent Design Patterns 3

History of Design Pattern n 1979: Christopher Alexander, architect, “The Timeless Way of Building”, Oxford Press n 1987: OOPSLA (Object Oriented Programming System), Orlando, presentation of design pattern to the community OO by Ward Cunningham and Kent Beck n 1995: Group of Four alias E. Gamma, R. Helm, R. Johnson and J. Vlissides : “Design Pattern: Elements of Reusable OO software” Design Patterns 4

What Is a Design Pattern § A design pattern is a descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context § A pattern is made by four elements: § name § problem § solution § consequences Design Patterns 5

Name of Design Pattern § Describe a design problems and its solutions in a word or two § Used to talk about design pattern with our colleagues § Used in the documentation Increase our design vocabulary Have to be coherent and evocative § § Design Patterns 6

Problem n Describes when to apply the patterns n Explains the problem and its context n Sometimes include a list of conditions that must be met before it makes sense to apply the pattern n Have to occurs over and over again in our environment Design Patterns 7

Solution n Describes the elements that make up the design, their relationships, responsibilities and collaborations n Does not describe implementation n Has to be well proven in some projects a concrete design or Design Patterns 8

Consequences n Results and trade-offs of applying the pattern n Helpful for describe design decisions, for evaluating design alternatives n Benefits of applying a pattern n Impacts on a system’s flexibility, extensibility or portability Design Patterns 9

Description of Design Pattern name and classification n contains the essence of pattern succinctly n Become part of your design vocabulary Intent n n What does the pattern do ? What particular problem does it address ? Design Patterns 10

Description of Design Pattern n Motivation n n Illustrate a design problem and how the class and the object structures solve the problem Applicability n In which situations the pattern can be applied? n How can you recognize these situations? Design Patterns 11

Description of Design Pattern n Structure n n Graphical representation of the classes their collaborations in the pattern and Participants n n n Class Objects Responsibilities Design Patterns 12

Description of Design Pattern n Collaborations n n How the participants collaborate to carry out their responsibilities Consequences n n How does the pattern support its objectives? What are the trade-offs and results of using the pattern? Design Patterns 13

Description of Design Pattern n Implementation n Sample Code n Known Uses n Examples of the pattern found in real systems Design Patterns 14

Description of Design Pattern n Related Patterns n n What design patterns are closely related to this one? What are the important differences? Design Patterns 15

Classification of Design Pattern By purpose and by scope n Creational patterns n n Abstract the instantiation process Make a system independent to its realization Class Creational use inheritance to vary the instantiated classes Object Creational delegate instantiation to an another object Design Patterns 16

Classification of Design Pattern n Structural patterns n Class Structural patterns concern the aggregation of classes to form largest structures n Object Structural pattern concern the aggregation of objects to form largest structures Design Patterns 17

Classification of Design Pattern n Behavioral patterns n n Concern with algorithms and assignment of responsibilities between objects Describe the patterns of communication between classes or objects Behavioral class pattern use inheritance to distribute behavior between classes Behavioral object pattern use object composition to distribute behavior between classes Design Patterns 18

Classification of design patterns ( a view) purpose Scope Class Creational Structural Behavioral Factory Method Adapter Interpreter Template Method Object Abstract Factory Adapter Chain of Responsibility Builder Bridge Command Prototype Composite Iterator Singleton Decorator Mediator Façade Memento Flyweight Observer Proxy State Strategy Visitor Design Patterns 19

Creational Pattern n n Singleton n Ensure a class only has one instance n Provide a global point of access to it Abstract Factory: n Provide an interface for creating families of related or dependent objects without specifying their concrete classes Design Patterns 20

Creational Pattern n Factory Method: n n n Define an interface for creating an object but let subclasses decide which class to instantiate Lets a class defer instantiation to subclasses Prototype n n Specify the kinds of objects to create using a prototypical instance Create new objects by copying this prototype Design Patterns 21

Creational Pattern n Builder: n Separate the construction of a complex object from its representation so that the same construction process can create different representations Design Patterns 22

Structural Pattern n Composite n n n Compose objects into tree structures to represent part-whole hierarchies Lets clients treat individual objects and compositions of objects uniformly Decorator n n Attach additional responsibilities to an object dynamically Provide a flexible alternative to subclassing for extending functionality Design Patterns 23

Structural Pattern n Adapter: n n n Convert the interface of a class into another interface clients expect Lets classes work together that couldn’t otherwise because of incompatible interfaces Bridge: n Decouple an abstraction from its implementation so that the two can vary independently Design Patterns 24

Structural Pattern n Façade n n n Provide a unified interface to a set of interfaces in a subsystem Defines an higher-level interface that makes the system easier to use Flyweight n Use sharing to support large numbers of finegrained objects efficiently Design Patterns 25

Structural Pattern n Proxy n Provide a surrogate or placeholder for another object to control access to it Design Patterns 26

Behavioral Pattern n Iterator n n Provide a way to access the elements of an aggregate object without exposing its representation Command n Encapsulate a request as an object, thereby letting you parameterize clients with different requests Design Patterns 27

Behavioral Pattern n Interpreter n n Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language Mediator n n n Define an object that encapsulate how a set of objects interact Promotes loose coupling by keeping objects from referring to each other explicitly Lets you vary their interaction independently Design Patterns 28

Behavioral Pattern n Memento n n Capture and externalize state an object’s internal Observer n Define a one-to-many dependency between objects so when one of them change state all its dependents are updated automatically Design Patterns 29

Behavioral Pattern n State n n n Allow an object to alter its behavior when its internal state changes The object will appear to change its class Visitor n n Represent an operation to be performed on the elements of an object structure Lets you define a new operation without changing the classes of the elements on which operates Design Patterns 30

Behavioral Pattern n Strategy n n n Define a family of algorithms Encapsulate each one Make them interchangeable Lets the algorithms vary independently from clients that use it Chain of responsibilities n n Avoid coupling the sender of a request to its receiver by giving more then one object a chance to handle the request Chain the receiving objects and pass the request along the chain until an object handles it Design Patterns 31

Singleton Pattern n Motivation n n we need to have exactly one instance for a class (ex. Printer spooler) Make the class itself responsible for keeping track of its sole instance The class provide a way to access the instance Applicability n There must be only one instance of a class accessible from a well-known point Design Patterns 32

Singleton Pattern n Structure Singleton Return unique. Instance Static Instance() Singleton. Operation() Get. Singleton. Data() Static unique. Instance Singleton. Data Design Patterns 33

Singleton Pattern n Participants n n Collaborations n n Singleton class Access only through Singleton’s instance operation Consequences n n n Controlled access to sole instance Permits refinement of operation and representation More flexible than class operations Design Patterns 34

Example of Singleton use “Lotteria Algebrica” n We had to have only one instance for class Director. We simply solve our problem using Singleton Pattern Director Static Instance() Given(n_ticket: int): void Error(): void Static Unique. Instance Design Patterns 35

Decorator Pattern n Motivation n n Add responsibilities to individual object not to an entire class conforming the interface of the component decorated Design Patterns 36

Decorator Pattern n Structure Component Operation() Concrete. Component Decorator Operation() Concrete. Decorator. A Operation() Added. State Concrete. Decorator. B Operation() Addedbehavior Design Patterns 37

Decorator Pattern n Participants n n Component n Define the interface for objects that can have responsibilities added to them dinamically Concrete Component n Defines an object to which additional responsibilities can be attached Decorator n Mantains a reference to a Component object and defines an interface that conforms to Component’s interface Concrete. Decorator n Added responsibilities to the component Design Patterns 38

Decorator Pattern n Consequences n n More flexibility than static inheritance Avoids feature-laden classes high up in the hierarchy A decorator and its component are not identical Lots of little objects Design Patterns 39

Example of Decorator n Motivation n If you have a Text. View object that displays text in a Window n Text. View has no scroll bars by default n Text. View has no bord by default … Design Patterns 40

Example of Decorator n Structure a. Border. Decorator component a. Scroll. Decorator component a. Text. View Design Patterns 41

Example of Decorator n Structure Visual. Component Draw() Text. View Decorator Draw() Scroll. Decorator Border. Decorator Draw() Scroll. To() Draw. Border() Scroll. Position Border. Width Design Patterns 42

Iterator Pattern n Also know as n n Cursor Motivation n Provide more way to access to the elements of an aggregate List. Iterator List First() Next() Is. Done() Current. Item Count() Append(Element) Remove(Element) … Index Design Patterns 43

Iterator Pattern n Applicability n n n Access an aggregate object’s contents Support multiple traversals of aggregate objects Provide an uniform interface Design Patterns 44

Iterator Pattern n Structure Aggregate Iterator Client Create. Iterator() First() Next() Is. Done() Current. Item() Concrete. Aggregate Create. Iterator() Concrete. Iterator Return new Concrete. Iterator(This) Design Patterns 45

Iterator Pattern n Participants n n Iterator n Defines interfaces for accessing elements Concrete. Iterator n Implements the Iterator interface n Keeps track of the current position in the traversal of the aggregate Aggregate n Defines an interface for creating an Iterator object Concrete Aggregate n Implements the Iterator creation interface n Return an instance of Concrete. Iterator Design Patterns 46

Iterator Pattern n Consequences n n n Simplify the Aggregate interface More that one traversal can be pending on an aggregate Variations in the traversal of an aggregate Design Patterns 47

Patterns and Frameworks n n n Set of cooperating classes that make up reusable design for abstract a specific class of software. Provides are more than frameworks architectural guidance by partitioning the design into are smaller architectural elements abstract classes and defining their responsibilities and collaborations. A developer customizes a framework are less specialized than to a particular application by subclassing and composing instances of framework classes. Design patterns than frameworks Design patterns frameworks Framework is executable software, design pattern represent knowledge about software Frameworks are the physical realization of one or more software pattern solutions, pattern are instructions for how to implement those solutions Design Patterns 48

Conclusions n n n At present, the software community is using pattern largely for software architecture and design More recently the software community is using pattern also for software development processes and o organizations Several object-oriented software design notations/methods have added support for the modeling and representation of design patterns Design Patterns 49

Night Patterns The coffee mug etched black inside Haunts the worn gray keys. The computer screen amused pearlwhite lights the developer’s hands And his night still life: pencils and books, scattered page of code circled and crossed with notes. Outside, along the river, white headlights flow, Red lights retreat. Design Patterns 50

Night Patterns Inside the agile black box it waits- the Conjuring That lifts alive vined parts, Veins flowing in forces he imagines By circles and crosses, in dozing spells, In whitened warmth upon noon earth, In peach trees by the wall. It waits to pulse the pages Toward life, the patterns of night, to bring The quality, singing soft and hangign low. RICHARD GABRIEL Design Patterns 51

Bibliography For the pattern catalogue: n Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. ADDISON-WESLEY 1995 For the pattern origin: n Cristopher Alexander. The Timeless Way of Building. Oxford Press 1979 For “Night Patterns”: n James O. Coplien, Douglas C. Schmidt. Pattern Languages of Program Design. ADDISONWESLEY 1995 Design Patterns 52

More Information Here you can find some example in Italian of design patterns. n http: //www. ugolandini. net/Patterns. Home. ht ml Here you can find some general information about patterns n n http: //www. mokabyte. it/1998/10/pattern. htm http: //www. c 2. com A complete Web site about pattern n http: //www. enteract. com/~bradapp/docs/pat terns-intro. html Design Patterns 53