Object -Oriented Analysis and Design

Document Sample
Object -Oriented Analysis and Design Powered By Docstoc
					                Object-Oriented
               Analysis and Design




1




                         Overview
§ In this module we will cover the basics of one of the
  most popular software analysis and design techniques –
  Object-Oriented analysis and design.
§ The methods are based around the notion of simulating
  the problem in terms of objects.
    § Objects correspond to things in the problem.
    § The behavior and dynamics inherent in the problem are modeled
      by interactions among the objects.
    § The notation used to convey the structure and dynamics of the
      object-oriented solution is UML – Unified Modeling Language.
§ We will cover some of the modeling notations in the
  UML.
2




                                                                      1
                     Objectives
§ Here is what you should be able to do upon
  completion of this module:
    § Define the following basic object-oriented principles:
      Object, Message, Class, Inheritance and
      Polymorphism.
    § Apply the object-oriented analysis process.
    § Construct the following UML models: Class Diagram,
      Activity Diagram, Sequence Diagram, State Transition
      Diagram.
    § Describe the difference between object-oriented
      analysis and object-oriented design.
3




                        Outline
§ Object-Oriented Principles.
§ Object-Oriented Analysis.
§ Object-Oriented Design.




4




                                                               2
       Object-Oriented Principles
§   Objects.
§   Messages.
§   Classes.
§   Inheritance.
§   Polymorphism




5




              Objects (1 of 2)
§ An object is a thing.
§ In analysis, an object is something in the
  problem to be solved.
§ In code, an object is a collection of data that
  describes something.
§ The “thing” can be something tangible (such as
  a box) or something conceptual (such as an
  event).



6




                                                    3
                    Objects (2 of 2)
§ Objects have two main properties:
    § Data (synonyms: state, attributes, fields, member
      data, slots).
    § Behavior (synonyms: operations, methods, member
      functions, procedures, services).
§ The only way to access the data is through the
  operations (encapsulation).




7




                          Messages
§ A message is a request for service.
§ An object will not perform any of its operations unless it receives a
  message.
§ An object may perform the operation entirely by itself, using only its
  own data, or it may invoke the operations of another object by
  sending it a message in turn.
§ A message has a name, parameters, and a result.
§ The name, parameter types and result type are called the signature
  of the operation.
§ The set of signatures of all operations on an object is called its
  protocol.




8




                                                                           4
                   Classes (1 of 3)
§ Classification uses the power of abstraction to treat a group of
  objects in the same way.
§ A class is a template for object instances.
§ All of the objects described by a class behave in the same way.
§ They each encapsulate the same kinds of data.
§ There may be any number of object instances described by a class.
§ A class has the ability to create object instances.
§ The information stored in a class is descriptive information used
  when the class creates object instances.




9




                   Classes (2 of 3)
§ When you program in an object-oriented
  language, you create classes.
§ The object instances don’t get created until run-
  time.
§ What we describe in object-oriented analysis
  and object-oriented design are classes, not
  object instances. That is, we are interested in
  the kinds of objects.



10




                                                                      5
                     Classes (3 of 3)
§ A class is an instance of a special type of object.
     § Its operations include constructors, which create object
       instances.
     § Its attributes include descriptive information needed to create
       the object instances.
§ Classes are loaded by the run-time system. Memory for
  classes is statically allocated.
§ Object instances are then created by sending constructor
  messages to the classes. Memory for object instances is
  dynamically allocated.




11




                Inheritance (1 of 3)
§ As in any classification scheme, there are often
  similarities among classes.
§ These similarities may be represented in the
  form of generalization-specialization
  relationships among classes.
§ The generalization class is sometimes referred to
  as the superclass, and the specialization the
  subclass.



12




                                                                         6
            Inheritance (2 of 3)
§ In the Unified Modeling Language (UML), generalization-
  specialization relationships are indicated by an arrow
  from the subclass to its superclass.


                                 VideoCopy




                      Cassette               DVD




13




            Inheritance (3 of 3)
§ The superclass defines a protocol that all of the
  subclasses inherit.
§ The subclasses may add new attributes or operations.
§ The subclasses may override operations (keeping the
  same signature, but providing different
  implementations).
§ Some of the operations in a superclass may be abstract
  – only the signature is specified, but no implementation
  – that’s in the subclasses.




14




                                                             7
          Polymorphism (1 of 2)
§ In an object-oriented program, variables may contain
  references to object instances.
§ The type of the variable is specified by a class.
§ If the type of a variable is a superclass, that variable
  may contain a reference to an object instance of any
  class in the subtree of the inheritance hierarchy
  stemming from the given class.
§ However, the protocol for any of the objects is given by
  the superclass.




15




          Polymorphism (2 of 2)

                                  Vehicle




                             turnRight()




                Automobile        Airplane         Yacht




             turnRight()     turnRight()     turnRight()




16




                                                             8
                Goals of OO Analysis
§ Find the classes of interest in the problem domain.
§ Model the static relationships among the objects
  (Analysis Class Diagram).
§ Determine the responsibilities (attributes and operations)
  of the objects.
§ Model the dynamic behavior of the system.
§ Model the state related behavior of the objects.




17




            The OO Analysis Process
§ Find candidate classes.
§ Find responsibilities (attributes and operations) of the objects
  through the use of scenarios and CRC analysis.
§ Show object (static) relationships in the Analysis Class Diagram.
§ Show the dynamics of the objects with Analysis Dynamic Models:
     § Activity Diagrams.
     § Interaction Diagrams.
     § State Models.




18




                                                                      9
         Finding Candidate Classes
§ Remember that, in analysis, we are modeling
  the requirements from the inside.
§ Requirements is the black-box view, while
  analysis is a clear-box view.
§ (Design models the code.)
§ Where do we find candidate classes?




19




     Candidate Classes-What to Look For

§ Properties of Objects:
     § Encapsulate information.
     § Provide operations.
     § Identity.
§ Objects in the problem space hold information
  and operations pertinent to the problem domain.




20




                                                    10
                    Some Hueristics
§ Objects act like little people (anthropomorphism).
     § They respond to messages.
     § They send messages to other objects.
     § They change their own state only in response to events.
§ Objects are persistence mechanisms. They control the
  lifetime of the information they encapsulate.
§ Remember, we are modeling the problem in terms of
  objects.




21




                The Knotty Problem
§ We want to create an application that will
  determine the number of nodes in a tree
  structure, to be provided as input to the
  application.
§ We may destroy the tree in doing this.
§ All data types must be implemented as classes.




22




                                                                 11
             Node Counting Algorithm

     § Maintain a node counter, initially zero.
     § Get the original tree, and put it onto a pile of trees, which is
       initially empty.
     § As long as the pile is not empty, do the following:
         § Get a tree from the pile (at random).
         § If the tree has subtrees then
             § Split it into two trees.
             § Put each resulting tree onto the pile.
         § Else
             § Discard the tree (node).
             § Increment the node counter by 1.
     § When the pile is empty, display the current value of the counter
       on the standard output device.


23




                  Discover The Classes
§ Diagram the candidate classes using UML
  notation.
§ Three part box.
       § Top: Name of the class, in bold, centered.
       § Middle: Names (and declarations of) attributes, in
         normal font, left justified.
       § Bottom: Names (and signatures) of operations in
         normal font, left justified.




24




                                                                          12
             Node Counting Algorithm

     § Maintain a node counter, initially zero.
     § Get the original tree, and put it onto a pile of trees, which is
       initially empty.
     § As long as the pile is not empty, do the following:
         § Get a tree from the pile (at random).
         § If the tree has subtrees then
             § Split it into two trees.
             § Put each resulting tree onto the pile.
         § Else
             § Discard the tree (node).
             § Increment the node counter by 1.
     § When the pile is empty, display the current value of the counter
       on the standard output device.


25




                  Analysis Class Diagram
§ Shows the classes
  And their static                                       Video                                             VideoCopy
                                                                                 describes

  Relationships.
                                             title : String                                         timesRented : int
                                                                        1                       *
                                             length : float



       § Associations
                                                                                                    rent()
                                             reserve()                                              isOverdue() : boolean
                                                                                                    isAvailable() : boolean

       § Multiplicity                                    *                                                    *
                                                                                                                  rented by




                                                                                   due : Date
                                                                 res
                                                                    erv
                                                                       ed                                   0..1
                                                                            by
                                                                                                           Customer

                                                                             {ordered}              name : String
                                                                                                *
                                                                                                    id : int


                                                                                                    isInGoodStanding()
                                                                                                    rent(vc:VideoCopy)




26




                                                                                                                              13
         Analyze the Dynamics
§ Run through scenarios.
§ At requirements time, the scenarios were in
  terms of the black box.
§ At analysis time, we are exploring how the
  objects (inside the box) collaborate.
§ We need to do this in order to determine the
  properties (attributes and operations) of the
  objects.



27




             Dynamic Models
§ Activity Diagram.
§ Sequence Diagram.
§ State Transition Diagram.




28




                                                  14
                          Activity Diagram
            Pile                                       Tree                                      Counter



                    Initialize                             Get Initial                               Initialize


      Put




                   Put                         Split                       Discard
           [Not
                                 [Has subtrees]
          Empty]
                   Get                                                                     Increment
                                                       [Doesn’t have subtrees]
[Empty]
                                                                                                     Display

 29




                         Sequence Diagram

                                       c1 : Customer           vc1:VideoCopy         vc2:VideoCopy




                           rent(vc2)


                                              isInGoodStanding()

                                                   isOverdue()

                                                       false


                                                true

                                                                isAvailable()

                                                                    true

                                                                  rent(c1)




 30




                                                                                                                  15
           State Transition Diagram


                                             Video Copy
                            checkOut
             In Store                        Checked Out          checkIn        For Sale
                                                           [timesRented > 200]


                              checkIn




                                                                                     sold
                        [timesRented<=200]




31




     Object-Oriented Design (1 of 2)
§ The design models the code.
§ There is much more detail about how relationships are
  maintained.
     § Which objects contain stored pointers (references) to which
       other objects?
§ There is also much more detail in the dynamic models.
     § When one object sends a message to another, it must have a
       reference to the other object.
§ Additional classes may be introduced (e.g., collections).




32




                                                                                            16
     Object-Oriented Design (2 of 2)
§ The same UML notation is used to record the static and
  dynamic models at the design level of abstraction.
§ The class diagram includes directional associations to
  indicate where the references are.
§ The sequence diagram includes the detail of obtaining
  references to objects you need to send messages to.




33




        Additional Concerns in OOD
§    Performance optimization.
§    Mapping to the technical architecture.
§    Persistence.
§    Security.
§    Legacy system interface.
§    Inter-process communication.




34




                                                           17