CS 4240: The OO Paradigm Revisited Readings: Chap. 1 of Design Patterns Explained OO (some review) Coupling, cohesion.

Slides:



Advertisements
Similar presentations
Structured Design. 2 Design Quality – Simplicity “There are two ways of constructing a software design: One is to make it so simple that there are obviously.
Advertisements

Communication between modules, cohesion and coupling
6-1 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer problem-solving process and relate it to Polya’s.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
OOAD Using the UML - Use-Case Analysis, v 4.2 Copyright  Rational Software, all rights reserved 1/18 Use Case Analysis – continued Control Classes.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
UML – Class Diagrams.
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Sharif University of Technology Session # 7.  Contents  Systems Analysis and Design  Planning the approach  Asking questions and collecting data 
Computer Science 240 Principles of Software Design.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Chapter 25 More Design Patterns.
Introduction To System Analysis and design
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CS2110: SW Development Methods Design of methods (functions) Class design – CRC cards – UML class and sequence diagrams Software Design.
CSCI-383 Object-Oriented Programming & Design Lecture 9.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
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.
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.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
CSE 219 Computer Science III Program Design Principles.
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.
SE: CHAPTER 7 Writing The Program
Cohesion and Coupling CS 4311
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
GRASP: Designing Objects with Responsibilities
Software Engineering Principles. SE Principles Principles are statements describing desirable properties of the product and process.
Structural Design Patterns
Kal Bugrara, Ph.DSoftware Engineering Northeastern University Fundamentals Of Software Engineering Lecture V.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Object Oriented Software Development
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Object-Oriented Analysis and Design CHAPTERS 9, 31: DOMAIN MODELS 1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Object-Oriented Programming Chapter Chapter
Lecture 13 Law of Demeter. Cohesion Cohesion: the “glue” that holds a module together. Don’t do things that do not support a common goal Cohesion: the.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
CS 4240: Rethinking Some OOP Ideas and Terms for OOA&D Readings: Chap. 8 in Shalloway and Trott (referred to as S&T in these slides) Wikipedia on information.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Design. Practices Principles Patterns What are the characteristics of good design? What are good solutions to common design problems? How do we go about.
Copyright © Craig Larman All Rights Reserved COMP-350 Object-Oriented Analysis and Design GRASP: Designing Objects with Responsibilities Reference:
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Object-Oriented Design Concepts University of Sunderland.
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Basic Characteristics of Object-Oriented Systems
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
CSCE 240 – Intro to Software Engineering Lecture 3.
ISBN Chapter 12 Support for Object-Oriented Programming.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Object Oriented Programming Some Interesting Genes.
Copyright © Jim Fawcett Spring 2017
GRASP – Designing Objects with Responsibilities
Design Patterns: MORE Examples
Algorithms and Problem Solving
Chapter 8 - Design Strategies
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Presentation transcript:

CS 4240: The OO Paradigm Revisited Readings: Chap. 1 of Design Patterns Explained OO (some review) Coupling, cohesion

Old problems Before OO, functional decomposition Break problem down Function deals with subproblem Hierarchical. Levels of abstraction Problems: Control and coordination centered in main method Required changes have big impacts Logic may be distributed Data changes affect may modules

Functions: What we call them: functions, methods, modules Functions are a core construct in OO and non-OO programs Principals of good functions apply in either language But less of a problem in OO – can you think why?

What makes a function good? Your answers:

Good qualities in functions: Lots written about this! Clean Code: A Handbook of Agile SW Craftsmanship Robert C. Martin Read Chapter 3 in UVa digital library version

Good Qualities in Functions Small How small? Do One Thing: Strong Cohesion Why? One level of abstraction per function Thus, functions fall into levels of abstraction Name reflects the one task it does

Interface Qualities of Functions What about its interface? Inputs, return value/arguments Other sources of data or “output” Side effects Some rules?

Good Qualities in Functions (2) Number of arguments: small “Bundling” arguments Flag arguments What are they? What bad thing do they suggest is happening? Avoid side effects Avoid output arguments

Good Qualities in Functions (3) Don’t Repeat Yourself (DRY) Command/Query Separation Do something. Or answer something. Don’t do both. Returning Errors Return value? Output argument? Burden on caller? Exceptions: what advantages?

Design Representation If you’re just programming with functions (not OO), how could you represent your system?

Example: Structure Chart

Comments on Struct. Chart How are data and functions tied together? Can procedural abstraction be done? Data abstraction? How sensitive to change are parts of this design? Where? Page 8 of DPE text: can a function cope with variability in data? (Will this be an issue?)

Back to Shalloway and Trott…

Requirements See textbook’s discussion on requirements and why they change. Bottom line: Change is inevitable. Deal with it.

Design Principles (again) Decomposition leads to modularity Properties of modules: Internal “goodness” Inter-module relationships Cohesion Coupling Note: old terms first defined for function- oriented modules

Cohesion of a module “How closely operations [what’s encapsulated] in a module are related.” Think of: Strength of purpose A cohesive group of people works well together towards a goal Extreme of non-cohesive OO module: a god class

Coupling between modules “strength [or goodness] of the connection between two modules.” We want quality connections. Why? Flexibility, independence of modules “Looser” connections, less brittle system Reusability Goal: Loosely-coupled modules, each highly cohesive

Coupling: What’s “Good”? How do modules “connect”? Invoke operations in each other [dynamic] Small operations, or start/invoke something larger Pass data to each other [dynamic] Defined in terms of each other [static] General: Simple, as little as possible (data) Direct connections, very visible Flexible E.g. report an event, but not tell the other module how to handle it

Back to Functional Decomposition Side effects Finding bugs is the problem, not fixing them Change to data impact many functions Snowball effect, cascade of changes

OO Improves This Because Modules become responsible for: Encapsulating data Controlling access, maintaining intergrity Encapsulating operations on data Functionality is bound to data Why is this an example of DRY principle? For a modules in OO, we define: Responsibilities, data, operations

Classes in Java, C++ “My responsibilities?” What my role is! Clear how data and operations are defined when we write a Java class, but what about responsibilities? Implied by operations? Answer: Note in the code. Maybe in the comments. Certainly in the design (even if implicit) Responsibilities matter a lot in understanding the design.

Step back: Perspectives of modules Martin Fowler (UML Distilled) talks about three levels of perspectives to talk about modules (or SW development) Note we’re practicing abstraction in doing this! Explain why to me later on! Note: it’s easier to understand this if you think about limiting it to objects for now

Levels of perspective in OO Conceptual Domain-level, problem-level -- not yet considering solutions What is an object’s responsibilities? Specification Solution-level, but an abstract view Interfaces, not internal implementation Implementation Code level: full details of how it’s coded

So… What’s an Object? Depends on what level you’re using, where you’re at in development Conceptual: set of responsibilities Specification: set of methods (an interface) that meets its responsibilities Implementation: coding you’ve learned to do Designers work at Specification level (mostly) Analysts work at Conceptual level (mostly)

Classes, Objects and Instantiation Review! What’s an instance? What’s instantiation? Do we need classes to do OO programming? No. But why are they helpful? Define common properties. (DRY again.)

Abstract Types Review: abstract class vs. interface How are they used? What’s common about them? Explain: Collections of these. References to these.

Review: polymorphism What are the mechanics of polymorphism in OO? (Implementation level perspective) At a higher level, what’s it for?

More (not from text) More on coupling, cohesion

Cohesion How diverse are the things inside an “entity” A what? Module, function,… In OO a class. What’s this mean? Class should represent a single abstraction Or, it should address a single general responsibility

Problems Created by Bad Cohesion Hard to understand the class If two abstractions grouped into one class, that implies a one-to-one relationship What if this changes? Often we specialize a class along a dimension This new thing is like the existing one except we extend it in one area (dimension) Problems arise when each of the several abstractions need such specialization Note meaning of “specialization” here

The “Multiplicity” Problem Consider an Account class that holds: Customer name, address, tax ID, Account status, etc. What if one customer needs two accounts? Two Account objects, but each stores name and address What if one account has two owners? You can’t do this, unless you create a collection in each Account to hold owner info

Specializing along Dimensions Let’s say we need variations on class Account First, based on account type: Cash Account, Credit Account Second, based on customer type: Individual Account, Institutional Account These are two dimensions, but are they mutually exclusive? We often compose along two dimensions E.g. Individual Cash Account, Individual Credit Account, etc. Specialization often implemented as inheritance: Do we really want multiple inheritance?

Inheritance Diamonds Structures like this cause messy problems!

Separating Abstractions Composition across dimensions achieved by aggregation (“PART-OF”) You can see how this improves earlier problem too

An OO Design Principle… Prefer aggregation over inheritance! Keep in mind: Often at the conceptual perspective, the “IS-A” relationship is true But at the design/implementation level, we don’t use inheritance

How to Achieve Better Cohesion Some of this is just good OO experience We can learn from database normalization Eliminate redundancy Attributes should have a single value and should not have structure (repeating groups of things) Attributes always describe an instance of its containing class That’s what attributes are all about! State values that define a particular instance Note: there are always tradeoffs! Sometimes we combine abstractions into one class for efficiency.

Coupling and Class Design How dependent an object/class is on the world around it How many connections Nature of the connections Will changes cause a “ripple effect”? Our goals: Reduce coupling if possible Improve nature of necessary coupling

Forms of Coupling (from Richter) Identity Coupling An object contains a reference or pointer to another object Eliminate associations or make them one- way Representational Coupling An object refers to another through that object’s interface How it does this affects the degree of coupling

Forms of Coupling (cont’d) Subclass Coupling Object refers to another object using a subclass reference for that object Not the more general superclass or interface A client should refer to the most general type possible Why? Subclasses may be added later, possibly by someone else Try to write code that minimizes dependencies on subclass details Instead rely on the common interface defined in the superclass or interface

Reminder: Use Java Interfaces to Avoid Subclass Coupling Java’s interfaces; C++ classes with pure virtual functions and no data members Interfaces define a role not a class- abstraction Many classes can pay that role THE POINT: We can define reference to a thing in terms of the role (interface) instead of the class type

Forms of Coupling (cont’d) Inheritance coupling A subclass is coupled to its superclass at compile-time In general, prefer late to early Seems like the only way to do things, but ask: While the program executes, does an object need to change its subclass? Aggregation is supported at run-time We’ll see the State design pattern later

Shy Code and the Law of Demeter See handout on the Law of Demeter Summary: An object’s method should only call other methods that belong to: Itself (the current object) Any parameter object that was passed to it An object it created Any of its components objects What does this rule out? (Ponder that.) Delegation

END