Chapter 25 More Design Patterns. Polymorphism Issue: Conditional variation –If-then-else or switch statements –New variation or case: Conditional statements.

Slides:



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

Chapter 11 Component-Level Design
Object-Oriented Analysis and Design CHAPTER 17, 25: GRASP PATTERNS 1.
Chapter 22 Object-Oriented Systems Analysis and Design and UML Systems Analysis and Design Kendall and Kendall Fifth Edition.
GRASP The other four What are the first five? What is the goal/purpose of using patterns?
Chapter 25 GRASP: More Objects with Responsibilities 1CS6359 Fall 2011 John Cole.
Object Oriented System Development with VB .NET
© 2005 Prentice Hall8-1 Stumpf and Teague Object-Oriented Systems Analysis and Design with UML.
NJIT More GRASP Patterns Chapter 22 Applying UML and Patterns Craig Larman Prepared By: Krishnendu Banerjee.
Fall 2009ACS-3913 R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
Computer Science 240 Principles of Software Design.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Chapter 25 More Design Patterns.
Client/Server Software Architectures Yonglei Tao.
Design Patterns.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
GRASP Principles. How to Design Objects The hard step: moving from analysis to design How to do it? –Design principles (Larman: “patterns”) – an attempt.
Ceg860 (Prasad)L6MR1 Modularity Extendibility Reusability.
Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
Copyright 2002 Prentice-Hall, Inc. Modern Systems Analysis and Design Third Edition Jeffrey A. Hoffer Joey F. George Joseph S. Valacich Chapter 20 Object-Oriented.
Database Management System Prepared by Dr. Ahmed El-Ragal Reviewed & Presented By Mr. Mahmoud Rafeek Alfarra College Of Science & Technology Khan younis.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Chapter 7: Object Design Examples with GRASP. Objective Design use case realizations. Apply GRASP to assign responsibilities to classes. Apply UML to.
SOFTWARE DESIGN (SWD) Instructor: Dr. Hany H. Ammar
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Chapter 17 GRASP: Designing Objects with Responsibilities. 1CS6359 Fall 2011 John Cole.
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Object-Oriented Design Part 2
Concepts of Software Quality Yonglei Tao 1. Software Quality Attributes  Reliability  correctness, completeness, consistency, robustness  Testability.
CSSE 374: More GRASP’ing for Object Responsibilities
Cohesion and Coupling CS 4311
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
Design. Practices Principles Patterns What are the characteristics of good design? What are good solutions to common design problems? How do we go about.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Copyright © Craig Larman All Rights Reserved COMP-350 Object-Oriented Analysis and Design GRASP: Designing Objects with Responsibilities Reference:
GRASP: More Patterns for Assigning Responsibilities Presented By Dr. Shazzad Hosain.
Rohini Sharma Roll No. RA1809A01 Regd. No M.Tech.(CSE) Part Time 3 rd Semester.
Object-Oriented Analysis and Design Week 12, 2009.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
Elaboration: Iteration 2. Elaboration: Iteration 2 Basics Iteration 1 ends with : All the software has been tested: The idea in the UP is to do early,
Design Patterns: MORE Examples
CompSci 280 S Introduction to Software Development
BTS530: Major Project Planning and Design
Coupling and Cohesion Rajni Bhalla.
GoF Patterns (GoF) popo.
Conception OBJET GRASP Patterns
Chapter 11 Object-Oriented Design
Behavioral Design Patterns
Apply Expert, Creator, Controller, Low Coupling, High Cohesion
Component-Level Design
Chapter 25 GRASP The other four.
Software Design Principles
Chapter 25 GRASP The other four.
Chapter 22 Object-Oriented Systems Analysis and Design and UML
Cohesion and Coupling.
HFOOAD Chapter 5 Interlude
GoF Patterns Ch. 26.
Presentation transcript:

Chapter 25 More Design Patterns

Polymorphism Issue: Conditional variation –If-then-else or switch statements –New variation or case: Conditional statements change Often in many places (methods, classes) –Makes it difficult to extend program, add new variations Examples –Alternatives based on type –Pluggable software components: Replace one server with another Without affecting the clients

Polymorphism Solution –Do not test for the type of an object and use conditional logic –Use polymorphic operations instead Example: Third-party tax calculator programs in the NextGen POS system We want to use an “adapter class” –We call adapter’s methods, adapter converts it to tax calculator’s methods We’d like our code not to depend on which tax calculator we use We’d like to avoid conditional statements in the adapter class Solution: Use one adapter class per tax calculator –Use polymorphism

Adapter classes implement iTaxCalculatorAdapter interface

Polymorphism in Monopoly Issue: Different actions need to be performed when player lands on different types of square Examples: –Go square: Get $200 –Tax square: Pay income tax (10% or $200) –Regular square: Do nothing (for now) Solution: Use abstract superclass, Square –RegularSquare, TaxSquare, IncomeTaxSquare subclasses

Polymorphism in Monopoly

Issue: How to adapt interaction diagrams to this change? May use “sd” and “ref” frames here to be more formal

One Polymorphic Case

Another polymorphic case

Notice design change: –Now Player knows location, instead of Piece –Decreases coupling –In fact, we realize “piece” class not really needed

When to use interfaces vs. abstract superclasses? Use interfaces whenever you can –Because of single inheritance, abstract classes are restrictive –Unless there are a lot of default method implementations inherited – then use abstract classes

Design Pattern: Pure Fabrication Idea: Make up (“fabricate”) new class with no direct equivalent concept in the domain –When other design patterns do not offer solutions with low coupling and high cohesion Recall example: Saving a Sale object in a database What goes wrong if “Sale” saves itself? –Task requires a lot of supporting database operations –Sale has to be coupled to the db interface –Saving to db a general task A lot of other classes need the same support Lots of duplication in each object

Solution: New “PersistentStorage” class Persistent storage not a domain concept We grouped a number of responsibilities into this class Similar issue in Monopoly: –Player rolls dice: Not reusable in another game –Summing of dice done by player –Can’t ask for dice total without rolling again Improved design: Fabricate “Cup” class that does the jobs above

Fabrication in Monopoly: The “Cup” class

Design of Objects Design of objects done in one of two ways –Objects chosen by representational decomposition Representing domain objects Example: –TableOfContents –Objects chosen by behavioral decomposition Group a number of related operations into a class Example: –Algorithm object: TableOfContentsGenerator Don’t overdo fabrication –If you put all functionality in one class, you end up writing a C program

Updated Monopoly Interaction Diagram

Design Pattern: Indirection Issue: How to avoid coupling between two or more things –Why? One is likely to change a lot. Don’t want the other to depend on it. Solution: Use indirection. Create intermediate object. Example: TaxCalculatorAdapter –Our design is independent from the third party tax program API –If the API changes, we only modify the adapter. Example: PersistentStorage –Just change PersistentStorage to use a different database

Indirection via the adapter class/object

Design Pattern: Protected Variations Issue: How to design objects, subsystems and systems so that variations or instability in these elements do not have an undesirable impact on others? Solution: –Identify points of predicted variations and instability –Put a stable interface around them to protect the rest of the design Example: External tax calculator and the class hierarchy of adapters

Examples of Mechanisms Motivated by Protected Variations Data-driven designs: –Example: Web-page style sheet read separately from the.html file Service look-up mechanisms: JNDI, Jini, UDDI –Protects designs from where services are located Interpreter-Driven Designs –Virtual machines, language interpreters,... Reflective or Meta-Level Designs –Java Beans –A way to learn what attribute or bean property a “Bean” has and what method to use to access it

Law of Demeter In any method M attached to a Class C, only methods defined by the following classes may be used: The instance variable classes of C the argument classes of the method M including C itself Note: Global objects or the local objects of M are considered arguments of M

Law of Demeter (weak) In any method M attached to a Class C, data can be accessed and messages can be sent to only the following objects: the arguments of the method M including self The instance variables for the receiver of the message. Global objects the local objects of M

Law of Demeter (strong) In any method M attached to a Class C, data can be accessed and messages can be sent to only the following objects: the arguments of the method M including self The instance variables defined in the class containing the method being executed. (no direct access to inherited instance variables.) Global objects the local objects of M

Law of Demeter (from Dr. Gordon) In any method M attached to a Class C, data can be accessed and messages can be sent to only the following objects: the arguments of the method M including self the local objects of M (no direct access to instance variables.)

Cohesion Revisited Varieties of Cohesion –Coincidental Cohesion –Logical Cohesion –Temporal Cohesion –Communication Cohesion –Sequential Cohesion –Functional Cohesion –Data Cohesion

Coincidental Cohesion This cohesion occurs when the components have no apparent reason for being grouped together In OO framework, we say that it occurs when a class consists of methods that are unrelated It is usually a sign of poor design in any software design approach.

Logical Cohesion This occurs when there is a logical connection among the elements of the software but no actual connection A library of mathematical functions might exhibit logical cohesion

Temporal Cohesion This occurs when the elements are bound together because they are all used at approximately the same time. The initialization routines of a compiler or other such software are usually a good example of temporal cohesion. A better OO approach would be to spread the initialization work over classes that would more closely be charged with subsequent behavior.

Communication Cohesion This occurs when methods are grouped because they all access the same data or devices. The “class” acts as a manager for the data or devices.

Sequential Cohesion This occurs when elements in a class are linked by the necessity to be activated in a particular order. A better design would be to raise the level of abstraction Since the sequentiality must be captured at some level, the goal is to hide this as much as possible from other levels of abstraction.

Functional Cohesion This is a desirable type of cohesion in which the elements of the component all relate to the achievement of a single goal. The measure of quality design at the level of a method is functional cohesion. Each method should have a clearly stated unifunctional goal achieved by that method.

Data Cohesion At the OO level, this is the measure of the quality of design of a class A class defines a set of data values and exports routines that manipulate the data. Data Cohesion occurs when a class is designed to implement a data abstraction.

Estimating Cohesion Write a brief statement of the Class (method). If the statement that describes the purpose of a class (method) is a compound statement containing more that one verb, the class is probably performing more than one function. It probably has sequential or communicating cohesion If the statement contains words relating to time, it probably has sequential or temporal cohesion. “Wait for... “ If the predicate of the statement does not contain a single specific object following the verb, it probably has logical cohesion. “Edit all data” If the statement contains words such as initialize or clean-up, it is probably temporal cohesion.