Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPUT 301: Lecture 05 More Java & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous courses.

Similar presentations


Presentation on theme: "CMPUT 301: Lecture 05 More Java & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous courses."— Presentation transcript:

1 CMPUT 301: Lecture 05 More Java & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous courses by Ken Wong, Eleni Stroulia Zach Dodds, Martin Jagersand

2 2 Today: Examles in UML and Java of 1.Association 2.Aggregation 3.Referencing: “this” 4.Interfaces

3 3 Good Classes What makes a good class?

4 4 Good Classes Major properties: –correct –safe –efficient

5 5 Challenges Design, Abstraction –Divide into parts, assign responsibilities Organize collaborations –Association, Aggregation –identify and form relationships among parts Implementation: Language features –understand syntax and semantics of the programming language

6 6 Challenges Debugging –systematically discovering the cause of errors Documentation –describe how to use the class to promote effective reuse Packaging –organize collections of classes into a cohesive unit

7 7 Remember: Collaborating objects Use association or aggregation to form groups of objects Connect between objects by writing “communication methods” Each object has a particular role in the group (remember abstraction)

8 8 Association A Message appears in a Frame. A Message object is associated with 0 or 1 Frame objects. A Frame object is associated with 0 or more Message objects.

9 9 Association public class Message { private Frame msgFrame; … public void displayIn( Frame whichFrame ) { msgFrame = whichFrame; } … } … window = new Frame( … ); greeting = new Message(…,”Hello”,.. ); // associate greeting with window greeting.displayIn( window ); // greeting uses associated window greeting.clear(); …

10 10 Association Parts (objects) related by parameter passing Not saving state from other objects

11 11 Aggregation Use aggregation to create containment structures. Whole “has-a” part. Encapsulation. Aggregation versus association.

12 12 Aggregation

13 13 Static Aggregation public class StopWatch { private Button startButton; private Button stopButton; private Clock clock; private ClockControl control; private Counter count; private Message message; private Canvas canvas; private Panel panel; public StopWatch( Location at, int interval ) { … } public void showIn( Frame frame ) { frame.add( canvas ); frame.add( panel ); } Aggregated Associated with Frame

14 14 Aggregation: UML Stopwatch Button Clock Panel

15 15 Advantages What are some advantages of using aggregation?

16 16 Advantages Simplicity –contained objects can be referred as a single unit Safety –contained objects are protected from misuse Specialization –provided specialized operations

17 17 Advantages Structure –containment relationship expresses design intent Substitution –alternative implementations can be substituted

18 18 Types of Aggregation Static: –lifetimes of contained objects are identical to the lifetime of the containing object –e.g., Frame, Location, Shape Dynamic: –number of contained objects may not be known in advance –e.g., Vector

19 19 Static Aggregation Indirect control: –the extent to which the object’s user is aware of the type or number of the contained objects –the extent to which the object’s user can change the organization of the contained objects

20 20 Static Aggregation Design issues Stopwatch example: –placement of buttons and time message? –labels of buttons? –time interval?

21 21 Static Aggregation Issue: –balance between too much indirect control versus none

22 22 Static Aggregation Providing degrees of control: –design several similar containing classes –use an auxiliary control information class (e.g., StopWatchLayout) –provide overloaded constructors to customize the contained objects

23 23 Static Aggregation

24 24 Static Aggregation public class StopWatch { private Button startButton; private Button stopButton; private Clock clock; private ClockControl control; private Counter count; private Message message; private Canvas canvas; private Panel panel; public StopWatch( Location at, int interval ) { … } public void showIn( Frame frame ) { frame.add( canvas ); frame.add( panel ); }

25 25 Static Aggregation Interface methods to access internal structure: public void redraw() { message.draw(); } public void start() { clock.on(); } public void stop() { clock.off(); } }

26 26 Dynamic Aggregation The Java Vector class can be used to implement a dynamic, indexed collection of objects. In Java, an object of any class can be treated as if it is also an instance of the Object class. The containing object of the aggregation has a Vector instance.

27 27 Dynamic Aggregation public class Vector { … public Vector() { … } public void addElement( Object e ) { … } public void insertElementAt( Object e, int i ) { … } public Object elementAt( int i ) { … } public void removeElement( Object e ) { … } public void removeElementAt( int i ) { … } public int size() { … } … }

28 28 More About this Chaining constructors: public class Frame { … public Frame( String name, Location p, Shape s ) { … } public Frame( String name, Shape s, Location p ) { this( name, p, s ); } public Frame( String name, Shape s ) { this( name, s, new Location( 0, 0 ) ); } … }

29 29 More About this Removing ambiguity: public class Rectangle { private Location topLeft; private Location botRight; private Canvas canvas; … public void drawIn( Canvas canvas ) { this.canvas = canvas; // canvas = canvas is ambiguous } … }

30 30 More About this Returning this PolyShape poly = new PolyShape( 20, 20 ); poly.right( 100 ); poly.down( 50 ); poly.mark(); poly.left( 20 ); poly.mark(); poly.down( 30 ); poly.left( 50 ); poly.mark(); poly.draw(); PolyShape poly = new PolyShape( 20, 20 ); poly.right(100).down(50).mark(); poly.left(20).mark(); poly.down(30).left(50).mark(); poly.draw();

31 31 Copying Objects Copy constructor: Color red = new Color( 255, 0, 0 ); Color redCopy = new Color( red ); public class Color { private int red; private int green; private int blue; … public Color( Color other ) { red = other.red; blue = other.blue; green = other.green; } … }


Download ppt "CMPUT 301: Lecture 05 More Java & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous courses."

Similar presentations


Ads by Google