Topics Designing the program Class cohesion and coupling Cohesion

Document Sample
Topics Designing the program Class cohesion and coupling Cohesion Powered By Docstoc
					(c) Addison Wesley                                                                                                                                    Chapter 9




                                                                                             Class cohesion and coupling
                    MACIASZEK, L.A. (2001): Requirements Analysis and
                    System Design. Developing Information Systems with                       !   Class cohesion
                    UML, Addison Wesley                                                           • Degree of inner self-determination of the class
                                                                                                  • Measure of the strength of the class
                                                                                                    independence
                                                                                                  • One action, a single goal
                                                                                                  • The stronger the better
                                          Chapter 9
                                                                                             !   Class coupling
                                Program and Transaction Design                                    • Degree of connections between classes
                                                                                                  • Measures the class interdependence
                                                                                                  • The weaker the coupling – the better
                                                                                             !   Better cohesion induces worse coupling and vice
                                                                                                 versa
                                   Copyright © 2000 by Addison Wesley
                                                 Version 1.0
                                                                                      (c) Addison Wesley              Chapter 9                       4




                  Topics                                                                     Cohesion and coupling heuristics
                                                                                             !   Two classes to either be not dependent on
                                                                                                 one another or one class to be only
                  !   Designing the Program                                                      dependent on the public interface of another
                                                                                                 class
                  !   Program Navigation                                                     !   Attributes and the related methods to be
                                                                                                 kept in one class
                  !   Designing the Transaction                                              !   A class to capture one and only one
                                                                                                 abstraction - unrelated information to be
                  !   Round-Trip Engineering                                                     kept in separate classes
                                                                                             !   The system intelligence to be distributed as
                                                                                                 uniformly as possible

           (c) Addison Wesley               Chapter 9                             2   (c) Addison Wesley              Chapter 9                       5




                  Designing the program                                                      Kinds of class coupling
                  !   Design                                                                 !   X inherits from Y
                                                                                             !   X has an attribute of class Y
                       • Architectural design
                                                                                             !   X has a template attribute with a parameter of class
                       • Detailed design                                                         Y
                       • Program design                                                      !   X has a method with an input argument of class Y
                            – One application program at a time                              !   X has a method with an output argument of class Y
                            – Extends the GUI design                                         !   X knows of a global variable of class Y
                            – Uses a database subschema                                      !   X knows of a method containing a local variable of
                            – Defines database procedural parts - stored procedures              class Y
                            – Spans the client and the server processes                      !   X is a friend of Y

           (c) Addison Wesley               Chapter 9                             3   (c) Addison Wesley              Chapter 9                       6




MACIASZEK (2001): Req Analysis & Syst Design                                                                                                                 1
(c) Addison Wesley                                                                                                                                                                            Chapter 9




                  Coupling and BCDE approach                                                                    Accessor methods - example
                  !   BCDE specifies four layers of classes                                                     !   Add a student to a course offering.
                  !   Objects communicate within a layer and                                                    !   Check
                      between the adjacent layers                                                                    • what are the prerequisite courses for the course
                  !   Intra-layer coupling                                                                             offering
                       • desirable                                                                                   • if the student satisfies the prerequisites
                       • localizes software maintenance and evolution to                                        !   Consider that
                         individual layers                                                                           • the enrol() message is sent by the boundary
                  !   Inter-layer coupling                                                                             object :EnrolmentWindow
                       • to be minimized                                                                             • Three entity classes are involved –
                                                                                                                       CourseOffering, Course, and Student
                       • communication interfaces to be carefully defined
                                                                                                                !   At least four solutions possible
                  !   The Law of Demeter to be obeyed

           (c) Addison Wesley                    Chapter 9                                           7   (c) Addison Wesley                  Chapter 9                                     10




                  The Law of Demeter                                                                            Accessor methods - solution 1
                  !   Message target can only be one of the following
                      objects
                       1 The method’s object itself (i.e. this in C++ and Java, self
                         and super in Smalltalk)                                                                     : Enrolment                         aCourseOffering :
                       2 An object that is an argument in the method’s signature                                       Window                             CourseOffering

                       3 An object referred to by the object’s attribute (including an                                         enrol()
                         object referred to within a collection of attributes)
                                                                                                                                                  [prereq OK] enrol()
                       4 An object created by the method
                                                                                                                                                           getAcadRec()
                       5 An object referred to by a global variable
                                                                                                                                         aCourse :                               aStudent :
                                                                                                                                          Course                                  Student
                  !   The Strong Law of Demeter
                                                                                                                 Policy maker
                       • Rule 3 limited to attributes defined in the class - inherited
                                                                                                                                                                      Mindless
                         attributes not to be used to identify the target object

           (c) Addison Wesley                    Chapter 9                                           8   (c) Addison Wesley                  Chapter 9                                     11




                  Accessor methods and mindless classes                                                         Accessor methods - solution 2
                  !   Accessor methods
                       • observers (get)
                       • mutators (set)
                                                                                                                              aCourseOffering :                              : Enrolment
                  !   Object state can only be accessed through                                                                CourseOffering                                  Window
                      accessor methods in its interface
                  !   Mindless class                                                                                                                                      enrol()
                       • puts too many accessor methods in its interface                                                           [acad_rec OK] enrol()

                       • allows other objects to freely view and modify its                                       aCourse :                              aStudent :
                         information content                                                                       Course                                 Student
                       • sometimes difficult to avoid                                                                               getPrereq()
                            – “On an object-oriented farm there is an object-oriented milk. Should
                              the object-oriented cow send the object-oriented milk the                                                                                   Policy maker
                              uncow_yourself message, or should the object-oriented milk send                       Mindless
                              the object-oriented cow the unmilk_yourself message?”

           (c) Addison Wesley                    Chapter 9                                           9   (c) Addison Wesley                  Chapter 9                                     12




MACIASZEK (2001): Req Analysis & Syst Design                                                                                                                                                         2
(c) Addison Wesley                                                                                                                                                                              Chapter 9




                  Accessor methods - solution 3                                                             Mixed-instance cohesion
                                                                    Policy maker                                                                St udent
                                                                    (the “God” class)
                                                                                                                                   / current_sem_credit_points : Integer
                                         enrol()
                      : Enrolment                          aCourseOffering :
                        Window                              CourseOffering

                                                   getPrereq()            getAcadRec()                                PartTimeStudent
                                                                                                                                                                            FullTimeS tudent
                                                                                                        evening_preference : Boolean

                                            aCours e :                            aStudent :            payExtraFee(crs_off)
                                             Course                                Student
                                                                                                        !       This design has no mixed-instance cohesion provided that
                                                                                                                 • extra fees are paid even if a part time student elects to take
                                                                                                                   datetime course offerings


           (c) Addison Wesley                Chapter 9                                        13   (c) Addison Wesley                            Chapter 9                                     16




                  Accessor methods - solution 4                                                             Mixed-instance cohesion
                                                                                                                !    This design eliminates mixed-instance cohesion even if
                                                         aCours eOffering :                                            • extra fees are not paid by a part time student who elects to take
                                                          CourseOffering                                                 datetime course offerings
                       : Enrolment
                                                                                                                                                            Student
                         Window          enrol()                                                                                              / current_sem_credit_points : Integer
                                                                              The control object
                                                    [OK] enrol()

                                                            : Enrolment
                                                               Policy                                                                      PartTimeStudent                 FullTimeStudent

                                                   getPrereq()          getAcadRec()


                                     aCourse :                                   aStudent :
                                      Course                                      St udent                          EveningPrefPartTimeStudent          DayPrefPartTimeStudent

                                                                                                                    payExtraFee(crs_off)

           (c) Addison Wesley                Chapter 9                                        14   (c) Addison Wesley                            Chapter 9                                     17




                  Mixed-instance cohesion                                                                   Mixed-instance cohesion
                  !   The consequence of the lack of dynamic                                                !    But what if a DayPrefPartTimeStudent is forced
                                                                                                                 to take an evening course offering because there
                      classification                                                                             are no more places available in daytime course
                  !   A class with mixed-instance cohesion                                                       offerings?
                                                                                                            !    Perhaps, some other fee would then apply.
                       • “has some features that are undefined for some                                          Should we specialize further to derive a class
                         objects of the class”                                                                   UnluckyDayPrefPartTimeStudent and avoid
                       • features (methods and attributes) do not apply to                                       the mixed-instance cohesion again?
                         all object of the class                                                            !    We may have to use the IF statement in the code:
                                                                                                       method payExtraFee(crs_off) for the class PartTimeStudent
                       • e.g. not all objects of the Employee class get                                       if evening_preference = “False”
                         allowance; only Manager objects do (i.e.                                                 return
                                                                                                              else
                          employees who are managers).                                                            do it
                                                                                                       end method

           (c) Addison Wesley                Chapter 9                                        15   (c) Addison Wesley                            Chapter 9                                     18




MACIASZEK (2001): Req Analysis & Syst Design                                                                                                                                                           3
(c) Addison Wesley                                                                                                                                                                                                                Chapter 9




                  Designing Client/Server collaboration                                                                Program Navigation Diagram
                           Level 1                                                        SQL, data                                                                                     <<dialog box>>
                        designer/DBA                                                 definition language                                                                                Update Product
                                                                                                                                                                                         <<command button>>
                                                                                                                                                                                               Cancel
                                                                                       SQL, data
                           Level 2
                                                                                  manipulation language                                                                  <<command button>>
                       ad-hoc user/DBA                                                                                                                                          OK
                                                                                                                                      <<database>>
                                                                                                                                     Inventory Control
                                                      • native SQL                                                                                                                      <<command button>>
                              Level 3                 • client DB library             SQL, embedded                                                                                           Save

                            programmer                • ODBC/JDBC                       language                                     <<stored procedure>>
                                                                                                                                        Update Product                                    <<command button>>
                                                                                                                                                                                                Clear
                                                      • native SQL
                            Level 4                   • client DB library          4GL/SQL, application                                 <<table>>
                      designer/programmer             • ODBC/JDBC                       generator                                        Product


                                                                                                                                      <<trigger>>
                                                                                                                                      On Update
                            Level 5                                                    PL/SQL, stored                                   Product
                      designer/programmer                                                procedures

           (c) Addison Wesley                       Chapter 9                                              19   (c) Addison Wesley                                Chapter 9                                                      22




                                                                                                                       Program Navigation Diagram
                  Stored procedures                                                                                                                                                        <<primary window>>
                                                                                                                                                                                           Contact Management


                                                                                                                                                                                       <<toolbar button /menu item>>
                            SQL query                           Stored procedure call                                                                                                           Delete Event
                                                                                                                                             <<database>>
                   (from the client application)             (from the client application)                                                       CMS
                                                                                                                                                                                     <<toolbar button /pop-up menu>>
                                                                                                                                          <<stored procedure>>                               Complete Event
                                                                                                                                              Delete Event
                                Parse                                                                                                                                           <<toolbar button / menu item / double c lick>>
                                                                Locate procedure                                                               <<stored procedure>>                             Update Event
                                                                (perhaps in procedure cache)                                                     Complete Event
                        Validate syntax                                                                                                                                        <<toolbar button /menu item>>
                        and object references                                                                                             <<stored procedure>>                          Insert Event
                                                                                                                                               Save Event
                                                                  Check authorization
                        Check authorization Server                                                                                            <<table>>
                                                                                                                                                Event                                         <<dialog box>>
                                           Database                                                                                                                                          Task/Event Details
                                                                                                                                      <<trigger>>
                          Optimize                                                                                                                                                   <<command button>>
                                                                                                                                       On Insert
                                                                Substitute parameters          Trigger =                                 Event
                                                                                                                                                                                          Complete


                            Compile                                                            special kind                                         <<trigger>>
                                                                                                                                                    On Update                         <<command button>>
                                                                                               of stored                                               Event                                OK
                                                                                               procedure
                                                   Execute


           (c) Addison Wesley                       Chapter 9                                              20   (c) Addison Wesley                                Chapter 9                                                      23




                  Stereotyping Activity Diagrams for program
                  navigation                                                                                           Designing the transaction
                  !   States (rounded rectangles) stereotyped for                                                      !   Transaction
                      data objects:                                                                                         • logical unit of work
                        • tables
                                                                                                                            • unit of database consistency
                        • records
                        • etc                                                                                               • atomic - all results committed to DB or
                                                                                                                              rollbacked to original DB state
                  !   Activities (ovals) stereotyped for program
                      objects:                                                                                         !   Transaction Manager
                        •   stored procedures                                                                               • database recovery
                        •   triggers                                                                                        • concurrency control
                        •   SQL queries
                        •   etc.
                                                                                                                       !   Short transaction - conventional DB
                                                                                                                           business applications
           (c) Addison Wesley                       Chapter 9                                              21   (c) Addison Wesley                                Chapter 9                                                      24




MACIASZEK (2001): Req Analysis & Syst Design                                                                                                                                                                                             4
(c) Addison Wesley                                                                                                                                                            Chapter 9




                  Pessimistic concurrency control                                                                 Programmable recovery
                  !   Locks on data objects:                                                                      !   Undo from user mistake
                       • Exclusive (write) lock – other transactions must                                              • compensating transaction may be necessary
                         wait                                                                                     !   Savepoint
                       • Update (write intent) lock – other transactions
                                                                                                                       • divides a transaction into smaller parts
                         can read
                                                                                                                       • allows rollback to a named savepoint
                       • Read (shared) lock – other transactions can
                         read and possibly obtain an update lock on the                                           !   Trigger rollback
                         object                                                                                        • special kind of savepoint
                       • No lock – other transactions can update an                                                    • only the trigger is rollbacked and the transaction
                         object                                                                                          can take a remedial action


           (c) Addison Wesley                       Chapter 9                                       25     (c) Addison Wesley               Chapter 9                     28




                  Levels of transaction isolation                                                                 Long transaction
                  !   Between concurrently executing transactions:                                                !   In workgroup computing applications
                       • Dirty read possible – transaction t1 modified an object                                  !   Facilitate user cooperation whereas the
                         but it has not committed yet; transaction t2 reads the                                       purpose of short transactions is to isolate
                         object
                                                                                                                      the users
                       • Nonrepeatable read possible – t1 has read an object; t2
                         updates the object; t1 reads the same object again                                       !   Can span computer sessions
                       • Phantom possible – t1 has read a set of objects; t2                                      !   Require
                         inserts a new object to the set; t1 repeats the read                                          • version management
                         operation
                                                                                                                       • collaborative concurrency control
                       • Repeatable read – t1 and t2 can still execute concurrently
                                                                                                                       • no automatic rollbacks
                         but the interleaved execution of these two transactions will
                         produce the same results as if the transactions executed                                 !   Short transactions still required during
                         one at a time (serializable execution)                                                       check-in/check-out operations
           (c) Addison Wesley                       Chapter 9                                       26     (c) Addison Wesley               Chapter 9                     29




                  Automatic recovery                                                                              Round-trip engineering
                  !   Rollback
                  !   Rollforward
                                                                                                                  !   The combination of
                  !   Checkpoint - forcing changes to DB                                                               • forward engineering
                  !   Backup recovery
                                                                                                                            – generation of code from the design
                                                                                 Recovery after failure:
                      t1         commit
                                                                                 t1 - rollforward (redo)
                                                                                                                       • reverse engineering
                           t2        rollback
                                                                                                                            – recovery of design from the code
                                                                                 t2 - rollback
                                          t3            commit
                                                                                 t3 - rollforward                 !   Required for
                                               t4               rollback
                                                                                 t4 - rollback                         • client application programs
                                                            t5
                                                                                 t5 - no action                        • server database programs
                           checkpoint                                      failure
           (c) Addison Wesley                       Chapter 9                                       27     (c) Addison Wesley               Chapter 9                     30




MACIASZEK (2001): Req Analysis & Syst Design                                                                                                                                         5
(c) Addison Wesley                                                                                                                                                      Chapter 9




                  Round-trip engineering with Client                                                            Summary
                                                                                                                !   The coupling and cohesion principles can be
                                                                                                                    achieved through the Law of Demeter
                                                                                                                !   Excessive use of accessor methods can lead to
                        UML Design                       Code                         Source                        mindless classes
                          Model                        Generation                      Code
                                                                                                                !   Mixed-instance cohesion sometimes necessary
                                                                                                                !   Five levels of SQL interfaces
                                                                                                                !   Program Navigation Diagrams extend Window
                           Model                                                    Programming
                         Differencing
                                                                                                                    Navigation Diagrams
                                                                                                                !   Conventional DB applications require short
                                                                                                                    transactions
                                                                                                                !   Some new DB applications work in long
                     UML Implemented                                                  Modified
                          Model
                                                          UML
                                                                                       Code                         transactions
                                                        Generation
                                                                                                                !   Round-trip engineering applied to client programs
                                                                                                                    and to database programs
           (c) Addison Wesley                   Chapter 9                                           31   (c) Addison Wesley          Chapter 9                      34




                  Round-trip engineering with DB
                          Archive                                                        Load




                         Initial PDM                     Create DB                     Initial DB




                                           Modify PDM                 Modify DB
                           Archive                                                      Reload



                                                       Synchronize
                           Current                     PDM with DB                    Current DB
                            PDM
                                                       Synchronize
                                                       DB with PDM

           (c) Addison Wesley                   Chapter 9                                           32




                  Reengineering from RDB to ORDB

                                                            Modify Model for ORDB




               Exist ing RDB            Analyze RDB             UML Design
              Implementation                                      Model


                                                            Test and Modify ORDB    Synchronize UML
                                        Generate and
                                         Load ORDB                                  Model with ORDB



                                                                   ORDB
                                                               Implementation




           (c) Addison Wesley                   Chapter 9                                           33




MACIASZEK (2001): Req Analysis & Syst Design                                                                                                                                   6

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:6
posted:4/16/2010
language:English
pages:6