- Object Relations:
- Associations between objects (uni-directional & bi-directional association)
- Aggregation & Composition
- Dependency
Associations between objects
- If 2 objects from different classes have a reference that lasts beyond the current method, there is a link between the objects
- The driver class, containing main(), establishes links between itself and the objects it creates.
- If we know at the time of design that the objects of 2 (or more) different classes will have lasting links, there is an association between the two classes. A link is the realisation at run time of an association between specific instances of the classes.
- If an object needs to send a message to another object of the same class type, it has a self-referencing association
- If an association is implemented as a permanent link between two objects, and neither of the objects can be substituted by another object, the association is immutable.
- If one of the objects can be substituted for another object, the association is mutable.
- In uni-directional systems, objects sending messages maintain references to the objects that receive the messages (linked objects). This can be done with constructors / mutators
- A bi-directional association is when both objects know of each other and are linked to each other.
- Both objects must maintain references to each other
- Both ends of a bi-directional association must be updated simultaneously
- Have to instantiate the other class with a default constructor, use a mutator method to update the reference variable (creating the link between the two classes)
- The class with a reference variable in it's constructor takes responsibility for maintaining the association, by sending a message from it's constructor asking the other class to update it's reference variable (using the 'this' variable)
Example from this week's sample program:
public Player(String name, Team team, int birthYear, int birthMonth, int birthDay)
{
this.name = name;
this.birthYear = birthYear;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
setTeam(team); // Establish the bi-directional link of this player to that team.
}
Aggregation & Composition
- Aggregation is a strong form of grouping where similar objects are grouped together into one class
- a gaggle of geese is an aggregate of geese
- Individuals which make up the aggregate can exist independently of the aggregate, and may be involved in several associations
- Composition is a form of association where components are an integral part of the composite's being- the composite cannot exist if it doesn't have some of it's components. The components can only be part of one composite at any one time.
- A toothbrush is not a toothbrush without the bristles, handle, etc, and the bristles, handle, etc cannot be on more than one toothbrush at a time.
- In Java, aggregation & composition are implemented the same as simple association
- Component classes are declared as reference variables inside aggregate or composite classes
My learnings:
- The assessments we have centre around building a human resource management systems, and I feel all of this uni/bi-directional association is a hack to re-create a database. I don't understand why we'd create methods to store employee details - wouldn't you want to create a database, store the information in the database, and use Java to create a GUI to enter the data? When does logical program design get covered?
- The first 3 weeks of Java are covered in 132 pages of my study guide, which is 47% of the book, whereas this is only 27% of the course (11 weeks + 1 week of break + 1 week of revision). It feels like a lot has been crammed into the "revision" section.
- Although we've flicked all over the textbook, so far I've read 260 pages. Ignoring all the appendixes, index, solutions, etc, the textbook is less than 1000 pages, so I've already read more than a quarter of it. Which means that the textbook reading is much better paced - there's enough time to read the whole book.
- There's too many references to things I haven't learnt yet, such as "cloning" objects (Interfaces will be in Module 5), Exceptions, and, uh, I can't remember what else. Why do I need to be told about useful things that I haven't learnt yet??
- It's all starting to sink in. Java is making more sense this time than when I did Computer Programming 1. That's a start :-)
No comments:
Post a Comment