Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns - An Introduction

Similar presentations


Presentation on theme: "Design Patterns - An Introduction"— Presentation transcript:

1 Design Patterns - An Introduction
Today’s Agenda 2/25/2019 BITS, Pilani. Sundar B.

2 Today’s Topics Design Templates Patterns - Motivating Example
Principles, Implements / Processes, Templates Relevance to Software Templates Context Granularity Patterns - Motivating Example MVC Observer BITS, Pilani. Sundar B. 2/25/2019

3 Design - Principles Consider designing an “entrance to a building”
Physical entity could be a door, a change in elevation, or through a courtyard. Principle: Transition of the environment – i.e. crossing the boundary - should be a sensory experience. BITS, Pilani. Sundar B. 2/25/2019

4 Design – Principles [2] Consider creating an opening scene for a movie
An event or an activity that includes principal actors (hero, heroine, villain) Principle: Establish the “embodiment” – i.e. physical realization - of a theme. BITS, Pilani. Sundar B. 2/25/2019

5 Design - Implements/Processes
Building Entrance Drawing of the front view Clay model of courtyard Movie Opening A car chase A confrontation between adversaries BITS, Pilani. Sundar B. 2/25/2019

6 Design - Templates Building Entrance Movie Opening
Library: “High elevation” entrances Airport: Wide entrance with multiple doors Church: “oak doors with stained glass decorations” Movie Opening Villain has a weapons factory – hero destroys it: “James Bond” template Young kid gets separated from mother/brother: -Bollywood variant of the “Karna” template. BITS, Pilani. Sundar B. 2/25/2019

7 Software Design – Principles and Implements / Processes
Basic: Separation of Concerns Implied: Modularity and Re-use. Implements / Processes: Top-down design Language constructs for data abstraction User Interface Toolkit BITS, Pilani. Sundar B. 2/25/2019

8 Software Design – Templates
Example 1: Iterate over array elements in C or Java for ( j=0; j<N; j++) … A[j] … Example 2: Use indirection to enable sharing. int *m, *n; … // Assignments to m, n swap(m, n); // Code in swap may modify m,n BITS, Pilani. Sundar B. 2/25/2019

9 Software Design – Templates [2]
Example 3: Store interfaces or type declarations in header files (of a C program) /*file SetOps.h */ Set setUnion(Set s1, Set s2); /*file Set.h */ typedef unsigned int Set; /*file SetOps.h */ Set setUnion(Set s1, Set s2) { return s1 | s2; }

10 Software Design – Templates [3]
Example 4: Browser - Document Object Model Requirements: Elements may contain other elements e.g. Table may contain a Form or another Table; a Form contain a Table. etc. Parser should present a uniform interface for all elements to other modules (say rendering engine) BITS, Pilani. Sundar B. 2/25/2019

11 Software Design – Templates [4]
Example 4 (contd..) - Design: Element Text Formatter is-a Semantic Element is-a is-a 1..n composed of Anchor Layout Container Table is-a Form is-a

12 Software Design – Templates [5]
Example 5: Sets or Collections – Operations Requirements: Operations may have to access each element of a set / collection Different implementations (with different pros and cons) are available for collections. Design: Provide a way to enumerate elements of a set. Operations use enumerator for accessing elems. BITS, Pilani. Sundar B. 2/25/2019

13 Software Design – Templates [6]
Example 6: Design an online document publishing system for BITS Requirements: Authors submit documents online in different formats. Publishing system uses different markup languages for different displays. Documents have to spell-checked before being published. BITS, Pilani. Sundar B. 2/25/2019

14 Software Design – Templates [7]
Example 6 (contd..) - Design: Word Doc. Generic Transducer XML PDF Doc. Spell Checker Device Transducer XML DeviceML DeviceML BITS Styler Display / Blaster DeviceStyle BITS, Pilani. Sundar B. 2/25/2019

15 Software Design – Templates [8]
Example 6 (contd..) – Design: Modules are designed as input to output transducers. That is, they are filters! (recall Unix commands) Modules are not sharing any external data or other modules. So, they can be run concurrently (without explicit synchronization). That is, they can be piped! (recall Unix pipes) BITS, Pilani. Sundar B. 2/25/2019

16 Software Design – Templates [9]
What are templates? They provide (generic) solutions – often for more than one instance of a problem. These solutions can be (contextually) reused with minor adaptations. Templates are contextual but generic solutions that are re-usable with minor adaptations. BITS, Pilani. Sundar B. 2/25/2019

17 Software Design – Templates [10]
Observations: There are some differences among templates (even among the ones we studied today) Problem contexts are different: Low level design (close to coding) Medium level design High level design (close to architecture) Often leads to different granularities! BITS, Pilani. Sundar B. 2/25/2019

18 Software Design – Templates [11]
Low level templates: Fine grained; Often no additional work required to use. Implementation (say language or OS) specific a.k.a IDIOMS Almost always work as advertised! Examples: 1 (Iteration & Arrays), -- Refer to Slide 8 2 (Sharing & Indirection) -- Refer to Slide 8 3 (Program Structuring in C) -- Refer to Slide 9 BITS, Pilani. Sundar B. 2/25/2019

19 Software Design – Templates [12]
Medium level templates: Not so fine-grained; Adaptations often required for specific situation/problem. May lead to minor problems when mis-applied. a.k.a (DESIGN) PATTERNS Examples: 4 ( Browser – Composite Pattern) (Refer to Slide 11) 5 ( Set Enumeration – Iterator Pattern) (Refer to Slide 12) BITS, Pilani. Sundar B. 2/25/2019

20 Software Design – Templates [13]
High level templates: Coarse-grained. Often require lot of customization and implementation to achieve desired result. Often Specific to problem domain. When mis-applied lead to disastrous results! a.k.a (ARCHITECTURAL) FRAMEWORKS Examples: 6 (Document Publishing – Pipe & Filter Architecture) BITS, Pilani. Sundar B. 2/25/2019

21 ((Software) Design) Patterns
Definition: Contextual Design solutions of medium granularity that can be re-used in recurring design problems with minimal adaptation. Usage: Documentation of (Expert) Knowledge of Design solutions. Purpose: Communication and Persistence for Re-use. BITS, Pilani. Sundar B. 2/25/2019

22 ((Software) Design) Patterns
Why study patterns? “to organize implicit knowledge about how people solve recurring problems when they go about building things” – Christopher Alexander, (Architect by profession; Father of modern study of patterns) BITS, Pilani. Sundar B. 2/25/2019

23 Patterns Software context:
Most of the work originated in SmallTalk community. SmallTalk was a programming language: Object Oriented Meant for rapid prototyping Came with (what are known today as) IDE and API IDE elements were in SmallTalk! (Concrete) Precursor to modeling, frameworks and patterns! BITS, Pilani. Sundar B. 2/25/2019

24 Patterns – MVC framework
SmallTalk’s user interface framework Model - refers to data model View – refers to external views or presentation of data. Controller – refers to module relating reactions of view or presentation to changes in data. BITS, Pilani. Sundar B. 2/25/2019

25 MVC Framework [2] Need to separate presentational aspects with the data, i.e. separate views and data. Classes defining application data and presentation can be reused. Change in one view automatically reflected in other views. Also, change in the application data is reflected in all views. Defines one-to-many dependency amongst objects so that when one object changes its state, all its dependents are notified. BITS, Pilani. Sundar B. 2/25/2019

26 MVC Framework [3] A D C B Y 10 40 30 20 X 15 35 35 15 Z 10 40 30 20
Relative Percentages Y X Z A B C D A B C D A=10% B=40% C=30% D=20% Application data Change notification Requests, modifications BITS, Pilani. Sundar B. 2/25/2019

27 Observer Pattern Model – View paradigm can be generalized:
A view is an observer A model is an subject that is observed. Referred to as the observer pattern. BITS, Pilani. Sundar B. 2/25/2019

28 Observer Pattern [2] Subject observers Observer
attach (Observer) detach (Observer) Notify () observers Observer Update() For all x in observers{ x  Update(); } Concrete Observer Update() observerState subject Concrete Subject GetState() SetState() subjectState observerState= subject  getState(); BITS, Pilani. Sundar B. 2/25/2019


Download ppt "Design Patterns - An Introduction"

Similar presentations


Ads by Google