couplingandcohesion-student (1).ppt

Upload: paksmiler

Post on 21-Feb-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    1/40

    Coupling and Cohesion

    Pfleeger, S., Software Engineering Theory

    and Practice. Prentice Hall, 2001.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    2/40

    Characteristics of Good Design

    Component independence

    High cohesion

    Low coupling

    Eception identification and handling

    !ault pre"ention and fault tolerance

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    3/40

    Coupling# Degree of dependence

    among components

    $o dependencies Loosel% coupled&some dependencies

    Highl% coupled&man% dependencies

    High coupling ma'es modif%ingparts of the s%stem difficult, e.g.,

    modif%ing a component affects all

    the components to which the

    component is connected.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    4/40

    (ange of Coupling

    High Coupling

    Loose

    Low

    Content

    Common

    Control

    Stamp

    Data

    Uncoupled

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    5/40

    Content coupling

    Definition# )ne component references

    contents of another

    Eample# Component directl% modifies another*s data

    Component refers to local data of another

    component in terms of numerical

    displacement

    Component modifies another*s code, e.g.,

    +umps into the middle of a routine

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    6/40

    Eample of Content Coupling&

    Part of program handles loo'up for customer.

    -hen customer not found, component adds

    customer % directl% modif%ing the contents ofthe data structure containing customer data.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    7/40

    Eample of Content Coupling&/

    Part of program handles loo'up for customer.

    -hen customer not found, component addscustomer % directl% modif%ing the contents of

    the data structure containing customer data.

    0mpro"ement#

    -hen customer not found, component calls the1ddCustomer23 method that is responsile formaintaining customer data.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    8/40

    Common Coupling

    Definition# 4wo components share data

    Gloal data structures

    Common loc's

    5suall% a poor design choice ecause

    Lac' of clear responsiilit% for the data

    (educes readailit%

    Difficult to determine all the components that affecta data element 2reduces maintainailit%3

    Difficult to reuse components

    (educes ailit% to control data accesses

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    9/40

    Eample&

    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.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    10/40

    Eample&/

    Each source process writes directl% to gloal datastore. Each sin' process reads directl% fromgloal data store.

    0mpro"ement Data manager component is responsile for datain data store. Processes send data to andre6uest data from data manager.

    Process control component maintains current data

    about state of operation. Gets data from multiple

    sources. Supplies data to multiple sinks.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    11/40

    Control Coupling

    Definition# Component passes control

    parameters to coupled components.

    7a% e either good or ad, depending on

    situation.

    8ad when component must e aware of

    internal structure and logic of another module

    Good if parameters allow factoring and reuse

    of functionalit%

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    12/40

    Eample

    1cceptale# 7odule p calls module 6 and 6

    passes ac' flag that sa%s it cannot complete

    the tas', then 6 is passing data

    $ot 1cceptale#7odule p calls module 6 and 6

    passes ac' flag that sa%s it cannot complete

    the tas' and, as a result, writes a specific

    message.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    13/40

    Stamp Coupling

    Definition# Component passes a data structure

    to another component that does not ha"e

    access to the entire structure.

    (e6uires second component to 'now how tomanipulate the data structure 2e.g., needs to

    'now aout implementation3

    7a% e necessar% due to efficienc% factors# this

    is a choice made % insightful designer, not la9%

    programmer.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    14/40

    Eample&

    4he print routine of the customer illing

    accepts a customer data structure as anargument, parses it, and prints the name,

    address, and illing information.

    Customer billing system

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    15/40

    Eample&/

    4he print routine of the customer illing accepts a

    customer data structure as an argument, parses it,and prints the name, address, and illinginformation.

    0mpro"ement4he print routine ta'es the customer name,address, and illing information as an argument.

    Customer Billing System

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    16/40

    Data Coupling

    Definition# 4wo components are data coupled

    if there are homogeneous data items.

    E"er% argument is simple argument or data

    structure in which all elements are used

    Good, if it can e achie"ed.

    Eas% to write contracts for this and modif%

    component independentl%.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    17/40

    :e% 0dea in )+ect&)riented

    Programming )+ect&oriented designs tend to ha"e low

    coupling.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    18/40

    Cohesion

    Definition# 4he degree to which all elements of a

    component are directed towards a single tas'

    and all elements directed towards that tas' are

    contained in a single component. 0nternal glue with which component is

    constructed

    1ll elements of component are directed toward

    and essential for performing the same tas'

    High is good

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    19/40

    (ange of Cohesion

    High Cohesion

    Low

    Functional

    Informational

    Sequential

    Communicational

    Procedural

    Temporal

    Logical

    Coincidental

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    20/40

    Coincidental Cohesion

    Definition# Parts of the component are onl%

    related % their location in source code

    Elements needed to achie"e some

    functionalit% are scattered throughout the

    s%stem.

    1ccidental

    -orst form

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    21/40

    Eample

    Print net line

    (e"erse string of characters in second

    argument

    1dd ; to

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    22/40

    Logical Cohesion

    Definition# Elements of component are

    related logicall% and not functionall%.

    Se"eral logicall% related elements are in

    the same component and one of the

    elements is selected % the client

    component.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    23/40

    Eample&

    1 component reads inputs from tape, dis',

    and networ'. 1ll the code for these

    functions are in the same component.

    )perations are related, ut the functions aresignificantl% different.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    24/40

    Eample&/

    1 component reads inputs from tape, dis', andnetwor'. 1ll the code for these functions are inthe same component. )perations are related,ut the functions are significantl% different.

    0mpro"ement

    1 de"ice component has a read operation thatis o"erridden % su&class components. 4hetape su&class reads from tape. 4he dis' su&

    class reads from dis'. 4he networ' su&classreads from the networ'.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    25/40

    4emporal Cohesion

    Definition# Elements of a component are

    related % timing.

    Difficult to change ecause %ou ma% ha"e

    to loo' at numerous components when a

    change in a data structure is made.

    0ncreases chances of regression fault

    Component unli'el% to e reusale.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    26/40

    Eample&

    1 s%stem initiali9ation routine# this routine

    contains all of the code for initiali9ing all of the

    parts of the s%stem. Lots of different acti"ities

    occur, all at init time.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    27/40

    Eample&/

    1 s%stem initiali9ation routine# this routinecontains all of the code for initiali9ing all ofthe parts of the s%stem. Lots of different

    acti"ities occur, all at init time.

    0mpro"ement

    1 s%stem initiali9ation routine sends aninitiali9ation message to each component.

    Each component initiali9es itself atcomponent instantiation time.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    28/40

    Procedural Cohesion

    Definition# Elements of a component are

    related onl% to ensure a particular order of

    eecution.

    1ctions are still wea'l% connected and

    unli'el% to e reusale

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    29/40

    Eample

    ...

    (ead part numer from

    data ase

    update repair record onmaintenance file.

    ...

    7a% e useful toastract the intent ofthis se6uence. 7a'ethe data ase andrepair recordcomponents handlereading and updating.7a'e component that

    handles moreastract operation.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    30/40

    Communicational Cohesion

    Definition# 7odule performs a series of

    actions related % a se6uence of steps to

    e followed % the product and all actions

    are performed on the same data

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    31/40

    Eample

    5pdate record in data

    ase and send it to

    the printer.

    dataase.5pdate

    2record3.

    record.Print23.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    32/40

    Se6uential Cohesion

    4he output of one component is the input

    to another.

    )ccurs naturall% in functional programming

    languages

    Good situation

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    33/40

    0nformational Cohesion

    Definition# 7odule performs a numer of

    actions, each with its own entr% point, with

    independent code for each action, all

    performed on the same data. Different from logical cohesion

    Each piece of code has single entr% and single eit

    0n logical cohesion, actions of module intertwined 1D4 and o+ect&oriented paradigm promote

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    34/40

    !unctional Cohesion

    Definition# E"er% essential element to a

    single computation is contained in the

    component.

    E"er% element in the component is

    essential to the computation.

    0deal situation.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    35/40

    Eamples of Cohesion&

    !unction 1

    !unction

    8!unction

    D

    !unction

    C!unction

    E

    Coincidental

    Parts unrelated

    !unction 1

    !unction 1*

    !unction 1**

    logic

    Logical

    Similar functions

    4ime t>

    4ime t> ? @

    4ime t> ? /@

    4emporal

    (elated % time

    !unction 1

    !unction 8

    !unction C

    Procedural

    (elated % order of functions

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    36/40

    Eamples of Cohesion&/

    !unction 1 part

    !unction 1 part /

    !unction 1 part A

    !unctional

    Se6uential with complete, related functions

    !unction 1

    !unction 8

    !unction C

    Communicational

    1ccess same data

    !unction 1

    !unction 8

    !unction C

    Se6uential

    )utput of one is input to another

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    37/40

    p

    q

    t

    r s

    u

    1

    ! "

    # $

    1ircraft t%pe Status flag

    / &&&&& List of aircraft

    partsA !unction code &&&&&

    = &&&&& List of aircraftparts

    < Part numer Part manufacturer

    B Part numer Part name

    No. In Out

    Interface Description

    p, t, u access the same

    database in update

    mode

    Problem: Define coupling between pairs of modules.

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    38/40

    6 r s t u

    p

    6 &&& &&&

    r &&& &&&

    s &&&

    t

    Coupling between pairs of modules

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    39/40

    Prolem# Classif% cohesion for

    each module Compute a"erage dail% temperatures at "arious

    sites

    0nitiali9e sums and open files

    Create new temperature record Store temperature record

    Close files and print a"erage temperatures

    (ead in site, time, and temperature

    Store record for specific site

    Edit site, time, or temperature field

  • 7/24/2019 CouplingandCohesion-student (1).ppt

    40/40

    0n Class

    P# -hat is the effect of cohesion on

    maintenance

    P/# -hat is the effect of coupling on

    maintenance

    PA# Produce an eample of each t%pe of

    cohesion. ustif% %our answers.

    P=# Produce an eample of each t%pe of

    coupling. ustif% %our answers.