#1,556 in Computers & technology books
Use arrows to jump to the previous/next product
Reddit mentions of Object Design: Roles, Responsibilities, and Collaborations
Sentiment score: 2
Reddit mentions: 2
We found 2 Reddit mentions of Object Design: Roles, Responsibilities, and Collaborations. Here are the top ones.
Buying options
View on Amazon.comor
Specs:
Height | 9 Inches |
Length | 7.25 Inches |
Number of items | 1 |
Weight | 1.38009376012 pounds |
Width | 1 Inches |
I actually ended up helping a few people on this exact problem. Small world, eh?
The way I would have designed it would be to start at how I wanted to use the system. This may seem odd and fairly abstract, but can lead some detail as to how it can be done:
Car entrant = new Car("Ford Fusion"); // or whatever
Spot spot = carPark.park(entrant);
if (spot == null) {
System.out.println("Could not find a spot for the car!");
} else {
System.out.println("Car was parked at: " + spot);
}
This tells me I automatically need a few things.
Car
classSpot
classCarPark
classCar
and returns theSpot
that the car was parked into; if available, otherwisenull
.So what was the purpose of this exercise? Merely to show one way to approach the problem. I'd personally say that the code above is fairly clean and understandable, would be a good way to have the final implementation be written.
---
From here you have a few options. One I found to be quite nice was having a
Floor
class that contains all theSpot
s available for that floor. This kinda makes sense, as multipleFloor
s make up a singleCarPark
and will usually be very very similar.Then as for parking the car, a modified Visitor Pattern was used. Which might seem a little confusing at first. However, think of it in the real world. A car will keep visiting available floors until it either runs out of floors or finds a spot to park. Just as you would do as you are trying to find a spot to park in.
So in the end it could look like:
public class Car {
public Spot visit(Floor floor) {
// find suitable empty spot
}
}
public class Floor {
private final List<Spot> spots; // all available spots
}
public class Spot {
// type, availability, etc...
}
public class CarPark {
private final List<Floor> floors;
public Spot park(final Car vehicle) {
for (Floor floor : floors) {
Spot spot = vehicle.visit(floor);
if (spot != null) {
return spot;
}
}
return null;
}
}
Very bare-bones implementation, but a good starting block. Here, I designed it with the real world in mind.
Car
,CarPark
,Spot
, these are all tangible real-world objects. And aCarPark
is basically a bunch ofFloor
s and eachFloor
has certainSpot
s available to it.This design here should mirror the real world fairly nicely. That is the purpose of OOP. Its to you can say things like
carPark.park(car);
and understand what that does.---
Hopefully you understood the steps I took here. If anything was too confusing, not explained in enough depth or anything, feel free to ask.
As to learning this stuff, here are my recommendations in order of importance:
Hopefully this helped clarify a few issues here.
These are all excellent. I'm a particular fan of the DDD book.
I really liked Kent Beck's Implementation Patterns. I've only thumbed through Object Design by Rebecca Wirfs-Brock, but she's brilliant, and the book has a good reputation.