Class DIAGRAMS by bilal008

VIEWS: 151 PAGES: 28

									Software Design and
    Architecture

  UML Class Diagrams
                                                                               officially in UML, the top format is
                                           SuperclassFoo                       used to distinguish the package
                                                 or                            name from the class name
                                      SuperClassFoo { abstract }
                                                                               unofficially, the second alternative
                        - classOrStaticAttribute : Int                         is common
                        + publicAttribute : String
3 common
                        - privateAttribute
compartments
                        assumedPrivateAttribute
                        isInitializedAttribute : Bool = true                                     java.awt::Font
1. classifier name
                        aCollection : VeggieBurger [ * ]                                                or
                        attributeMayLegallyBeNull : String [0..1]                                 java.awt.Font
2. attributes
                        finalConstantAttribute : Int = 5 { readOnly }
                        /derivedAttribute                                               plain : Int = 0 { readOnly }
3. operations
                                                                                        bold : Int = 1 { readOnly }
                        + classOrStaticMethod()                                         name : String
                        + publicMethod()                                                style : Int = 0
                        assumedPublicMethod()                                           ...
 an interface           - privateMethod()
 shown with a           # protectedMethod()                                             getFont(name : String) : Font
 keyword                ~ packageVisibleMethod()                                        getName() : String
                        «constructor» SuperclassFoo( Long )                             ...
                        methodWithParms(parm1 : String, parm2 : Float)
                        methodReturnsSomething() : VeggieBurger
  «interface»           methodThrowsException() {exception IOException}
   Runnable             abstractMethod()
                        abstractMethod2() { abstract } // alternate                                            Fruit
run()                   finalMethod() { leaf } // no override in subclass     dependency
                        synchronizedMethod() { guarded }                                             ...

                                                                                                     ...

interface
implementation
and
subclassing                                   SubclassFoo
                                                                                                           PurchaseOrder
                         ...                                                                    1
                                                                                                     ...
                         run()                                                               order
                                                                                                     ...
                         ...
                                                                             association with
                                                                             multiplicities
                 - ellipsis “…” means there may be elements, but not shown
                 - a blank compartment officially means “unknown” but as a
                 convention will be used to mean “no members”
     UML Class Diagrams in Two Perspectives

  Domain model is the analysis class diagram, don‟t show methods
Domain Model                                                                   Sale
                               Register              Captures     1
                                                1
                                                                       time
conceptual
                     ...                                               isComplete : Boolean
perspective
                                                                       /total



                                Register                                       Sale
Design Model          ...                                              time
                                                                 1
                                                                       isComplete : Boolean
DCD; software         endSale()                          currentSale   /total
perspective           enterItem(...)
                      makePayment(...)                                 makeLineItem(...)

                            Avoid showing no-argument constructors & getters/setters


    • Design model shows methods and visibility (arrowhead on association)
    • Register has reference to Sale; Sale does not have reference to Register
          Definition: Classifier
• A UML classifier is "a model element that
  describes behavioral and structure features“.
  – classes, interfaces, use cases, and actors.
  – In class diagrams, the two most common
    classifiers are regular classes and interfaces.
• Attributes of a classifier (also called structural
  properties in the UML) are shown several
  ways:
  – attribute text notation, such as currentSale : Sale.
  – association line notation
  – both together
   Attribute Text Vs Association-Line Notation
               for a UML Attribute
                                 Register                                              Sale
using the attribute
text notation to                                                                 ...
                         currentSale : Sale
indicate Register has
a reference to one       ...                                                     ...
Sale instance

       Avoid                                   Role-name is attribute name

                                 Register                                              Sale
 OBSERVE: this style
 visually emphasizes                                                         1
                         ...                                                     ...
 the connection
                                                                 currentSale
 between these classes   ...                                                     ...

       Preferred               using the association notation to indicate
                               Register has a reference to one Sale instance



                                 Register                                              Sale
 thorough and
 unambiguous, but some                                                       1
                         currentSale : Sale                                      ...
 people dislike the
                                                                 currentSale
 possible redundancy     ...                                                     ...

        Avoid
                       Class Diagrams
• UML notation for attributes
   – visibility attribName : type multiplicity = default {property-string}
   – You don‟t have to use entire notation
   – Visibility
       • + (public)
       • - (private)
• attribute-as-association line has the following style:
   – a navigability arrow pointing from the source (Register) to
     target (Sale) object, indicating a Register object has an
     attribute of one Sale
   – a multiplicity at the target end, but not the source end
   – a rolename (currentSale) only at the target end to show the
     attribute name
   – no association name
       Idioms in Association Notation
       Usage in Different Perspectives
                                               the association name, common when drawing a
                                               domain model, is often excluded (though still legal)
                                               when using class diagrams for a software
                                               perspective in a DCD


                                    Register                                               Sale
  UP Domain Model                                  1 Captures-current-sale     1
conceptual perspective   id : Int                                                  time : DateTime




  UP Design Model                   Register                                               Sale
         DCD                                                                  1
                         id: Int                                                   time: DateTime
 software perspective                                               currentSale
                         ...                                                       ...
  Guideline: When to Use Attribute Text versus
       Association Lines for Attributes?

                                            Register                              Sale
applying the guideline
to show attributes as                                               1
                                  id: Int                                 time: DateTime
attribute text versus as                                    currentSale
association lines                 ...                                     ...



                           Register has THREE attributes:                        Store
                           1. id                                     1
                           2. currentSale                                 address: Address
                           3. location                                    phone: PhoneNumber
                                                               location
                                                                          ...



       Showing Sale & Store attributes as associations visually emphasizes
       relationships important in object collaboration
       Use the attribute text notation for data type objects and the
       association line notation for others
     Two ways to show a collection attribute in
                                          the UML
                                           Sale                                       SalesLineItem

                       time: DateTime                                           ...
                       lineItems : SalesLineItem [1..*]
                                   or                                           ...
                       lineItems : SalesLineItem [1..*] {ordered}

                       ...
Two ways to show a
collection attribute


                                Sale                                                  SalesLineItem
                                                                         1..*
                       time: DateTime                                           ...
                                                                   lineItems
                       ...                                    {ordered, List}   ...


                                       notice that an association end can optionally also
Preferred, less visual clutter         have a property string such as {ordered, List}



   public class Sale {
            private List<SalesLineItem> lineItems = new
   ArrayList<SalesLineItem>();
    // …
   }
                                  Note Symbols
• A note symbol may represent several things, such as:
     – a UML note or comment, which by definition have no
       semantic impact
     – a UML constraint, in which case it must be encased in
       braces '{…}'
     – a method body
                                                                           Register
«method»
// pseudo-code or a specific language is OK                       ...
public void enterItem( id, qty )
{                                                                 endSale()
   ProductDescription desc = catalog.getProductDescription(id);   enterItem(id, qty)
   sale.makeLineItem(desc, qty);                                  makeNewSale()
}                                                                 makePayment(cashTendered)

                        UML note symbol showing a method body
        Operations and Methods
• Operations
  – One of the compartments of the UML class box shows the
    signatures of operations for many examples. official format
    of the operation syntax is:
      • visibility name (parameter-list) {property-string}
  – Preferred
      • visibility name (parameter-list) : return-type
        {property-string}
  – UML allows the operation signature to be written in any
    language, such as Java, assuming the reader or tool is
    notified. For example, both expressions are possible:
      • + getPlayer( name : String ) : Player {exception IOException}
      • public Player getPlayer( String name ) throws IOException
                Keywords
• UML keyword is a textual adornment to
  categorize a model element.
  – the keyword to categorize that a classifier box is an
    interface is «interface».
  – Most keywords are shown in guillemet (« ») but
    some are shown in curly braces, such as
    {abstract}, which is a constraint containing the
    abstract keyword.
• Stereotypes
  – Extension mechanism in UML
                Class Diagrams
• More UML notation
  – Generalization
     • Same as inheritance when in design mode
     • Solid line with open arrow
       Superclass               Subclass




  – To indicate abstract, use italics
  – For final classes or operations, use {leaf}
                  Class Diagrams
• There are many kinds of dependency; here are some
  common types in terms of objects and class
  diagrams:
  – having an attribute of the supplier type
  – sending a message to a supplier; the visibility to the
    supplier could be:
     • an attribute, a parameter variable, a local variable, a global variable,
       or class visibility (invoking static or class methods)
  – receiving a parameter of the supplier type
  – the supplier is a superclass or interface
• All of these could be shown with a dependency line in
  the UML, but some of these types already have
  special lines that suggest the dependency.
           Guidelines
• Guideline: In class diagrams use
  the dependency line to depict
  global, parameter variable, local
  variable, and static-method (when
  a call is made to a static method
  of another class) dependency
  between objects.
                   Showing Dependency
        the Sale has parameter visibility to a
        ProductDescription, and thus some kind of
        dependency
                                                                ProductDescription

                                                              ...
                 Sale
                                                              ...
...

updatePriceFor( ProductDescription )
...                                                                 SalesLineItem

                                                       1..*   ...
                                                lineItems
                                                              ...



  If ProductDescription‟s public interface changes, then Sale may need to be modified
         Showing Dependency
public class Sale
{
  public void updatePriceFor(ProductDescription
  description )
  {
      Money basePrice = description.getPrice();
      //…
   }
  // …
}
                Showing Dependency

the doX method invokes the runFinalization
static method, and thus has a dependency on
the System class
                                                             System

                                                      ...
                    Foo
                                                      runFinalization()
          ...                                         ...

          doX()
          ...


                Dependency due to calling a static method
         Showing Dependency
public class Foo
{
  public void doX()
  {
      System.runFinalization();
      //…
  }
  // …
}
           Optional Dependency Labels in UML


                                               Clock                                                  B
     Window                «call»                                A            «create»
                                           getTime()                                            ...
                                           ...

a dependency on calling on operations of                a dependency that A objects create B objects
the operations of a Clock



                 Using keywords or stereotypes to indicates type of dependency
                                                    Interface
                                       socket line notation

                Timer                  Window1 uses the Timer                     dependency line notation
      Window1
                                       interface
                                                                                  Window2 has a dependency on the
                                       it has a required interface                Timer interface when it collaborates
                                                                                  with a Clock2 object

  «interface»                                       Clock2
     Timer
                                              ...                Timer                          Window2
getTime()
                                              getTime()
                                              ...

                 Clock1                                                Ball & Socket notation new to UML 2!
                 implements and
                 provides the                       Clock3
                 Timer interface
      Clock1                                  ...                    Timer                      Window3
...                                           getTime()
                                              ...
getTime()                                                                         socket line notation
...                     lollipop notation indicates Clock3 implements
                        and provides the Timer interface to clients               Window3 has a dependency on the
                                                                                  Timer interface when it collaborates
                        Timer is a provided interface                             with a Clock3 object
         Composition Over Aggregation
              1         0..7                 composition means
Hand                           Finger
                                             -a part instance (Square) can only be part of one
                                             composite (Board) at a time

          composition                        -the composite has sole responsibility for management of
                                             its parts, especially creation and deletion


              1          40                                            1       1..*
Board                          Square                   Sale                           SalesLineItem




 •      Aggregation is not a distinctive term (same as regular association)
 •  Aggregation is a vague kind of association in the UML that loosely
   suggests whole-part relationships
 • Use composition when appropriate
 • Composition is the „strong‟ form of aggregation
                               Constraints

                                    three ways to show UML constraints

              Stack

size : Integer { size >= 0 }

push( element )                      { post condition: new size = old size + 1 }
pop() : Object


                                      {
                                      post condition: new size = old size – 1
                                      }
         Qualified Association

(a)   Product                        Contains                        Product
      Catalog                                                       Description
                        1                                 1..*



                                 1                         1
      Product                            Contains                   Product
(b)                     itemID
      Catalog                                                      Description



                qualifier                       multiplicity reduced to 1
                     Association Class

                                        Employs
        Company           *                                     *        Person




a person may have                    Employment
employment with several
companies                       salary
                                startDate




                Association class simplifies many-to-many multiplicity
        User-Defined Compartments
                                DataAccessObject

                          id : Int
                          ...

                          doX()
                          ...

                             exceptions thrown
                          DatabaseException
                          IOException

Additional compartments          responsibilities
for a class               serialize and write objects
                          read and deserialize objects
                          ...
                    Active Class

                                                          active class

                                         Clock
  «interface»
   Runnable                        ...
run()                              run()
                                   ...




                Active class executes in its own thread
     Interaction and Class Diagrams
                                : Register                                : Sale


    makePayment(cashTendered)

                                             makePayment(cashTendered)


messages in interaction
diagrams indicate operations
in the class diagrams                                                              classes
                                                                                   identified in the
                                                                                   interaction
                                                                                   diagrams are
                 Register                                          Sale            declared in the
                                                        1                          class diagrams
          ...                                                ...
                                               currentSale
          makePayment(…)                                     makePayment(…)
          ...                                                ...



                      • Relationship between class & interaction diagrams
                      • Models must be consistent with each other

								
To top