Download presentation
Presentation is loading. Please wait.
1
Relationships amongst Objects
By Rick Mercer with help from Object-Oriented Design Heuristics, Arthur Riel
2
Outline Consider the relationships between objects
A few design heuristics Consider role play in current project
3
Relationship Between Objects
1) Object relationships occur when one object sends a message to itself public class Stack<E> { public boolean isEmpty() {} public void push(E element) { if(this.isEmpty()) first = new Node(element, first); else … }
4
An object relationship occurs when…
2) An object sends a message to an instance variable private JTextArea textEditor = new JTextArea("You can edit me"); public FirstGUI() { this.setSize(500, 400); textEditor.setLineWrap(true); textEditor.setWrapStyleWord(true); textEditor.setBackground(Color.RED); textEditor.setFont(new Font("Courier", Font.BOLD, 12));
5
An object relationship occurs when…
3) One object sends messages to other objects passed as formal parameters public class JukeBoxAccount extends JFrame { // Someone else asks this object if a song can be played by a user public boolean mayQueueUp(Song song, JukeboxAccount user) { return song.canBePlayedToday() && user.canSelect(song.getPlayTimeInSeconds()) }
6
An object relationship occurs when…
4) One object constructs another public class JukeBoxAccount extends JFrame { private class SelectListener implements ActionListener { public void actionPerformed(ActionEvent ae ) { if (mayQueueUp(selectrSong, currentUser)) PlayList playList = new PlayList(song); // Not a good idea else … }
7
Design Guidelines Minimize the number of objects with which another object collaborates Worst case: The design has a collection of simple objects where each one uses all others One class could contain several smaller objects
8
A Meal Class Wrap 3 Smaller Classes
Melon cost() Restaurant Patron cost() Steak Meal Pie cost() Melon cost() Restaurant Patron cost() cost() Steak Pie cost()
9
Meal Wrapper Class Encapsulation makes 4 messages look like one
Containment simplifies things RestaurantPatron collaborates with one object now, instead of three
10
If you have instance variables, use them
Containment implies a relationship, messages should be sent to contained objects If no messages are sent to a contained object, it doesn't belong, Put the attribute in the right class An exception: Container classes ArrayList, HashMap, TreeMap, and so on, do not send messages to their elements Okay, equals or compareTo sometimes The collection’s responsibility is to add, find, and remove elements
11
Don't use too many instance variables
Most of the methods in a class should be using most of the instance variables most of the time If not, perhaps there should be two classes Classes should not contain more instance variables than a developer can fit in short term memory Maximum should be 7 plus or minus 2 That's about 5 to 9 instance variables
12
Contained objects don’t talk to their containers
A class must know what it contains, but the contained class should not know who contains it It's okay for a bedroom to know it has a clock, but the clock should not be dependent on the bedroom currentAccount should not send messages to Jukebox The JukeboxAccountCollection need not know it is contained in an instance of Jukebox should be standAlone All contained objects are loosely coupled Meaning they are not dependent on each other
13
Use a Mediator to Coordinate Activities
Objects that share lexical scope -- those in the same containment --need not have relationships between them Consider an ATM that contains a card reader and a display screen The card reader should not send a message to the display screen better reuse--could use the card reader software for a security entrance without taking the display screen
14
Violation Increases Complexity
An ATM may contain card reader, display screen, and keypad The ATM should coordinate activities between these three objects Avoid collaborations between instance variables if possible ATM displayPIN() HaveACard? getPIN() Display Card Reader Keypad
15
In our system? The Jukebox may contain currentUser selectedSong
But they need not send messages to each other\ but they could currentUser.canPlay(selectedSong) Jukebox canBePlayedToday()? allowedToPlay()? selectSong currentUser
16
Information Expert The most basic, general principle of assigning responsibilities (behavior/methods) is to Assign the responsibility to the object that has the information necessary to fulfill it. “That which has the information, does the work.” Who decides if the jukebox object can select? Most important skill in OO Design Which objects should do what? find the objects assign responsibilities to the correct objects
17
Role Play Jukebox: Now I need to determine whether or not the current user can play the selected Song. Who is responsible for knowing the playing time of any Song? Song: It seems like I should be responsible for knowing the duration of my Song. For example, it might take 3 minutes and 34 seconds to be completely played. I'll add the responsibility “know play time”. Jukebox: Okay, now I should check to make sure the user is able to select this Song before telling playlist to queue it up. I seem to remember I cannot simply play a Song without checking on a few things. I know the current user and the selected Song. What do I do now?
18
Alternative #1 JukeBox: So tell me Song, how many minutes and seconds do you require to be played? Song: 3 minutes and 34 seconds. JukeBox: JukeboxAccount, do you have 3 minutes and 34 seconds credit? JukeboxAccount: I'll be responsible for maintaining remaining time credit, so I can answer that, Yes, I have enough credit. JukeBox: JukeboxAccount, have you played fewer than 2 Songs? JukeboxAccount : I have not played 2 songs today. JukeBox: Okay, now we can play the Song. Here it playList. PlayList: Okay JukeBox, I will add this Song to my queue. I'll take care of the add(String fileName) responsibility
19
Alternative #2 JukeBox: JukeboxAccount, can you play this Song?
JukeBoxAccount: It feels as though I should be responsible for maintaining my own time credit, it seems appropriate that I should also know how many Songs I've played today. So I should be able to do some simple calculations to give you the answer you seek. Yes JukeBox, I can play the Song you sent me. I'll add these responsibilities to my CRC card: know how much time credit I have left know how many Songs I've played on this date respond to a message like this: JukeboxAccount.canSelect(selectedSong)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.