CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Common mistakes Basic Design Principles David Rabinowitz.
The Architecture Design Process
Design Patterns CS is not simply about programming
Common mistakes Basic Design Principles Amit Shabtay.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Design Patterns Ref : Chapter 15 Bennett et al. useful groups of collaborating classes that provide a solution to commonly occuring problems. provide.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Unified Modeling Language
An Introduction to Software Architecture
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Y2 eProjects Session 4 – Advanced Topics. Objectives  Dynamic Models  Design Patterns (Optional)  Software testing (for S4) ACCP i7.1\Sem3_4\eProject\T4.
In the name of Allah The Proxy Pattern Elham moazzen.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CSE 432: Design Patterns Introduction What’s a Pattern? What’s an Idiom? According to Alexander, a pattern: –Describes a recurring problem –Describes the.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Frameworks & Patterns Use of Organized Classes. Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt 1.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Object Oriented Programming
Design Patterns Introduction
Overview of C++ Polymorphism
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
1 Good Object-Oriented Design Dr. Radu Marinescu Lecture 4 Introduction to Design Patterns.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Unit II-Chapter No. : 5- design Patterns
The Movement To Objects
Chapter 10 Design Patterns.
Software Design Patterns
MPCS – Advanced java Programming
A Brief Introduction to Design Patterns
Design Patterns.
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Design Patterns Outline
Subtype Polymorphism, Subtyping vs
An Introduction to Software Architecture
DESIGN PATTERNS : Introduction
CS 350 – Software Design Singleton – Chapter 21
John D. McGregor Module 6 Session 1 More Design
Introduction to Design Patterns
Overview of C++ Polymorphism
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Presented by Igor Ivković
Presentation transcript:

CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns

Where are we going Functional architecture Conceptual architecture Design architecture <= You are here Integration architecture

Architectural styles – Event based Design patterns – observer Language idioms – J+= 1

Open/Closed Principle SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.)SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION

Open/closed – not a solution struct Square { ShapeType itsType; double itsSide; Point itsTopLeft; }; // // These functions are implemented elsewhere // void DrawSquare(struct Square*) void DrawCircle(struct Circle*); typedef struct Shape *ShapePointer; void DrawAllShapes(ShapePointer list[], int n) { int i; for (i=0; i<n; i++) { struct Shape* s = list[i]; switch (s->itsType) { case square: DrawSquare((struct Square*)s); break; case circle: DrawCircle((struct Circle*)s); break; }

Open/closed – not a solution void DrawAllShapes(ShapePointer list[], int n) { int i; for (i=0; i<n; i++) { struct Shape* s = list[i]; switch (s->itsType) { case square: DrawSquare((struct Square*)s); break; case circle: DrawCircle((struct Circle*)s); break; }

The Open/Closed solution class Shape { public: virtual void Draw() const = 0; }; class Square : public Shape { public: virtual void Draw() const; }; class Circle : public Shape { public: virtual void Draw() const; }; void DrawAllShapes(Set & list) { for (Iterator i(list); i; i++) (*i)->Draw(); }

Liskov's Substitution Principle If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.

2 possibilities

Capture experience Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides This is the initial effort at patterns Idea is to capture design experience It is not a pattern until it has appeared in practice multiple times Good source -

Alexander’s definition As an element in the world, each pattern is a relationship between a certain context, a certain system of forces which occurs repeatedly in that context, and a certain spatial configuration which allows these forces to resolve themselves. As an element of language, a pattern is an instruction, which shows how this spatial configuration can be used, over and over again, to resolve the given system of forces, wherever the context makes it relevant.

Pattern format Pattern Name Problem Context Forces Solution Resulting context Rationale

Does it fit? My problem Model Controller View Are the pre-conditions met? What forces are resolved?

Categories Creational Structural Behavioral

Creational How does the static type definition get mapped to dynamic instantiations Forces constrain how the mapping happens Cardinality must be enforced

Singleton Pattern Name - Singleton Problem – The nature of the behavior of the class is such that it is important that there is a sole source for the behavior. Context – In a program where the number of instances of a class is critical, such as a server. Forces – The constraint needs to be enforced as locally as possible.

Singleton Solution – Protect the constructor of the class so that it can only be accessed indirectly Resulting context – the callers to this class do not have to check to determine if they get the correct object, e.g. server. Rationale – Hiding the constraint inside the class improves the modularity of the design.

UML diagram

Code class Singleton { private static Singleton instance; private Singleton() {... } public static synchronized Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; }... public void doSomething() {... } }

Visitor - Behavioral Pattern Name - Visitor Problem – How to apply a common operation to a set of objects in a data structure Context – Which operations are needed changes over time; multiple types of objects in the structure Forces – The more changes made, the worse the structure of the design.

Visitor Solution: define a specialization hierarchy that has new algorithms Resulting context – it is easy to add a new algorithm Rationale – New implementations of algorithms can be used whenever a new operation is needed

UML:Visitor

Trade-off The Visitor pattern illustrates the typical trade off situation. One approach to implementation makes each Visitable Class easy to modify for a new Visitor (a new algorithm) compared to making each Visitor Class easy to modify for a new Vistable Class. The other approach is just the reverse The designer has to analyze the situation: – Which will happen most frequently – new Visitor or new Visitable? – What is the effect on the quality attributes? – What are the risks? – What happens if I am wrong?

code public abstract class Visitor { public abstract void visit(Customer customer); public abstract void visit(Order order); public abstract void visit(Item item); public abstract void defaultVisit(Object object); public void visit(Object object) { try { Method downPolymorphic = object.getClass().getMethod("visit", new Class[] { object.getClass() }); if (downPolymorphic == null) { defaultVisit(object); } else { downPolymorphic.invoke(this, new Object[] {object}); } } catch (NoSuchMethodException e) { this.defaultVisit(object); } catch (InvocationTargetException e) { this.defaultVisit(object); } catch (IllegalAccessException e) { this.defaultVisit(object); } } }

Adapter: Structural Pattern Name - Adapter Problem – Two objects need to communicate but their provides/requires interfaces do not match Context – we may not have source code for one or both of the implementations Forces – re-implementing one class with a new interface is expensive

Adapter Solution - Convert the interface of a class into another interface clients expect. – Adapter lets classes work together, that could not otherwise because of incompatible interfaces. Resulting context – the two classes work together and any places where one of the original classes worked, it still does Rationale – the two classes are needed but there is no money/time to redesign

design

It’s a big design space There are many, many design patterns. Some better than others Learn: – how to find them, – recognize what will be useful and – how to apply them

Unified Modeling Language UML provides a comprehensive design notation Read these brief introductions to UML: – – rary/769.html rary/769.html Work through the tutorial at: –

Concurrent programming ngPatterns