CS5103 Software Engineering Lecture 09 Software Design and Design Patterns.

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

Design Concepts and Principles
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Adapted from Prof. Necula CS 169, Berkeley1 Design Patterns V Software Engineering Lecture 9, Spring 2006 Clark Barrett, New York University.
Design Patterns CS is not simply about programming
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
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.
Introduction To System Analysis and design
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
Prof. Aiken CS 169 Lecture 51 Design Patterns CS169 Lecture 5.
An Introduction to Software Architecture
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
SAMANVITHA RAMAYANAM 18 TH FEBRUARY 2010 CPE 691 LAYERED APPLICATION.
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
SOFTWARE DESIGN.
Chapter 17 GRASP: Designing Objects with Responsibilities. 1CS6359 Fall 2011 John Cole.
Design engineering Vilnius The goal of design engineering is to produce a model that exhibits: firmness – a program should not have bugs that inhibit.
CSE 219 Computer Science III Program Design Principles.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Cohesion and Coupling CS 4311
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
GRASP: Designing Objects with Responsibilities
CS4723 Software Engineering Lecture 12 Software Design Quality.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
GoF: Document Editor Example Rebecca Miller-Webster.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Software Engineering Principles. SE Principles Principles are statements describing desirable properties of the product and process.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
1 CMPT 275 High Level Design Phase Modularization.
Software Design: Principles, Process, and Concepts Getting Started with Design.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
12 Chapter 12: Advanced Topics in Object-Oriented Design Systems Analysis and Design in a Changing World, 3 rd Edition.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Architecture View Models A model is a complete, simplified description of a system from a particular perspective or viewpoint. There is no single view.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Lecture VIII: Software Architecture
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Software Engineering 2007/2008 Chapter 5 Designing the System.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
7. Modular and structured design
The Object-Oriented Thought Process Chapter 15
MPCS – Advanced java Programming
Chapter ? Quality Assessment
Hao Zhong Shanghai Jiao Tong University
Data Abstraction: The Walls
Software Engineering Lecture 7 - Design Patterns
Design Patterns A Case Study: Designing a Document Editor
Informatics 122 Software Design II
DESIGN PATTERNS : Introduction
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
Designing For Testability
Presentation transcript:

CS5103 Software Engineering Lecture 09 Software Design and Design Patterns

2 Last class  Sequence diagrams  Software architecture  Why? Higher level design of larger software projects  Software architectural styles  Pipe and Filter  Layered  Repository

3 Today’s class  Software Design  Process  Factors  Measures  Design Patterns  Composite pattern  Factory pattern  Visitor pattern

4 Software Design Process  Software design is usually created in two stages  Architecture design  Detail design (component-level design)  Design patterns  Design classes  Design review  Other design issues  Refactoring  UI Design

5 Software Design Factors  Fundamental software design factors  Modularity  Abstraction  Information hiding  Component independence  Fault prevention and fault tolerance

6 Modularity and Abstraction  When we consider modular solutions to any problems, many levels of abstraction can be posed At the highest level of abstraction, a solution is stated in broad terms of problem domain: architecture At the lower levels of abstraction, a more detailed description of the solution is provided: class diagrams  Modularity hides details and facilitates evolvement Each component hides a design decision from the others

7 Component Independent  We strive in most designs to make the components independent of one another, why?  We measure the degree of component independence using two concepts Low coupling High cohesion

8 Coupling and Cohesion  Coupling Two components are highly coupled when there is a great deal of dependence between them Two components are loosely coupled when they have some dependence, but the interconnections among them are weak Two components are uncoupled when they have no interconnections at all  Cohesion A component is cohesive if the internal parts of the component are related to each other and to its overall purpose

9 Decoupling  Most difficult part in design: Need tradeoff  Consider the following case:  Online and offline bookstore components  Both need shopping cart checkout  Should we have a shopping cart checkout module for both of them? Or have one, and let the two components to call the module?  Consider potential changes on overall book discount policy and specific book discount policy?  Solution for the dilemma: break the checkout module, still not perfect 

10 Fault defense & tolerance  Defensive design anticipates situations the might lead to problems Network Failure Data corruption Invalid user inputs  Tolerate runtime errors Exception handling Redundant components (distributed system or critical software) Timely error reporting

11 Criteria for Good Software Design  High-quality designs should have characteristics that lead to quality products Correct translation from the requirements specification Ease of understanding Ease of implementation Ease of testing Ease of modification

12 Criteria for Good Software Design  Architecture  Using suitable architectural styles or patterns  Loose-coupled components  Can be implemented in an evolutionary fashion  Classes at a suitable abstract level  Interfaces are clear and minimize the data transfer  Design using a effective notation

13 Software Design Evaluation and Validation  We check a design in two different ways Validation: the design satisfies all requirements specified by the customer Verification: the characteristics (quality) of a good design are incorporated  We use some techniques for helping us to perform verification and validation Measuring design quality Design reviews

14 Measuring Software Design Quality  We check a design using a set of measures Coupling Cohesion Complexity Basic metrics: size Depths of relations Cyclomatic complexity

15 Design reviews  We check a design  Designers  Customers  Analysts  prospective users  Developers  Moderator leading the discussions  Secretary recording the issues

16 Design Patterns  Become popular due to a book  Design Patterns: Elements of Reusable Object- Oriented Software  Gang of Four: Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides  Provide solutions for common problems in micro-design

17 Design Patterns Structure  Name  Important to know for easier communication between designers  Problem solved  When to apply the pattern  Solution  Usually a class diagram segment (sometimes attributes and operations)

18 Design Patterns  Structure Patterns  Composite  Creation Patterns  Factory  Behavioral Patterns  Visitor

19 Running Example  A Document Editor  Text and graphics can be freely mixed  Graphical user interface  Support different look-and-feel GUI styles  Traversal operations: spell checking

20 Composite Pattern: Example  A document is represented by structure  Primitive glyphs  Characters, circles, pictures  Lines: A sequence of glyphs  Columns: A sequence of Lines  Pages: A sequence of Columns  Documents: A sequence of Pages

21 Example of Hierarchical Document

22 Possible Designs?  Classes of Character, Circle, Pictures, lines, Columns, pages, document  Composition relationship between classes  Not so good, why?  A lot of duplication:  all classes has onClick, draw behavior  Difficult to add or remove levels in hierarchy  Traverse/display operations need to change for any change of the hierarchy

23 Alternate Designs  One class of Glyph  All elements are subclasses of Glyph  All elements uniformly present the same interface  How to draw  Computing bounds  Mouse hit detection  …  Make extending the class easier

24 Logic Object Structure

25 Logic Object Structure

26 Composite Pattern  Applies to any hierarchy structure  Leaves and internal nodes have similar functionality  Pros:  Easy to add, remove new types  Clean interface  Cons:  Add corner cases  Enforce certain type of elements only have certain types of children

27 Problem: Supporting look-and-feel settings  Different look-and-feel standards  Appearance of scrollbars, menus, windows, etc., set by the user  We want to support them all  For example: having classes MotifScrollBar and WindowsScrollBar  Both are subclasses of ScrollBars  How should we create a new scrollbar in a window?

28 Possible designs?  Terrible ScrollBar sc = new WindowScrollBar();  Better, problems? If (style==MOTIF){ sc = new MotifScrollBar(); }else{ sc = new WindowScrollBar(); }  Conditions for other UI elements: menus, etc.  Hard to add new styles

29 Factory Pattern  One method to create a look-and-feel dependent object  Define a GUIFactory Class  One GUIFactory object for each look and feel  Create itself using conditions set by the user

30 Factory Pattern

31 Factory Pattern: factory design abstract class GUIFactory{ abstract ScrollBar CreateScrollBar(); abstract Menu CreateMenu();... } class MotifFactory extends GuiFactory{ ScrollBar CreateScrollBar(){ return new MotifScrollBar() } Menu createMenu(){ return new MotifMenu(); }... }

32 Factory Pattern: GUI code GuiFactory factory; if(style==MOTIF){ factory = new MotifFactory(); }else if(style==WINDOW){ factory = new WindowFactory(); }else{... } ScrollBar bar = factory.createScrollBar(); Menu mn = factory.createMenu();

33 Factory Pattern  Applies to the object creation of a family of classes  The factory can be changed at runtime  Pros & Cons  Lift the conditional creation of objects to the creation of factories  Flexible for add new type of objects  Sometimes make the code more difficult to understand

34 Spell Checking Problem  Considerations  Requires to traverse the document  Need to see every glyph in order  There maybe other analyses that require traverse the document  Counting words, Grammar checking

35 Possible design class Glyph{… void spellCheck(char[] curWord): for(int i = 0; i<this.children.length; i++): this.children[i].spellCheck(curWord); if(this isa character): if(this == space): Util.SpellCheck(curWord); curWord.clear(); else: curWord.append(this); …}

36 Other Actions  Document Stats  counting words,  counting pictures,  counting lines, columns, pages)  Find a word  Change line-break rules  …

37 Now we have  A large number of similar methods  All need to traverse the document  Code duplication always causes problems  change the data structure of “children” from array to list (better insertion and deletion)  Skip full-picture page to enhance performance for word-related tasks

38 Solution  Visitor pattern  Decouple traverse process and action  Write traverse code only once  Each element in the hierarchy under traverse has an “action” code

39 Visitor pattern basic design class Glyph{… void scan(Visitor v): for(int i = 0; i<d.children.length; i++): d.children[i].scan(v); …} We have different visitors: spellcheckVisitor, documentStatVisitor, etc.

40 Solution

41 Code of SpellCheckVisitor class SpellCheckVisitor extends Visitor{ char[] curWord = {}; void visitChar(Character c){ if(c==SPACE): spellCheck(curWord); curWord.clear(); else: curWord.add(c); }

42 Still have problems  How about counting all glyphs (including picture, character, circle)?  Need to change visitPicture, visitCharacter, and visitCircle?  Add visitGlyph

43 Add VisitGlyph class Glyph{ … scan(Visitor){ for each c in children: v.visitGlyph(c) c.Scan(v) } … }

44 Code of CountGlyphVisitor class CountGlyphVisitor extends Visitor{ int count = 0; void visitGlyph(Glyph g){ if(g.children.length==0): count++; }

45 Summary of design patterns  Major aim: decoupling  pros:  more flexibility  less changes when modification is required  Cons:  not intuitive at the beginning, harder to read code, but get easier when you are familiar with the pattern  Trouble for verifications and analysis (often not scalable without a design pattern)  So it is rather popular!

46 Other useful design patterns  Observer  Facade  Factory method  Decorator  Bridge  See them in wiki pages or read the book  Design Patterns: Elements of Reusable Object-Oriented Software

47 Today’s class  Software Design  Design process  Design factors  Design evaluation  Design Patterns  Composite pattern  Factory pattern  Visitor pattern

48 Next Class  Software Implementation: Versioning  Why versioning?  Versioning tools  CVS  SVN  GIT

49 Thanks!

50 A controversial pattern: singleton pattern  Problem  How to make sure a class only has one object  File system  Windows manager  …  The class itself is made responsible for keeping track of its instance. It can thus ensure that no more than one instance is created. This is the singleton pattern.

Singleton Pattern Singleton static Instance() SingletonOp() GetSingletonData() static uniqueInstance singletonData return uniqueinstance

Singleton Pattern: code class Singleton { public: static Singleton Instance(); } protected: Singleton(); private: Static Singleton _instance // Only one instance can ever be created. // Creation hidden inside Instance().// Cannot access directly.

Singleton Pattern: code Singleton _instance; Singleton Instance(){ if (_instance ==null) { _instance=new Singleton; } Return _instance; } // Clients access the singleton // exclusively via the Instance member // function.

54 Singleton Pattern  Pros  Controls the number of instances (no need to refer to another class)  Lazy initialization  Cons  Latent coupling between components and singleton  Singleton may have states: hard to test  Cannot extend singleton class  Terrible in multi-thread if not well implemented

55 View points  We love singletons (getting weaker and weaker in the past 10 years)  Singletons are evil, actually an anti-pattern (many people believe this)  Use singletons carefully and only when no other alternatives, control the number of singleton classes in your project

56 Alternative  Dependency injection:  Create the object at the beginning and pass it as a parameter

57 A story from google A new developer go to a group and want to try on some methods testCreditCardCharge() { CreditCard c = new CreditCard( " ", 5, 2008); c.charge(100); } This code: Only works when you run as part of the suite. When run in isolation, throws NullPointerException. When you get your credit card bill, you are out $100 for every time the test runs.

58 A story from google After a lot of digging, you learn that you need to initialize the CreditCardProcessor. testCreditCardCharge() { CreditCardProcessor.init(); CreditCard c = new CreditCard( " ", 5, 2008); c.charge(100); } Still not working 

59 A story from google After a lot of digging, you learn that you need to initialize the OfflineQueue. testCreditCardCharge() { OfflineQueue.init(); CreditCardProcessor.init(); CreditCard c = new CreditCard( " ", 5, 2008); c.charge(100); } Still not working 

60 A story from google After a lot of digging, you learn that you need to initialize the Database. testCreditCardCharge() { Database.init(); OfflineQueue.init(); CreditCardProcessor.init(); CreditCard c = new CreditCard( " ", 5, 2008); c.charge(100); } But sometimes you have to find out the correct sequence!

61 Solution with dependency injection testCreditCardCharge() { Database db = Database(); OfflineQueue q = OfflineQueue(db); CreditCardProcessor ccp = new CreditCardProcessor(q); CreditCard c = new CreditCard( " ", 5, 2008); c.charge(ccp, 100); }