Session02 Part Ii

553 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
553
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Session02 Part Ii

  1. 1. UML Exercise <ul><li>A &quot;draw&quot; 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). </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
  2. 2. Object Copying <ul><li>Let’s first look at some thing simple and fundamental </li></ul><ul><li>How do we copy an object </li></ul><ul><li>Consider an example of a Car with an Engine </li></ul>
  3. 3. Venkat’s past recommendation <ul><li>Before reading Bloch’s Effective Java! </li></ul><ul><li>Writing a Copy Constructor is a bad idea </li></ul><ul><li>Why? </li></ul><ul><li>Leads to extensibility issues </li></ul>
  4. 4. Bloch’s Recommendation <ul><li>Cloning comes with its own problems </li></ul><ul><ul><li>No constructor called when object cloned </li></ul></ul><ul><ul><li>If a class has final fields, these can’t be given a value within close method! </li></ul></ul><ul><li>Bloch’s recommendation: </li></ul><ul><li>&quot;... you are probably better off providing some alternative means of object copying or simply not providing the capability.&quot; He goes on to say &quot;A fine approach to object copying is to provide a copy constructor.&quot; </li></ul><ul><li>I agree with the part “simply not providing the capability” </li></ul><ul><li>But providing a copy constructor has problems mentioned earlier? </li></ul><ul><li>What’s the solution? </li></ul>Further Reading: [1]
  5. 5. A combined approach <ul><li>Implement the clone method </li></ul><ul><ul><li>but not the way it is generally done in Java </li></ul></ul><ul><li>Write a protected copy constructor </li></ul><ul><li>From the clone method, invoke the protected copy constructor </li></ul>Further Reading: [2]
  6. 6. References <ul><li>1. Effective Java, Joshua Bloch </li></ul><ul><li>2. “Why copying an object is a terrible thing to do?” (downloadable from </li></ul><ul><li>http:// www.AgileDeveloper.com/download.aspx ) </li></ul>

×