3SFE514 Objectoriented Design - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

3SFE514 Objectoriented Design

Description:

Class diagrams model the compile-time structure of a system ... navigability (shown by an arrowhead) shows which direction the reference 'points' in ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 21
Provided by: usersW
Category:

less

Transcript and Presenter's Notes

Title: 3SFE514 Objectoriented Design


1
3SFE514 Object-oriented Design
  • Lecture 2 Class diagrams

2
Class diagrams
  • Class diagrams model the compile-time structure
    of a system
  • They show classes and the relationships between
    them
  • UML defines 4 types of relationship
  • associations
  • generalization (inheritance, extends)
  • implementation (of an interface)
  • dependencies

3
Stock Control System
  • All parts of the same kind have the same
    attribute values (name, number, cost)
  • wasteful of resources
  • hard to maintain, eg if the cost changes
  • Better to create a new class for shared data
  • call this a catalogue entry
  • describes a type of part
  • each individual part described by one entry

4
The Catalogue Entry Class
  • Contains data describing types of part
  • public class CatalogueEntry
  • private String name
  • private long number
  • private double cost
  • public CatalogueEntry(String nm, long num, double
    cost)
  • name nm
  • // etc
  • public String getName( ) return name
  • // etc

5
Linking Parts to Catalogue Entries
  • Catalogue entry class defines shared data
  • Each part holds a reference to an entry
  • public class Part
  • private CatalogueEntry entry
  • public Part(CatalogueEntry e) entry e
  • When parts are created, an entry is given
  • CatalogueEntry screw new CatalogueEntry(screw,
    28834,0.02)
  • Part s1 new Part(screw)
  • Part s2 new Part(screw)

6
Associations
  • Model data relationships between classes
  • at run time a part will hold a reference to a
    catalogue entry

7
Association Properties
  • How to model a data member of a class
  • if it holds a simple data value, model as an
    attribute
  • if it holds a reference to another object, model
    as an association
  • Associations have
  • a name (optional)
  • two (or more) association ends

8
Association Ends
  • Each end of an association can have a number of
    properties
  • rolename
  • can be derived from variable names in source code
  • visibility
  • the access level of the corresponding data member
  • multiplicity
  • stating how many references the source object
    holds
  • navigability (shown by an arrowhead)
  • shows which direction the reference points in

9
Multiplicities
  • Associations derive from
  • data members that hold a single reference
  • usual multiplicity 0..1 (zero or one) (null
    reference)
  • can specify exactly 1 (the UML default)
  • a data structure that can hold many references
  • usual multiplicity 0.. or (zero or more)
  • can specify more precisely if required, eg 1..
    (one or more)
  • Multiplicities have lower and upper bounds
  • represents an unbounded multiplicity

10
References between objects
  • Associations model variables which hold
    references
  • Different cases
  • instance variables (including static)
  • model as an association (default case)
  • parameters
  • association with the stereotype ltltparametergtgt
  • local variables
  • association with the stereotype ltltlocalgtgt

11
Implementing Assemblies
  • Assemblies could be implemented using a data
    structure to hold references to parts
  • public class Assembly
  • private Vector parts new Vector()
  • public void add(Part p)
  • parts.addElement(p)
  • These links are instances of a new association

12
The Contains Association
  • Contains is the association name
  • an assembly contains parts
  • parts is the rolename
  • An assembly can contain zero or more parts
  • this is documented by the multiplicity

13
Polymorphism
  • Using inheritance to construct hierarchies of
    assemblies and subassemblies
  • public abstract class Component ...
  • public class Part extends Component ...
  • public class Assembly extends Component
  • private Vector components new Vector()
  • public void addComponent(Component c)
    components.addElement(c)

14
Generalization
  • Javas extends relationship is represented by
    generalization in UML
  • Also called specialization if viewed downwards

15
Properties of Generalization
  • Generalization is not an association
  • usually unlabelled
  • never has multiplicity annotations
  • Explained by substitutability
  • an instance of a subclass can be used wherever an
    instance of a superclass is expected
  • similar to reference conversions in Java
  • Superclass features are inherited

16
A Polymorphic Association
  • Assemblies contain Components
  • instances of Contains can link assemblies to
    instances of both Component subclasses
  • the Component class is abstract (no instances)

17
Design models
  • Diagrams which reflect the structure of the
    software artefacts under consideration
  • contrast analysis models which reflect the
    structure of the application
  • developer vs user perspective
  • UML used for both analysis and design
  • What's the relationship between code and design
    model?

18
Code Generation
  • The activity of producing eg Java code
    corresponding to a UML model
  • Potentially a useful aid to implementation
  • A straight-forward rule-based process
  • UML class becomes Java class
  • associations implemented by storing references
  • Widely supported by tools

19
Reverse Engineering
  • The activity of producing a UML model from eg
    Java code
  • Useful for dealing with undocumented or legacy
    systems
  • Hard to automate abstraction
  • can extract syntactic information from code
  • harder to recapture the abstract design

20
Round-trip Engineering
  • Designs evolve as development progresses
  • One-off generation of code or design of limited
    use
  • Ideally, would be able to maintain both code and
    UML in parallel
  • change to one reflected automatically in the
    other
  • move in a 'round trip' from one to the other and
    back again
Write a Comment
User Comments (0)
About PowerShow.com