Object Oriented Software Development Class diagrams: Towards implementation OOSAD Booklet Chapter 4 Seminar: Week 4 Brian Farrimond
2 Session Topics Object diagrams – snapshots for before and after Role names Implementing classes in Java Implementing associations
3 Object diagram Object diagrams represent actual objects and the links between them They give useful snapshots of the state of the system to help understand what changes to the system the functionality will cause
4 Class diagram
5 Object diagram
6 Object diagram after Fred borrows “Sing with Bing”
7 What has happened between the two object diagrams A new LP object with name A416 has been created and given the attribute value “Sing with Bing”. This new object has been linked to the Fred object.
8 What has happened between the two object diagrams A new LP object with name A416 has been created and given the attribute value “Sing with Bing”. This new object has been linked to the Fred object... This is what the code for borrowing an LP must achieve
9 Object diagrams Objects are connected by links A link is an instance of an association just like an object is an instance of a class
10 Using role names Role names are an alternative to association names More useful when we come to implementation
11 Using role names
12 Using role names If only one association between two classes then some UML authors permit the role names to be omitted The convention is that if role name omitted then a default role name is the name of the class at this end of the association
13 Using role names If only one association between role names then some UML authors permit the role names to be omitted The convention is that if role name omitted then a default role name is the name of the class at the this end of the association
14 Using role names Exercise 4.2 on Page 56
15 Using role names Exercise 4.2 on Page 56
16 Attributes and operations in class diagrams - reminder Three layered cake Class name Attribute list Operations list
17 Attributes and operations in class diagrams - reminder Things we miss out Attributes to indicate association with other classes in the diagram … Because … the association line provides that information already so we avoid clutter by avoiding duplication
18 Attributes and operations in class diagrams - reminder Things we miss out Operations to set and retrieve attribute values … Because … we can take those for granted and so save clutter
19 Implementing the Member class in Java We leave implementation in C++ until later in the module – it needs knowledge of pointers - scary Java hides the presence of pointers from us. It was one of the design goals of the creators of Java
20 Implementing the Member class in Java
21 Implementing the Member class in Java Attributes -> Java fields Operations -> Java methods
22 Implementing the Member class in Java Keywords private: fields not visible outside the class – need messages to access them
23 Implementing the Member class in Java Keywords public: the class and the methods can be used outside
24 Implementing associations in Java
25 Implementing associations in Java With role names:
26 Implementing associations in Java With role names: public class Ambulance { private Crew[] crew; : public class Crew { private Ambulance ambulance; :
27 Implementing associations in Java With role names: public class Ambulance { private Crew[] crew; : public class Crew { private Ambulance ambulance; : An array since there is more than one crew in each ambulance
28 Sending messages between objects Class diagrams record the way messages can pass between objects AB CD
29 Sending messages between objects In this diagram messages can be sent: between A and B between A and D between B and C AB CD
30 Sending messages between objects In this diagram messages can be sent: between A and B between A and D between B and C AB CD
31 Sending messages between objects In this diagram messages can be sent: between A and B between A and D between B and C AB CD
32 Sending messages between objects To send a message from A to C we must send a message to B that causes B to send a message to C AB CD
33 Sending messages between objects To send a message from A to C we must send a message to B that causes B to send a message to C AB CD
34 Sending messages between objects To send a message from A to C we must send a message to B that causes B to send a message to C AB CD
35 public class Ambulance { private Crew[] crew; : crew[1].doThis(); public class Crew { private Ambulance ambulance; : public void doThis() { } Sending messages between objects doThis() Sending the message Method to handle message
36 public class Ambulance { private Crew[] crew; : public void doThat(){ } public class Crew { private Ambulance ambulance; : ambulance. doThat() ; Sending messages between objects doThat() Sending the message Method to handle message
37 Navigation If we know that messages only travel one way along an association we can modify the class diagram to indicate this …
38 Navigation If we know that messages only travel one way along an association we can modify the class diagram to indicate this …
39 Navigation If we know that messages only travel one way along an association we can modify the class diagram to indicate this … Arrow head indicates direction of messages
40 public class Ambulance { private Crew[] crew; : public class Crew { : No attribute needed for Ambulance since we are not sending messages from here to the “owning” Ambulance Navigation
41 If a class diagram shows no arrowheads than assume the navigation is both ways If any arrow heads are present then the only navigability allowed is that indicated by the arrow heads Navigation
42 Summary Object diagrams provide snapshots for before and after Role names are an alternative to association names Attributes and operations can be recorded in class diagrams We can implement classes in Java We can implement associations in Java taking into account navigability
43 Exercises Ex 4.4 page 67 Ex 4.5 page 67 Ex 4.6 page 68