Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 7.1 © The McGraw-Hill Companies, 2002 Object-Oriented and Classical Software Engineering Fifth Edition, WCB/McGraw-Hill, 2002 Stephen R. Schach

Similar presentations


Presentation on theme: "Slide 7.1 © The McGraw-Hill Companies, 2002 Object-Oriented and Classical Software Engineering Fifth Edition, WCB/McGraw-Hill, 2002 Stephen R. Schach"— Presentation transcript:

1 Slide 7.1 © The McGraw-Hill Companies, 2002 Object-Oriented and Classical Software Engineering Fifth Edition, WCB/McGraw-Hill, 2002 Stephen R. Schach srs@vuse.vanderbilt.edu

2 Slide 7.2 © The McGraw-Hill Companies, 2002 CHAPTER 7 INTRODUCTION TO OBJECTS

3 Slide 7.3 © The McGraw-Hill Companies, 2002 Overview l What is a module? l Cohesion l Coupling l Data encapsulation product maintenance l Abstract data types l Information hiding l Objects l Inheritance, polymorphism and dynamic binding l Cohesion and coupling of objects

4 Slide 7.4 © The McGraw-Hill Companies, 2002 Introduction to Objects What is a module? –A lexically contiguous sequence of program statements, bounded by boundary elements, with an aggregate identifier

5 Slide 7.5 © The McGraw-Hill Companies, 2002 Design of Computer l A highly incompetent computer architect decides to build an ALU, shifter and 16 registers with AND, OR, and NOT gates, rather than NAND or NOR gates.

6 Slide 7.6 © The McGraw-Hill Companies, 2002 Design of Computer (contd) l Architect designs 3 silicon chips

7 Slide 7.7 © The McGraw-Hill Companies, 2002 Design of Computer (contd) l Redesign with one gate type per chip l Resulting “masterpiece”

8 Slide 7.8 © The McGraw-Hill Companies, 2002 Computer Design (contd) l The two designs are functionally equivalent –Second design is »Hard to understand »Hard to locate faults »Difficult to extend or enhance »Cannot be reused in another product l Modules must be like the first design –Maximal relationships within modules, minimal relationships between modules

9 Slide 7.9 © The McGraw-Hill Companies, 2002 Composite/Structured Design Method for breaking up a product into modules for –Maximal interaction within module, and –Minimal interaction between modules l Module cohesion –Degree of interaction within a module l Module coupling –Degree of interaction between modules

10 Slide 7.10 © The McGraw-Hill Companies, 2002 Function, Logic, and Context of module l In C/SD, the name of a module is its function l Example –Module computes square root of double precision integers using Newton’s algorithm. Module is named compute square root

11 Slide 7.11 © The McGraw-Hill Companies, 2002 Cohesion l Degree of interaction within a module l Seven categories or levels of cohesion (non-linear scale)

12 Slide 7.12 © The McGraw-Hill Companies, 2002 1.Coincidental Cohesion l A module has coincidental cohesion if it performs multiple, completely unrelated actions l Example –print next line, reverse string of characters comprising second parameter, add 7 to fifth parameter, convert fourth parameter to floating point l Arise from rules like –“Every module will consist of between 35 and 50 statements”

13 Slide 7.13 © The McGraw-Hill Companies, 2002 Why Is Coincidental Cohesion So Bad? l Degrades maintainability l Modules are not reusable l This is easy to fix –Break into separate modules each performing one task

14 Slide 7.14 © The McGraw-Hill Companies, 2002 2.Logical Cohesion l A module has logical cohesion when it performs a series of related actions, one of which is selected by the calling module l Example 1 function code = 7; new operation (op code, dummy 1, dummy 2, dummy 3); // dummy 1, dummy 2, and dummy 3 are dummy variables, // not used if function code is equal to 7 l Example 2 –Module performing all input and output l Example 3 –One version of OS/VS2 contained logical cohesion module performing 13 different actions. Interface contained 21 pieces of data

15 Slide 7.15 © The McGraw-Hill Companies, 2002 Why Is Logical Cohesion So Bad? l The interface is difficult to understand l Code for more than one action may be intertwined l Difficult to reuse

16 Slide 7.16 © The McGraw-Hill Companies, 2002 Why Is Logical Cohesion So Bad? (contd) l A new tape unit is installed l What is the effect on the laser printer?

17 Slide 7.17 © The McGraw-Hill Companies, 2002 3.Temporal Cohesion l A module has temporal cohesion when it performs a series of actions related in time l Example –open old master file, new master file, transaction file, print file, initialize sales district table, read first transaction record, read first old master record (a.k.a. perform initialization)

18 Slide 7.18 © The McGraw-Hill Companies, 2002 Why Is Temporal Cohesion So Bad? l Actions of this module are weakly related to one another, but strongly related to actions in other modules. –Consider sales district table l Not reusable

19 Slide 7.19 © The McGraw-Hill Companies, 2002 4.Procedural Cohesion l A module has procedural cohesion if it performs a series of actions related by the procedure to be followed by the product l Example –read part number and update repair record on master file

20 Slide 7.20 © The McGraw-Hill Companies, 2002 Why Is Procedural Cohesion So Bad? l Actions are still weakly connected, so module is not reusable

21 Slide 7.21 © The McGraw-Hill Companies, 2002 5.Communicational Cohesion l A module has communicational cohesion if it performs a series of actions related by the procedure to be followed by the product, but in addition all the actions operate on the same data l Example 1 –update record in database and write it to audit trail l Example 2 –calculate new coordinates and send them to terminal

22 Slide 7.22 © The McGraw-Hill Companies, 2002 Why Is Communicational Cohesion So Bad? l Still lack of reusability

23 Slide 7.23 © The McGraw-Hill Companies, 2002 7.Informational Cohesion l A module has informational cohesion if it performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data structure

24 Slide 7.24 © The McGraw-Hill Companies, 2002 Why Is Informational Cohesion So Good? l Essentially, this is an abstract data type (see later)

25 Slide 7.25 © The McGraw-Hill Companies, 2002 7.Functional Cohesion l Module with functional cohesion performs exactly one action l Example 1 –get temperature of furnace l Example 2 –compute orbital of electron l Example 3 –write to floppy disk l Example 4 –calculate sales commission

26 Slide 7.26 © The McGraw-Hill Companies, 2002 Why is functional cohesion so good? l More reusable l Corrective maintenance easier –Fault isolation –Fewer regression faults l Easier to extend product

27 Slide 7.27 © The McGraw-Hill Companies, 2002 Cohesion Case Study

28 Slide 7.28 © The McGraw-Hill Companies, 2002 Coupling l Degree of interaction between two modules l Five categories or levels of coupling (non-linear scale)

29 Slide 7.29 © The McGraw-Hill Companies, 2002 1.Content Coupling l Two modules are content coupled if one directly references contents of the other l Example 1 –Module a modifies statement of module b l Example 2 –Module a refers to local data of module b in terms of some numerical displacement within b l Example 3 –Module a branches into local label of module b

30 Slide 7.30 © The McGraw-Hill Companies, 2002 Why Is Content Coupling So Bad? l Almost any change to b, even recompiling b with new compiler or assembler, requires change to a l Warning –Content coupling can be implemented in Ada through use of overlays implemented via address clauses

31 Slide 7.31 © The McGraw-Hill Companies, 2002 2.Common Coupling l Two modules are common coupled if they have write access to global data l Example 1 –Modules cca and ccb can access and change value of global variable

32 Slide 7.32 © The McGraw-Hill Companies, 2002 2.Common Coupling (contd) l Example 2 –Modules cca and ccb both have access to same database, and can both read and write same record l Example 3 –FORTRAN common –COBOL common (nonstandard) –COBOL-80 global

33 Slide 7.33 © The McGraw-Hill Companies, 2002 Why Is Common Coupling So Bad? l Contradicts the spirit of structured programming –The resulting code is virtually unreadable

34 Slide 7.34 © The McGraw-Hill Companies, 2002 Why Is Common Coupling So Bad? (contd) l Modules can have side-effects –This affects their readability l Entire module must be read to find out what it does l Difficult to reuse l Module exposed to more data than necessary

35 Slide 7.35 © The McGraw-Hill Companies, 2002 3.Control Coupling l Two modules are control coupled if one passes an element of control to the other l Example 1 –Operation code passed to module with logical cohesion l Example 2 –Control-switch passed as argument

36 Slide 7.36 © The McGraw-Hill Companies, 2002 Why Is Control Coupling So Bad? Modules are not independent; module b (the called module) must know internal structure and logic of module a. –Affects reusability l Associated with modules of logical cohesion

37 Slide 7.37 © The McGraw-Hill Companies, 2002 4.Stamp Coupling l Some languages allow only simple variables as parameters –part number –satellite altitude –degree of multiprogramming l Many languages also support passing of data structures –part record –satellite coordinates –segment table

38 Slide 7.38 © The McGraw-Hill Companies, 2002 4.Stamp Coupling (contd) l Two modules are stamp coupled if a data structure is passed as a parameter, but the called module operates on some but not all of the individual components of the data structure

39 Slide 7.39 © The McGraw-Hill Companies, 2002 Why Is Stamp Coupling So Bad? l It is not clear, without reading the entire module, which fields of a record are accessed or changed –Example calculate withholding (employee record) l Difficult to understand l Unlikely to be reusable l More data than necessary is passed –Uncontrolled data access can lead to computer crime l There is nothing wrong with passing a data structure as a parameter, provided all the components of the data structure are accessed and/or changed invert matrix (original matrix, inverted matrix); print inventory record (warehouse record);

40 Slide 7.40 © The McGraw-Hill Companies, 2002 5.Data Coupling l Two modules are data coupled if all parameters are homogeneous data items [simple parameters, or data structures all of whose elements are used by called module] l Examples –display time of arrival (flight number); –compute product (first number, second number); –get job with highest priority (job queue);

41 Slide 7.41 © The McGraw-Hill Companies, 2002 Why Is Data Coupling So Good? l The difficulties of content, common, control, and stamp coupling are not present l Maintenance is easier

42 Slide 7.42 © The McGraw-Hill Companies, 2002 Coupling Case Study

43 Slide 7.43 © The McGraw-Hill Companies, 2002 Coupling Case Study (contd) l Interface description

44 Slide 7.44 © The McGraw-Hill Companies, 2002 Coupling Case Study (contd) l Coupling between all pairs of modules

45 Slide 7.45 © The McGraw-Hill Companies, 2002 Coupling Case Study (contd) l Good design has high cohesion and low coupling –What else characterizes good design?

46 Slide 7.46 © The McGraw-Hill Companies, 2002 Summary

47 Slide 7.47 © The McGraw-Hill Companies, 2002 Data Encapsulation l Example –Design an operating system for a large mainframe computer. It has been decided that batch jobs submitted to the computer will be classified as high priority, medium priority, or low priority. There must be three queues for incoming batch jobs, one for each job type. When a job is submitted by a user, the job is added to the appropriate queue, and when the operating system decides that a job is ready to be run, it is removed from its queue and memory is allocated to it l Design 1 (Next slide) –Low cohesion—operations on job queues are spread all over product

48 Slide 7.48 © The McGraw-Hill Companies, 2002 Data Encapsulation — Design 1

49 Slide 7.49 © The McGraw-Hill Companies, 2002 Data Encapsulation — Design 2

50 Slide 7.50 © The McGraw-Hill Companies, 2002 Data Encapsulation l m_encapsulation has informational cohesion l m_encapsulation is an implementation of data encapsulation –Data structure ( job_queue ) together with operations performed on that data structure l Advantages –Development –Maintenance

51 Slide 7.51 © The McGraw-Hill Companies, 2002 Data Encapsulation and Development l Data encapsulation is an example of abstraction l Job queue example –Data structure »job_queue –Three new functions »i nitialize_job_queue »add_job_to_queue »delete_job_from_queue l Abstraction –Conceptualize problem at higher level »job queues and operations on job queues –not lower level »records or arrays

52 Slide 7.52 © The McGraw-Hill Companies, 2002 Stepwise Refinement l 1.Design in terms of high level concepts –It is irrelevant how job queues are implemented l 2.Design low level components –Totally ignore what use will be made of them l In 1st step, assume existence of lower level –Concern is the behavior of the data structure »job_queue l In 2nd step, ignore existence of high level –Concern is the implementation of that behavior l In a larger product, there will be many levels of abstraction

53 Slide 7.53 © The McGraw-Hill Companies, 2002 Data Encapsulation and Maintenance l Identify aspects of product likely to change l Design product so as to minimize the effects of change –Data structures are unlikely to change –Implementation may change l Data encapsulation provides a way to cope with change

54 Slide 7.54 © The McGraw-Hill Companies, 2002 Implementation of Class JobQueue C++ Java

55 Slide 7.55 © The McGraw-Hill Companies, 2002 Implementation of queueHandler C++ Java

56 Slide 7.56 © The McGraw-Hill Companies, 2002 Data Encapsulation and Maintenance (contd) l What happens if queue is now implemented as a two-way linked list of JobRecord ? –Module that uses JobRecord need not be changed at all, merely recompiled – C++ Java

57 Slide 7.57 © The McGraw-Hill Companies, 2002 Abstract Data Types Problem with both implementations –Only one queue l Need –We need: Data type + operations performed on instantiations of that data type l Abstract data type

58 Slide 7.58 © The McGraw-Hill Companies, 2002 Abstract Data Type l (Problems caused by public attributes solved later)

59 Slide 7.59 © The McGraw-Hill Companies, 2002 Information Hiding l Data abstraction –Designer thinks at level of an ADT l Procedural abstraction –Define a procedure—extend the language l Instances of a more general design concept, information hiding –Design the modules in way that items likely to change are hidden –Future change is localized –Changes cannot affect other modules

60 Slide 7.60 © The McGraw-Hill Companies, 2002 Information Hiding (contd) l C++ abstract data type implementation with information hiding

61 Slide 7.61 © The McGraw-Hill Companies, 2002 Information Hiding (contd) l Effect of information hiding via private attributes

62 Slide 7.62 © The McGraw-Hill Companies, 2002 Major Concepts of Chapter 7

63 Slide 7.63 © The McGraw-Hill Companies, 2002 Objects l First refinement –Product is designed in terms of abstract data types –Variables (“objects”) are instantiations of abstract data types l Second refinement –Class: abstract data type that supports inheritance –Objects are instantiations of classes

64 Slide 7.64 © The McGraw-Hill Companies, 2002 Inheritance l Define humanBeing to be a class –A humanBeing has attributes, such as age, height, gender –Assign values to attributes when describing object l Define Parent to be a subclass of HumanBeing –A Parent has all attributes of a HumanBeing, plus attributes of his/her own ( name of oldest child, number of children ) –A Parent inherits all attributes of humanBeing l The property of inheritance is an essential feature of object-oriented languages such as Smalltalk, C++, Ada 95, Java (but not C, FORTRAN)

65 Slide 7.65 © The McGraw-Hill Companies, 2002 Inheritance (contd) l UML notation –Inheritance is represented by a large open triangle

66 Slide 7.66 © The McGraw-Hill Companies, 2002 Java implementation

67 Slide 7.67 © The McGraw-Hill Companies, 2002 Aggregation l UML Notation

68 Slide 7.68 © The McGraw-Hill Companies, 2002 Association l UML Notation

69 Slide 7.69 © The McGraw-Hill Companies, 2002 Equivalence of Data and Action l Classical paradigm –record_1.field_2 l Object-oriented paradigm –thisObject.attributeB –thisObject.methodC ()

70 Slide 7.70 © The McGraw-Hill Companies, 2002 Polymorphism and Dynamic Binding l Classical paradigm –Must explicitly invoke correct version

71 Slide 7.71 © The McGraw-Hill Companies, 2002 Polymorphism and Dynamic Binding (contd) l Object-oriented paradigm

72 Slide 7.72 © The McGraw-Hill Companies, 2002 Polymorphism and Dynamic Binding (contd) l All that is needed is myFile.open() –Correct method invoked at run-time (dynamically) l Method open can be applied to objects of different classes –Polymorphic

73 Slide 7.73 © The McGraw-Hill Companies, 2002 Polymorphism and dynamic binding (contd) Method checkOrder (b : Base) can be applied to objects of any subclass of Base

74 Slide 7.74 © The McGraw-Hill Companies, 2002 Polymorphism and Dynamic Binding (contd) l Can have a negative impact on maintenance –Code is hard to understand if there are multiple possibilities for a specific method l Polymorphism and dynamic binding –Strength and weakness of the object-oriented paradigm

75 Slide 7.75 © The McGraw-Hill Companies, 2002 Cohesion and Coupling of Objects l No such thing! –Object-oriented cohesion and coupling always reduces to classical cohesion l The only feature unique to the object-oriented paradigm is inheritance –Cohesion has nothing to do with inheritance –Two objects with the same functionality have the same cohesion –It does not matter if this functionality is inherited or not –Similarly, so-called object-oriented coupling always reduces to classical coupling

76 Slide 7.76 © The McGraw-Hill Companies, 2002 Object-Oriented Metrics (contd) l Two types of so-called object-oriented metric –Not related to inheritance »Reduces to a classical metric –Inheritance-related »May reduce to a classical metric l No problem –Classical metrics work just fine –But don’t mislead others by calling them object- oriented

77 Slide 7.77 © The McGraw-Hill Companies, 2002 Summary

78 Slide 7.78 © The McGraw-Hill Companies, 2002 Advantages of Objects l Same as as advantages of abstract data types –Information hiding –Data abstraction –Procedural abstraction l Inheritance provides further data abstraction –Easier and less error-prone product development –Easier maintenance l Objects are more reusable than modules with functional cohesion –(See next chapter)


Download ppt "Slide 7.1 © The McGraw-Hill Companies, 2002 Object-Oriented and Classical Software Engineering Fifth Edition, WCB/McGraw-Hill, 2002 Stephen R. Schach"

Similar presentations


Ads by Google