UML Exercise - PowerPoint PPT Presentation

About This Presentation
Title:

UML Exercise

Description:

A 'draw' utility program lets users draw several geometric objects on a diagram. ... Consider an example of a Car with an Engine. UML2-3. Venkat Subramaniam ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 7
Provided by: venkatsub
Learn more at: https://www2.cs.uh.edu
Category:
Tags: uml | car | diagram | engine | exercise

less

Transcript and Presenter's Notes

Title: UML Exercise


1
UML Exercise
  • A "draw" utility program lets users draw several
    geometric objects on a diagram. A geometric
    object may be a Circle, Rectangle, Square or
    Triangle. A diagram may also contain a text
    object. Each geometric object provides a draw()
    function to draw itself. They also contain
    information about thickness of border lines
    (unsigned short), and shading (filling) (a byte
    value for color).
  • A Circle holds its center and radius and provides
    functions to get and set these values, in
    addition to a function that returns the area. A
    Triangle holds the coordinates of the three
    sides and provides functions to get and set
    these values. A Rectangle and Square also hold
    the coordinates of their four sides and provide
    functions to get and set these values. A Text
    Object has a string, coordinates of the first
    character, color of the text, its font name,
    point size and if it is bold, italics,
    underlined or stricken. All these parameters
    may be modified and accessed.
  • In addition to the above geometric objects, the
    diagram may also hold sheets. A sheet is an
    object that appears grayed out on the diagram and
    may hold a title. A user may double click on a
    sheet and it opens a window of its own. The
    difference between a sheet and a diagram is
    that a sheet may be contained in a diagram. A
    sheet may hold any of the geometric objects
    described above, including other sheets.

2
Object Copying
  • Lets first look at some thing simple and
    fundamental
  • How do we copy an object
  • Consider an example of a Car with an Engine

3
Venkats past recommendation
  • Before reading Blochs Effective Java!
  • Writing a Copy Constructor is a bad idea
  • Why?
  • Leads to extensibility issues

4
Blochs Recommendation
  • Cloning comes with its own problems
  • No constructor called when object cloned
  • If a class has final fields, these cant be given
    a value within close method!
  • Blochs recommendation
  • "... you are probably better off providing some
    alternative means of object copying or simply not
    providing the capability." He goes on to say "A
    fine approach to object copying is to provide a
    copy constructor."
  • I agree with the part simply not providing the
    capability
  • But providing a copy constructor has problems
    mentioned earlier?
  • Whats the solution?

Further Reading 1
5
A combined approach
Further Reading 2
  • Implement the clone method
  • but not the way it is generally done in Java
  • Write a protected copy constructor
  • From the clone method, invoke the protected copy
    constructor

6
References
  • 1. Effective Java, Joshua Bloch
  • 2. Why copying an object is a terrible thing to
    do? (downloadable from
  • http//www.AgileDeveloper.com/download.aspx)
Write a Comment
User Comments (0)
About PowerShow.com