CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff,...

30
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 ك ب ي ص ن س ن ت و لا رة خ لا ه الدار ا ل ل ا اك ت ا ما& ي ف غ ت ب و ا ا& ب ب ن, الد م

Transcript of CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff,...

Page 1: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

CS251 – Software EngineeringLectures 18: Intro to DP

Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others

1

و ابتغ فيما آتاك الله الدار اآلخرة و ال تنس نصيبك من الدنيا

Page 2: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Outline

Introduction to Design Patterns

Template Method Pattern

Decorator Pattern

Page 3: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Resources

The Swing Tutorialhttp://docs.oracle.com/javase/tutorial/uiswing/index.html

List of patterns: http://www.oodesign.com/

Page 4: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

16-4

Page 5: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

1. Background1

Search for recurring successful designs – emergent designs from practice (via trial and error)Supporting higher levels of reuse (i.e., reuse of designs) is quite challengingDescribed in Gama, Helm, Johnson, Vlissides 1995 (i.e., “gang of 4 book”)Based on work by Christopher Alexander (an Architect) on building homes, buildings and towns.

Page 6: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Background2

Design patterns represent solutions to problems that arise when developing software within a particular context. E.g., problem/solution pairs within a given contextDescribes recurring design structuresDescribes the context of usage

Page 7: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Background2

Design patterns represent solutions to problems that arise when developing software within a particular context. E.g., problem/solution pairs within a given contextDescribes recurring design structuresDescribes the context of usage

Page 8: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Background3

Patterns capture the static and dynamic structure and collaboration among key participants in software designsPatterns facilitate reuse of successful software architectures and designs.

Page 9: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Elements of Design Patterns

Design patterns have four essential elements:– Pattern name– Problem– Solution– Consequences

Page 10: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Pattern Name

A handle used to describe:– a design problem– its solutions– its consequences

Increases design vocabularyMakes it possible to design at a higher level of abstractionEnhances communication“The Hardest part of programming is coming up with good variable [function, and type] names.”

Page 11: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Problem

Describes when to apply the patternExplains the problem and its contextMay describe specific design problems and/or object structuresMay contain a list of preconditions that must be met before it makes sense to apply the pattern

Page 12: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Solution

Describes the elements that make up the– design– relationships– responsibilities– collaborations

Does not describe specific concrete implementationAbstract description of design problems and how the pattern solves it

Page 13: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

04/21/23 13

GoF: Design Patterns

Creational:

Abstract Factory

Builder

Factory Method

Prototype

Singleton

Structural:AdapterBridgeCompositeDecoratorFaçadeFlyweightProxy

Behavioral:Chain of ResponsibilityCommandInterpreterIteratorMediatorMementoObserver State / Player-roleStrategyTemplate MethodVisitor

Page 14: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

16-14

Page 15: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Outline

Introduction to Design Patterns

Template Method Pattern Decorator Pattern

Page 16: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Template Method Pattern

A template is a preset format, used as a starting point for a particular application so that the format does not have to be recreated each time it is used.

– MS Word Templates

– Java Generics – C++ Templates

Page 17: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Template Method Pattern

A template method defines an algorithm in a base class using abstract operations that subclasses override to provide concrete behavior.

Page 18: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Intent

1. Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.

2. Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm's structure.

Page 19: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Implementation

Page 20: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

UsageImplementing the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.

Refactoring is performed and common behavior is identified among classes. A abstract base class containing all the common code (in the template method) should be created to avoid code duplication.

Page 21: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Example 1

We have to develop an application for a travel agency.

The travel agency is managing each trip.

All the trips contain common behavior but there are several packages.

Each trip contains the basic steps:

– The tourists are transported to the holiday location by plane/train/ships,…

– Each day they are visiting something– They are returning back home.

?

Page 22: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Example 1See Code

Page 23: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

16-23

Page 24: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Outline

Introduction to Design Patterns

Template Method Pattern

Decorator Pattern

Page 25: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Decorator Pattern

Read it in readings R13

16-25

Page 26: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Readings

Readings 13 and 14Read code sets …….

16-26

Page 27: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Top 10 Books Programmers Read

Code Complete by Steve McConnell (2E, 2004).The Pragmatic Programmer by Andrew Hunt and David Thomas (1999).Structure and Interpretation of Computer Programs by Abelson, Sussman, and Sussman (2E, 1996).The C Programming Language by Brian Kernighan and Dennis Richie.

04/21/23 CPSC-4360-01, CPSC-5360-01, Lecture 8

27

Page 28: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Top 10 Books Programmers Read

Refactoring: Improving the Design of Existing Code by Martin Fowler, Kent Beck, John Brant, and William Opdyke (1999).Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, and Vlissides (1994). The "Gang of 4" book.The Mythical Man-Month by Frederick Brooks (1995).

04/21/23 CPSC-4360-01, CPSC-5360-01, Lecture 8

28

Page 29: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Top 10 Books Programmers Read

The Art of Computer Programming, Volume 1: Fundamental Algorithms (3rd Edition, 1997) by Donald Knuth.Compilers: Principles, Techniques and Tools 2E by Aho, Lam, Sethi, and Ullman (2006).

04/21/23 CPSC-4360-01, CPSC-5360-01, Lecture 8

29

Page 30: CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.

Top Magazines Recommended to Read

Dr Dobbs الله يرحمهاIEEE Software

04/21/23 CPSC-4360-01, CPSC-5360-01, Lecture 8

30