Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

Similar documents
CS 520 Theory and Practice of Software Engineering Fall 2018

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Originality is Overrated: OO Design Principles

CS 520 Theory and Practice of Software Engineering Fall 2017

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

COURSE 2 DESIGN PATTERNS

Principles of Object-Oriented Design

FACADE & ADAPTER CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 7 09/13/2011

Exam Questions. Object-Oriented Design, IV1350. Maximum exam score is 100, grade limits are as follows. Score Grade 90 A 80 B 70 C 60 D 50 E

CS 320 Introduction to Software Engineering Spring March 06, 2017

MSO Lecture 6. Wouter Swierstra. September 24, 2015

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

CHAPTER 5 GENERAL OOP CONCEPTS

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Reengineering II. Transforming the System

Design Patterns: State, Bridge, Visitor

Object Relationships

Inheritance and Polymorphism

Patterns and Testing

RDD and Strategy Pa.ern

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Introduction to Object Oriented Programming. (Hebrew University, CS / Spring 2012 )

The Software Design Process. CSCE 315 Programming Studio, Fall 2017 Tanzir Ahmed

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

Software Engineering

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

1. Write two major differences between Object-oriented programming and procedural programming?

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Essential Skills for the Agile Developer. Agile. copyright Net Objectives, Inc.

Lecture 4: Design Concepts For Responsibility- Driven Design Kenneth M. Anderson January 20, 2005

Inheritance and Polymorphism in Java

MSO Lecture 6. Wouter Swierstra (adapted by HP) September 28, 2017

Module Outline. What is Object-Oriented? Some Possible Definitions. Why Object-oriented? Fundamentals of Object Orientation

An Introduction to Patterns

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

Object-Oriented Design

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Chapter 1: Principles of Programming and Software Engineering

Understading Refactorings

Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Object-Oriented Concepts and Design Principles

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

Index Shalloway rev.qrk 9/21/04 5:54 PM Page 419. Index

Design Patterns Design patterns advantages:

THE OBJECT-ORIENTED DESIGN PROCESS AND DESIGN AXIOMS (CH -9)

Object-Oriented Systems Analysis and Design Using UML

The Object-Oriented Paradigm

Credit where Credit is Due. Lecture 4: Fundamentals of Object Technology. Goals for this Lecture. Real-World Objects

Relationships amongst Objects

Principles of Software Construction: Objects, Design, and Concurrency. Assigning Responsibilities to Objects. toad. Jonathan Aldrich Charlie Garrod

What is a Programming Paradigm

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

CS 370 Design Heuristics D R. M I C H A E L J. R E A L E F A L L

Bridge Pattern. Used to decouple an abstraction from its implementation so that the two can vary independently

Introduction to User Stories. CSCI 5828: Foundations of Software Engineering Lecture 05 09/09/2014

Agile Architecture. The Why, the What and the How

CSE 70 Final Exam Fall 2009

As a programmer, you know how easy it can be to get lost in the details

Object- Oriented Design with UML and Java Part I: Fundamentals

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

CS342: Software Design. November 21, 2017

Chapter 1: Programming Principles

ADVANCED SOFTWARE DESIGN LECTURE 4 SOFTWARE ARCHITECTURE

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

Information System Design (IT60105)

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Lecture: Modular Design

CS-202 Introduction to Object Oriented Programming

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

20 Years of. Improve the Design of your Code. Dr Dimitris Dranidis JAVA Meetup Group, Thessaloniki May 2015

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Chapter 8: Class and Method Design

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Test-Driven Development (TDD)

The Design Patterns Matrix From Analysis to Implementation

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Object Oriented Programming

Principles of Computer Game Design and Implementation. Lecture 3

Design and Information Hiding

Application Architectures, Design Patterns

The 10 Minute Guide to Object Oriented Programming

CS560: Formal Modelling and Implementation of Systems (Term II)

EMF Temporality. Jean-Claude Coté Éric Ladouceur

Introduction to Object-Oriented Programming

Electronic Design Automation Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Dealing with Bugs. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 27 04/21/2009

Transcription:

Expanding Our Horizons CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011 1

Goals of the Lecture Cover the material in Chapter 8 of our textbook New perspective on objects and encapsulation How to handle variation in behavior New perspective on inheritance Commonality and Variability Analysis Relationship between Design Patterns and Agile 2

Traditional View of Objects Data with Methods or Smart Data Based on the mechanics of OO languages In C, you have structs (data) and then you have functions that operate on the structs (methods) In C++, you could combine the two into a single unit hence data with methods But this view limits your ability to design with objects The focus is mainly on the data not the behavior! 3

Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class Pixel { } private double red; private double green; private double blue; private double alpha; public Pixel(double red, double green, double blue, double alpha) { this.red = red; this.green = green; this.blue = blue; this.alpha = alpha; } public double getred() { return red; } public void setred(double red) { this.red = red; }... Dumb Data Holder This is a class that exists solely to help some other class. It is the worst form of data with methods Part of the problem is this concept is too low level to be useful. 4

New Perspective on Objects (I) Objects are Things with Responsibilities Don t focus on the data; it is subject to change as the implementation evolves to meet non-functional constraints this is why we set attributes as private by default focus on behavior And how those behaviors allow you to fulfill responsibilities that the system must meet 5

New Perspective on Objects (II) The responsibilities come from the requirements If you have a requirement to create profiles for your users then somewhere in your design, you have an object with the responsibility of creating profiles and managing the workflow related to that task an object with the responsibility of storing and manipulating the data of a profile an object with the responsibility of storing and manipulating multiple profiles 6

New Perspective on Objects (III) Responsibilities help you design Requirements lead to responsibilities And responsibilities need to go somewhere The process of analysis becomes finding all of the responsibilities of the system The process of design becomes finding a home for each responsibility (object/subsystem) 7

New Perspective on Objects (IV) A focus on responsibilities also promotes a focus on defining the public interface of an object What methods will I need to meet my responsibilities? How will I be used? This focus early in design matches the external perspective we need to maintain see the system from the user s point of view A rush to implementation obscures that perspective 8

Example, continued Pixel was too low level to be useful but a collection of pixels an image Now you re talking With an image class you can specify useful services stretch, flip, distort, change to black and white, add a shadow, produce a mirror image effect, move, display yourself on this canvas, 9

Example, continued. Here s the public interface of the UIImage class in Apple s Cocoa touch library Note that they refer to the public interface as Tasks A + in front of a method name indicates a static method A - indicates an instance method This class is designed to be used with UIImageView to be displayed and CoreAnimation to be manipulated/ animated 10

Traditional View on Encapsulation Encapsulation means hiding data This view is too limited and again focuses on the data when we want to focus on behavior and responsibilities The Umbrella Example In the analogy, the car plays the role of encapsulation Thinking of a car as an umbrella is too limiting; it can do so much more! The same is true of encapsulation 11

New Perspective on Encapsulation (I) Encapsulation should be thought of as any kind of hiding especially the hiding of things that can change We certainly can hide data but also behavior, implementations, design details, derived classes, instantiation rules, etc. and the mechanisms can involve more than just attribute and method visibility annotations design patterns, subsystem boundaries, interfaces for example, Objective-C s class clusters 12

Multiple Types of Encapsulation (I) Shape setlocation(point) Circle Point LIne setlocation(point) OtherCircle setx(int) sety(int) 13

Multiple Types of Encapsulation (II) Shape setlocation(point) Circle Point LIne setlocation(point) Encapsulation of Data within each class OtherCircle setx(int) sety(int) 14

Multiple Types of Encapsulation (III) Shape setlocation(point) Circle Point LIne setlocation(point) Encapsulation of methods; e.g. setlocation() in Circle OtherCircle setx(int) sety(int) 15

Multiple Types of Encapsulation (IV) Shape setlocation(point) Circle Point LIne setlocation(point) Encapsulation of objects; only Circle knows about OtherCircle OtherCircle setx(int) sety(int) 16

Multiple Types of Encapsulation (V) Shape setlocation(point) Circle Point LIne setlocation(point) Encapsulation of type; clients of Shape do not have to know about points, lines and circles OtherCircle setx(int) sety(int) 17

Encapsulation of Type Encapsulation of Type occurs when there is an abstract class with derivations (subclasses) or an interface with implementations AND the abstract class or interface is used polymorphically When you encounter the term encapsulation in design patterns, this is typically what they are referring to These abstract types provide the means for decomposing designs around the major services the system provides 18

Inheritance: Specialization vs. Behavior Encapsulation of type provides a new way of looking at inheritance Subclasses of the abstract types are grouped because they all behave the same way (as defined by the methods of the abstract type) This contrasts with inheritance used to specialize (make more specific) an existing class Pentagon SpecialBorderPentagon 19

Specialization vs. Behavior (II) Pentagon SpecialBorderPentagon Pros Reuse pentagon s behavior; enable variation with borders Cons Weak Cohesion: If I specialize again with another border, I ve got classes that all deal with both pentagons and borders Poor Reuse: How do I share my borders with Circles? Does not scale across multiple dimensions: SpecialBorderBlinkingSpinningPentagon (give me a break!) 20

Example Imagine if we had to add Circle to this hierarchy! Shape Pentagon Specialization can lead to bad designs like this SpinningPentagon BlinkingPentagon SpinningBlinkingPentagon BlinkingSpinningPentagon 21

Specialization vs. Behavior (III) To avoid the trap of SpecialBorderBlinkingSpinningPentagon Encapsulate variation in behavior using the Strategy pattern we saw in Lecture 6 Subclasses become manageable as they are partitioned across multiple abstract types (FlyBehavior) Lots of polymorphic behavior is enabled since classes like Pentagon become customizable Reuse is enabled because Circle can plug these classes in as well This approach scales; one new abstract type, one concrete subclass for each new behavior that varies 22

Example: Rectangle and Square Shape Rectangle IS-A Shape Square IS-A Rectangle Is there a problem with this design? Rectangle Square 23

Example: Rectangle and Square What would happen if we did something like this? Shape List<Shape> shapes = (list of squares/rects) Rectangle // set width to 5; leave length the same for (Shape s: shapes) { s.setwidth(5); } Square 24

Example: Rectangle and Square Shape Squares share properties of rectangles but they don t BEHAVE the same If you set a square s width, you are also setting its length Rectangle Whereas with a Rectangle, setting width and length are independent operations Since we should use inheritance to group classes that behave the same, how should we change our design? Square 25

Example: Rectangle and Square Shape Since squares do not behave like Rectangles they no longer are a subclass Rectangle Square But since they share lots of properties, Square will keep a private copy of rectangle and delegate to rectangle when their properties or behaviors ARE the same Differences in behavior are then handled in Square itself 26

Commonality and Variability Analysis Answers the question What critical information do we need from a problem domain when we are performing analysis and design? What do we need to know in order to be effective at modeling the domain and the problem we ve been asked to tackle? Commonality Analysis identifies the major archetypes of a domain Look at ostensibly different objects and find a supertype Variability Analysis identifies how things vary Look at a supertype and identify variations 27

Example Objects whiteboard marker, pencil, ballpoint pen Commonality Analysis writing instruments Variability Analysis appearance varies, writing surface varies, ink varies 28

Commonality and Variability Analysis Variability only makes sense within a given commonality Commonality Analysis seeks structure in a problem domain that is unlikely to change over time Variability Analysis identifies the ways these common structures are likely to change A&D becomes locating common concepts (abstract superclasses) and their likely variations (concrete subclasses) The abstract classes identify important behavior (that fulfill responsibilities) within the domain; the subclasses outline the legal variations of that behavior 29

Comparison to Agile Techniques The approach to A&D advocated by this book is often called the design up front approach You identify the primary domain concepts relevant to solving the problem You identify the users of your system; You then develop a design that uses those domain concepts to allow your users to complete their tasks You iterate and flesh out the design until it is ready for implementation 30

Agile Techniques Agile methods are techniques/processes for developing software systems that rely on communicating with your customer frequently taking small steps (functionality wise) validating the small steps with the customer before moving on They emphasize iteration, feedback, and communication over upfront design, detailed analysis, diagrams, etc. 31

Opposition? These two techniques seem to be in opposition Up front design (top-down) vs. small steps (bottom-up) Yet, they are both driving towards the same goal systems built from effective, robust, flexible code They differ in approach but value the same things Design patterns produce flexible code; Agile values code that can change in a straightforward manner 32

Opposition? Not Really Agile techniques value characteristics in code that are valued by the design pattern approach No redundancy and Highly Cohesive Code Readability and Design to an Interface Testability and <all the other qualities> While the two techniques use different names for these characteristics, they are really talking about the same thing 33

No Redundancy When implementing code, don t repeat yourself One Rule, One Place: do not duplicate behavior Once and Only Once The system (code+tests) must communicate everything you want to communicate (about its responsibilites) The system must contain no duplicate code Code with no redundancy is highly cohesive and loosely coupled 34

Readability (I) Program by Intention You need to implement some functionality Pretend it exists, give it an intention-revealing name Write the method that calls it Write the method itself Code becomes a series of calls to functions with highly descriptive names 35

Readability (II) Martin Fowler encourages Program by Intention when he says Whenever [you] feel the need to [write a comment], write a method instead. This encourages shorter and more cohesive methods in cohesive classes Using intention-revealing names is very similar to Code to an Interface. By considering how the function is to be called/used before writing it, you establish its public interface 36

Testability Testability is key in software development The more you test the more confident you are in the software being developed Testable code (encouraged by Agile at every turn) is cohesive (doing only one thing), loosely coupled (less dependencies on a class may mean it is easier to instantiate its objects), non redundant (each rule to be tested lives in one place), readable (intention-revealing names make it easier to target test cases), encapsulated 37

Wrapping Up (I) New perspective on objects and encapsulation responsibilities; hide anything How to handle variation in behavior strategy pattern New perspective on inheritance Group via behavior 38

Wrapping Up (II) Commonality and Variability Analysis Examine problem domain for structure that are resistant to change (commonality) and then identify ways in which they can legally vary Relationship between Design Patterns and Agile They both value the same code qualities loose coupling, high cohesiveness, no redundancy, testability, readability, code to an interface, etc. 39

Coming Up Next Lecture 10: Strategy, Bridge, Abstract Factory Read Chapters 9, 10, & 11 Lecture 11: Introduction to Java No reading assignment 40