banaras hindu university - inflibnet...

35
Banaras Hindu University

Upload: others

Post on 24-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Banaras Hindu University

Page 2: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

A Course on

Software Reuse by Design Patterns and Frameworks

Page 3: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

by

Dr. Manjari Gupta Department of Computer Science

Banaras Hindu University

Page 4: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Lecture 4

Introduction to Design Patterns

Page 5: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ During the late 1970s ∗ Christopher Alexander ∗ different structures that were designed to solve the

same problem ∗ similarities among designs that were of high quality-

towns and buildings ∗ a context, a set of forces and a solution

∗ a set of correlated patterns- pattern language

Architectural patterns

Page 6: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ key to manage complexity ∗ One of the current successful abstractions

∗ design pattern ∗ birth of patterns in software field

∗ by Ward Canningham and Kent Beck ∗ first five patterns dealt with the design of user

interfaces ∗ concept of patterns is being adapted to many other

fields

Abstraction

Page 7: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ description of a proven (successful or efficient) solution to a recurring problem within a context

∗ informal descriptions of tested solutions to recurring problems ∗ a pattern can be reused as a building block for better software

construction and designer communication ∗ Properties inherent in the definition of patterns

∗ determine if the description of a design is pattern or not. ∗ artifacts of software reusability ∗ published mostly within collections or catalogs. ∗ well-known and widely used

∗ Gang-of-Four (GoF) patterns [Gamma95] and J2EE patterns

Design Pattern

Page 8: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ describes solution to a recurring problem in software design phase.

∗ descriptions of recurring pattern of communicating classes that form a common solution to a common design problem.

∗ describes when it applies, whether it can be applied in view of other design constraints, and the consequences and trade-offs of its use

Design Pattern

Page 9: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ make object oriented design more flexible, elegant and ultimately reusable

∗ A designer can apply them immediately to design problems

∗ help designers reuse successful designs ∗ It captures experts’ design decisions

Design Patterns

Page 10: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ improve the quality of software e.g. ∗ reusability, extensibility, and maintainability

∗ reflect the designers’ intents ∗ useful in both development of new, and comprehension of

existing object-oriented designs ∗ Patterns capture previous design experiences and make it

available to other designers ∗ Form a more flexible foundation for reuse

Importance of design patterns

Page 11: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ also serve as a communication medium among software designers

∗ promising technique for achieving widespread reuse of software architectures ∗ capture static and dynamic structures and

collaborations of components ∗ aid the development of other reusable artefacts

Importance of design patterns

Page 12: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ have common template that make it easy to learn and understand them.

∗ has four essential elements: ∗ The name of the pattern ∗ The Problem and its context ∗ The solution ∗ Consequences

∗ for different domains different template are needed to describe solution more elegantly in that domain

Elements of design patterns

Page 13: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ Model: stores the application’s state. ∗ View: interprets data in the model and presents it to

the user. ∗ Controller: processes user input, and either updates

the model or displays a new view. ∗ Originally designed for graphical environments ∗ adapted for use in other areas of programming

Model-View-Controller (MVC) Pattern

Page 14: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Overview of the MVC Pattern [Crawford06]

Controller

Views Model

+updateModel(): +chooseView(): +…():

View

User controls

Model

State Data

updates uses

Chooses

Sends user input

*

* 1

1

Page 15: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ each component is simple and self-contained, with well-defined interfaces

∗ components can vary independently and are therefore easier to share and reuse

∗ used in ET++ framework ∗ not suitable for simple user interface toolbox

components

MVC Pattern

Page 16: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ encapsulates possible variations of a given algorithm ∗ allows the system to invoke the more appropriate

algorithm depending on a given context

Strategy design pattern

Page 17: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Strategy design pattern Cont…

Page 18: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Explicit representation of the pattern hot-spots

Page 19: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Instantiation diagram for strategy pattern

Page 20: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ provides a uniform way to create object trees that represent part-whole hierarchies.

∗ allows new kinds of components to be added to the system without disturbing the behavior of the composite objects

Composite design pattern

Page 21: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Composite design pattern

Page 22: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Highlighting the configuration step

Page 23: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Composite pattern instantiation diagram

Page 24: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ Different categorization schemes ∗ Purpose

∗ creational, structural, or behavioral ∗ Scope

∗ whether the pattern applies primarily to classes or to objects.

Design patterns’ classification

Page 25: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

• relationship between patterns ∗ Pattern X uses pattern Y in its solution, ∗ Pattern X is similar to pattern Y, and ∗ Pattern X can be combined with pattern Y.

Zimmer Classification of Design Pattern [hammouda04(b)]

Page 26: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Zimmer Classification of Design Pattern [hammouda04(b)]

Page 27: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ method calls involved in the GoF patterns were classifiable by three orthogonal properties ∗ The relationship of the target object instance to the

calling object instance. ∗ The relationship of the target object’s type to the calling

object’s type ∗ The relationship between the method signatures of the

caller and callee.

Based on involved method calls

Page 28: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Classification system for design patterns [Gamma93]

Page 29: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ informal textual notation; ∗ formal textual notation, that is, a formalism or a

programming language; ∗ graphic notation.

Specification of Design patterns

Page 30: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ to better represent (to explicitly represent pattern hotspots) configuration patterns.

∗ explicitly represents pattern hotspots ∗ The representation of configuration patterns are enhanced by:

∗ explicitly representing the pattern instantiation, and ∗ being abstract enough to simplify the diagrams and allow several

implementation approaches to be used.

Extension to the UML design notation [Fontoura]

Page 31: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

∗ Changing the class diagrams to explicitly identify pattern hot-spots and application classes;

∗ Defining instantiation diagrams, which represent the pattern instantiation process.

∗ a new stereotype- Application class ∗ two new tagged values- variable and extensible ∗ incomplete constraint

UML Extension

Page 32: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the
Page 33: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Highlighting the configuration step

Page 34: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the

Composite pattern instantiation diagram

Page 35: Banaras Hindu University - INFLIBNET Centreeacharya.inflibnet.ac.in/data-server/eacharya-documents/...Banaras Hindu University Lecture 4 Introduction to Design Patterns * During the