Download presentation
Presentation is loading. Please wait.
Published byBennett Stephens Modified over 9 years ago
1
Generalization
2
Example CachPaymentCreditPaymentCheckPayment Paymentamount sale Attribute Relationship Each sub class inherit the attribute and Relationship in super class in super class
3
Representing Generalization GeneralizationGeneralization Abstraction of common features among multiple classes, as well as their relationships, into a more general classAbstraction of common features among multiple classes, as well as their relationships, into a more general class SubclassSubclass A class that has been generalizedA class that has been generalized SuperclassSuperclass A class that is composed of several generalized subclassesA class that is composed of several generalized subclasses A.3
4
Is – a Rule All the members of sub type set must be member of their super type setAll the members of sub type set must be member of their super type set In natural language this can usually be infomally tested by forming the statementIn natural language this can usually be infomally tested by forming the statement Subtype is a Super typeSubtype is a Super type
5
Representing Generalization DiscriminatorDiscriminator Shows which property of an object class is being abstracted by a generalization relationshipShows which property of an object class is being abstracted by a generalization relationship InheritanceInheritance A property that a subclass inherits the features from its superclassA property that a subclass inherits the features from its superclass Abstract ClassAbstract Class A class that has no direct instances, but whose descendents may have direct instancesA class that has no direct instances, but whose descendents may have direct instances Concrete ClassConcrete Class A class that can have direct instancesA class that can have direct instances A.5
6
A.6
7
A.7
8
Representing Aggregation AggregationAggregation A part-of relationship between a component object and an aggregate objectA part-of relationship between a component object and an aggregate object Example: Personal computerExample: Personal computer Composed of CPU, Monitor, Keyboard, etcComposed of CPU, Monitor, Keyboard, etc A.8
9
Coupling and Cohesion
10
Characteristics of Good Design Component independenceComponent independence High cohesionHigh cohesion Low couplingLow coupling Exception identification and handlingException identification and handling Fault prevention and fault toleranceFault prevention and fault tolerance
11
Slide 11 Coupling The interdependence or interrelationships of the modulesThe interdependence or interrelationships of the modules Interaction couplingInteraction coupling Relationships with methods and objectsRelationships with methods and objects through message passingthrough message passing
12
Coupling criteria Size - refers to the number of connections between modules, it is less work to connect other modules to a module with a smaller interface
13
Coupling: Degree of dependence among components No dependencies Loosely coupled-some dependencies Highly coupled-many dependencies High coupling makes modifying parts of the system difficult, e.g., modifying a component affects all the components to which the component is connected.
14
Levels of coupling High Coupling High Coupling Low Coupling
15
Slide 15 Types of Interaction Coupling LevelTypeDescription Good No Direct Coupling Methods don't call each other Data Calling method passes object, called method uses the entire object Stamp Calling method passes object, called method uses only part of the object Control Calling method passes a control variable whose value controls the execution of the called method Common or Global Methods refer to a "global data area" that is outside of the individual objects Bad Content or Pathological A method of one object refers to the hidden part of another object.
16
Content coupling Definition: One component references contents of anotherDefinition: One component references contents of another Example:Example: Component directly modifies another’s dataComponent directly modifies another’s data Component refers to local data of another component in terms of numerical displacementComponent refers to local data of another component in terms of numerical displacement Component modifies another’s code, e.g., jumps into the middle of a routineComponent modifies another’s code, e.g., jumps into the middle of a routine
17
Example of Content Coupling-1 Part of program handles lookup for customer. When customer not found, component adds customer by directly modifying the contents of the data structure containing customer data.
18
Example of Content Coupling-2 Part of program handles lookup for customer. When customer not found, component adds customer by directly modifying the contents of the data structure containing customer data. Improvement: When customer not found, component calls the AddCustomer() method that is responsible for maintaining customer data.
19
Common Coupling Definition: Two components share dataDefinition: Two components share data Global data structuresGlobal data structures Common blocksCommon blocks Usually a poor design choice becauseUsually a poor design choice because Lack of clear responsibility for the dataLack of clear responsibility for the data Reduces readabilityReduces readability Difficult to determine all the components that affect a data element (reduces maintainability)Difficult to determine all the components that affect a data element (reduces maintainability) Difficult to reuse componentsDifficult to reuse components Reduces ability to control data accessesReduces ability to control data accesses
20
Example-1 Each source process writes directly to global data store. Each sink process reads directly from global data store. Process control component maintains current data about state of operation. Gets data from multiple sources. Supplies data to multiple sinks.
21
Example-2 Each source process writes directly to global data store. Each sink process reads directly from global data store. Improvement Data manager component is responsible for data in data store. Processes send data to and request data from data manager. Data manager component is responsible for data in data store. Processes send data to and request data from data manager. Process control component maintains current data about state of operation. Gets data from multiple sources. Supplies data to multiple sinks.
22
Control Coupling Definition: Component passes control parameters to coupled components.Definition: Component passes control parameters to coupled components. May be either good or bad, depending on situation.May be either good or bad, depending on situation. Bad when component must be aware of internal structure and logic of another moduleBad when component must be aware of internal structure and logic of another module Good if parameters allow factoring and reuse of functionalityGood if parameters allow factoring and reuse of functionality
23
Example Acceptable: Module p calls module q and q passes back flag that says it cannot complete the task, then q is passing dataAcceptable: Module p calls module q and q passes back flag that says it cannot complete the task, then q is passing data Not Acceptable: Module p calls module q and q passes back flag that says it cannot complete the task and, as a result, writes a specific message.Not Acceptable: Module p calls module q and q passes back flag that says it cannot complete the task and, as a result, writes a specific message.
24
Stamp Coupling One component passes more data then needed to another One component passes more data then needed to another void swap(int v[], int i, int j); void swap(int v[], int i, int j); double calcsalary(Employee& e); double calcsalary(Employee& e); ● Often involves records (structs) with lots of fields ● Entire record is passed, but only a few fields are used
25
Example-1 The print routine of the customer billing accepts a customer data structure as an argument, parses it, and prints the name, address, and billing information. Customer billing system
26
Example-2 The print routine of the customer billing accepts a customer data structure as an argument, parses it, and prints the name, address, and billing information. Improvement The print routine takes the customer name, address, and billing information as an argument. Customer Billing System
27
Data Coupling Definition: Two components are data coupled if there are homogeneous data items.Definition: Two components are data coupled if there are homogeneous data items. Every argument is simple argument or data structure in which all elements are usedEvery argument is simple argument or data structure in which all elements are used Good, if it can be achieved.Good, if it can be achieved. Easy to write contracts for this and modify component independently.Easy to write contracts for this and modify component independently.
28
Key Idea in Object-Oriented Programming Object-oriented designs tend to have low coupling.Object-oriented designs tend to have low coupling.
29
Cohesion Definition: The degree to which all elements of a component are directed towards a single task and all elements directed towards that task are contained in a single component.Definition: The degree to which all elements of a component are directed towards a single task and all elements directed towards that task are contained in a single component. Internal glue with which component is constructedInternal glue with which component is constructed All elements of component are directed toward and essential for performing the same taskAll elements of component are directed toward and essential for performing the same task High is goodHigh is good
30
Slide 30 Cohesion “Single-mindedness of a module”“Single-mindedness of a module” Method cohesionMethod cohesion A method should do just a single taskA method should do just a single task Class cohesionClass cohesion A class should represent just one thingA class should represent just one thing Generalization/specialization cohesionGeneralization/specialization cohesion Addresses inheritanceAddresses inheritance Should represent "a-kind-of" or "is-a"Should represent "a-kind-of" or "is-a"
31
Levels of cohesion High Cohesion High Cohesion Low Cohesion
32
Slide 32 Types of Method Cohesion LevelTypeDescription GoodFunctional A method performs a single task Sequential Method performs two tasks, the output of the first is the input of the second Communicational Method combines two functions that use the same attributes (calc current and final GPA) Procedural Method supports many loosely related functions (calc current GPA, print, calc final GPA, print) Temporal or Classical Method supports multiple functions related in time (initialize all variable, print all reports) Logical Method supports many functions controlled by a passed control variable BadCoincidental Method's purpose cannot be determined, or it supports multiple unrelated functions
33
Slide 33 Types of Class Cohesion LevelTypeDescription GoodIdeal Class has none of the mixed cohesions Mixed-Role Class has some attributes that relate to other classes on the same layer, but the attributes are unrelated to the semantics of the class Mixed-Domain Class has some attributes that relate to classes on a different layer. WorseMixed-Instance Class represents two different types of objects. Should be decomposed into two classes.
34
Coincidental Cohesion Definition: Parts of the component are only related by their location in source codeDefinition: Parts of the component are only related by their location in source code Elements needed to achieve some functionality are scattered throughout the system.Elements needed to achieve some functionality are scattered throughout the system. AccidentalAccidental Worst formWorst form
35
Example Print next linePrint next line Reverse string of characters in second argumentReverse string of characters in second argument Add 7 to 5 th argumentAdd 7 to 5 th argument Convert 4 th argument to floatConvert 4 th argument to float
36
Logical Cohesion Definition: Elements of component are related logically and not functionally.Definition: Elements of component are related logically and not functionally. Several logically related elements are in the same component and one of the elements is selected by the client component.Several logically related elements are in the same component and one of the elements is selected by the client component.
37
Example-1 A component reads inputs from tape, disk, and network. All the code for these functions are in the same component.A component reads inputs from tape, disk, and network. All the code for these functions are in the same component. Operations are related, but the functions are significantly different.Operations are related, but the functions are significantly different.
38
Example-2 A component reads inputs from tape, disk, and network. All the code for these functions are in the same component. Operations are related, but the functions are significantly different.A component reads inputs from tape, disk, and network. All the code for these functions are in the same component. Operations are related, but the functions are significantly different.Improvement A device component has a read operation that is overridden by sub-class components. The tape sub-class reads from tape. The disk sub-class reads from disk. The network sub-class reads from the network.A device component has a read operation that is overridden by sub-class components. The tape sub-class reads from tape. The disk sub-class reads from disk. The network sub-class reads from the network.
39
Temporal Cohesion Definition: Elements of a component are related by timing.Definition: Elements of a component are related by timing. Difficult to change because you may have to look at numerous components when a change in a data structure is made.Difficult to change because you may have to look at numerous components when a change in a data structure is made. Increases chances of regression faultIncreases chances of regression fault Component unlikely to be reusable.Component unlikely to be reusable.
40
Example-1 A system initialization routine: this routine contains all of the code for initializing all of the parts of the system. Lots of different activities occur, all at init time.A system initialization routine: this routine contains all of the code for initializing all of the parts of the system. Lots of different activities occur, all at init time.
41
Example-2 Improvement A system initialization routine sends an initialization message to each component.A system initialization routine sends an initialization message to each component. Each component initializes itself at component instantiation time.Each component initializes itself at component instantiation time.
42
Procedural Cohesion Definition: Elements of a component are related only to ensure a particular order of execution.Definition: Elements of a component are related only to ensure a particular order of execution. Actions are still weakly connected and unlikely to be reusableActions are still weakly connected and unlikely to be reusable
43
Example... Read part number from data base update repair record on maintenance file.... May be useful to abstract the intent of this sequence. Make the data base and repair record components handle reading and updating. Make component that handles more abstract operation. May be useful to abstract the intent of this sequence. Make the data base and repair record components handle reading and updating. Make component that handles more abstract operation.
44
Communicational Cohesion Definition: Module performs a series of actions related by a sequence of steps to be followed by the product and all actions are performed on the same dataDefinition: Module performs a series of actions related by a sequence of steps to be followed by the product and all actions are performed on the same data
45
Example Update record in data base and send it to the printer.Update record in data base and send it to the printer. database.Update (record). database.Update (record). record.Print(). record.Print().
46
Sequential Cohesion The output of one component is the input to another.The output of one component is the input to another. Occurs naturally in functional programming languagesOccurs naturally in functional programming languages Good situationGood situation
47
Informational Cohesion Definition: Module performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data.Definition: Module performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data. Different from logical cohesionDifferent from logical cohesion Each piece of code has single entry and single exitEach piece of code has single entry and single exit In logical cohesion, actions of module intertwinedIn logical cohesion, actions of module intertwined ADT and object-oriented paradigm promoteADT and object-oriented paradigm promote
48
Functional Cohesion Definition: Every essential element to a single computation is contained in the component.Definition: Every essential element to a single computation is contained in the component. Every element in the component is essential to the computation.Every element in the component is essential to the computation. Ideal situation.Ideal situation.
49
Cohesion in coding refers to the degree to which component’s instructions are functionally related
50
Levels of cohesion Coincidental cohesion occurs when the operations in a routine have no discernable relationship to each other
51
Levels of cohesion - coincidental functions have little or no relationship to one another
52
Levels of cohesion - coincidental multiple completely unrelated actions short printNextLine(String inStr, short x, short y) { short printNextLine(String inStr, short x, short y) { System.out.println(inStr); System.out.println(inStr); resetScreen(); resetScreen(); return x+y; return x+y; } Can you tell what printNextLine intend to do?
53
Levels of cohesion Logical cohesion occurs when several logically related operations are stuffed into the same module and only one of the operations is selected
54
Levels of cohesion - logical functions appear to be related because they fall into the same logical class of function (they perform a set of related actions)
55
Levels of cohesion - logical a series of related actions, one is selected by the caller short eval(short opCode, short x[], short y[]) { short eval(short opCode, short x[], short y[]) { short m1,m2; short m1,m2; switch (opCode){ switch (opCode){ case 1 : m1=max(x); return m1; case 1 : m1=max(x); return m1; case 2 : m1=max(x); m2=max(y); return (m1>m2?m1:m2); case 2 : m1=max(x); m2=max(y); return (m1>m2?m1:m2); case 3 : m1=min(x); return m1; case 3 : m1=min(x); return m1; case 4 : m1=min(x); m2=min(y); return (m1<m2?m1:m2); case 4 : m1=min(x); m2=min(y); return (m1<m2?m1:m2); default: return 0; default: return 0; } }
56
Levels of cohesion Temporal cohesion occurs when operations are combined into a module because they are all done at the same time
57
Levels of cohesion - temporal functions are grouped together because of “time” (actions related in time)
58
Levels of cohesion - temporal a series of actions related in time void init() { void init() { openAccountDB(); openAccountDB(); openTransactionDB(); openTransactionDB(); resetTransactionCount(); resetTransactionCount(); System.out.println(“Balance update in progress…“); System.out.println(“Balance update in progress…“); return; return; }
59
Levels of cohesion Procedural cohesion occurs when operations in a module are done in a specified order, the operations in procedural cohesion do not share the same data
60
Levels of cohesion - procedural functions accomplish different tasks, yet have been combined because the actions correspond to the sequence of steps in some operation
61
Levels of cohesion - procedural a series of actions related by sequence of steps to be followed by the result (a series of actions sharing sequence of steps) void sumOfTran() { void sumOfTran() { DB[0]=0; DB[0]=0; CR[0]=0; CR[0]=0; for (short i=1; i<= noAcct; i++){ for (short i=1; i<= noAcct; i++){ DB[0] += DB[i]; DB[0] += DB[i]; CR[0] += CR[i]; CR[0] += CR[i]; } return; return; }
62
Levels of cohesion Communicational cohesion occurs when a sequence of steps, related to some operation, operate on the same data
63
Levels of cohesion - communicational functions perform operations on the same body of data (data is passed between functions)
64
Levels of cohesion - communicational a series of actions related by sequence of steps to be followed by the product and if all the actions are performed on the same data (a procedural cohesion but on the same data) short stackPop2() { short stackPop2() { short next = stack[top--]; short next = stack[top--]; short nextNext = stack[top--]; short nextNext = stack[top--]; return (next+nextNext); return (next+nextNext); }
65
Levels of cohesion Informational cohesion occurs when a module contains independent actions that must be performed on the same data structure (gives the ability to "package" all of the functionality that relates to a given data structure )
66
Levels of cohesion - informational functions are related by the fact that they perform actions on a single data structure
67
Levels of cohesion - informational a number of actions, each with its own entry point and independent code, all performed on the same data short op(char opCode, short x, short y) { short op(char opCode, short x, short y) { switch (opCode){ switch (opCode){ case ’+’ : return (x+y); case ’+’ : return (x+y); case ’-’ : return (x-y); case ’-’ : return (x-y); case ’*’ : return (x*y); case ’*’ : return (x*y); case ’/’ : return (x/y); case ’/’ : return (x/y); default: return 0;} default: return 0;} }
68
Levels of cohesion Functional cohesion strongest and best kind of cohesion – when a module performs one and only one operation
69
Levels of cohesion - functional functions work collectively to accomplish a single well- define action
70
Levels of cohesion - functional performs exactly one action or achieves a single goal short sumOfArray(short number[]) { short sumOfArray(short number[]) { short result = 0; short result = 0; for (short i=0; i< number.len; i++) for (short i=0; i< number.len; i++) result += number[i]; result += number[i]; return result; return result; }
71
Dynamic Modeling: State Diagrams
72
StateState A condition during the life of an object during which it satisfies some conditions, performs some actions or waits for some eventsA condition during the life of an object during which it satisfies some conditions, performs some actions or waits for some events Shown as a rectangle with rounded cornersShown as a rectangle with rounded corners State TransitionState Transition The changes in the attribute of an object or in the links an object has with other objectsThe changes in the attribute of an object or in the links an object has with other objects Shown as a solid arrowShown as a solid arrow Diagrammed with a guard condition and actionDiagrammed with a guard condition and action EventEvent Something that takes place at a certain point in timeSomething that takes place at a certain point in time A.72
73
A.73
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.