I am a complete beginner so forgive my ignorance. I have created a project where I have used composition in some classes. In my Cinema class I have a Schedule object.
public class Cinema {
private String name; //set via constructor
private int seatCount; // set in constructor
private int rowCount; // set in constructor
private int cleanUpTime; //set via constructor
private LocalTime openTime = LocalTime.of(9, 30);
private LocalTime closeTime = LocalTime.of(23, 59);
private LocalTime peakTime = LocalTime.of(16, 30);
private int costPerHour; //set via constructor
private Schedule schedule = new Schedule(this);
//Constructors, other methods....
}
A Schedule belongs to a Cinema. It needs a Cinema object for some of its methods. A Schedule can not exist without a Cinema.
When reading about OOP I am led to believe that I have created a class that is now tightly coupled to another class and that is potentially bad.
Therefore how could I improve this design?
I have a few tightly coupled classes it seems. e.g Booking class and Customer class. A booking has a Customer and a Customer contains a list of all Bookings they have made.
I thought I was using composition and that would be good but now I am confused as I have read about coupling.
Please help me understand.
There has to be some coupling. A Cinema and a Schedule are not completely independent.
A Schedule belongs to a Cinema.
So far, so good.
It needs a Cinema object for some of its methods.
Nope. A Schedule object should be able to stand on it's own.
Since you haven't provided any code, I'll make the following assumptions.
So here's a Schedule class.
public class Schedule {
private final Calendar showingTimestamp;
public Schedule(Calendar showingTimestamp) {
this.showingTimestamp = showingTimestamp;
}
public Calendar getShowingTimestamp() {
return showingTimestamp;
}
public int getShowingWeekday() {
return showingTimestamp.get(Calendar.DAY_OF_WEEK);
}
}
The only field on the Schedule class holds a showing date and a showing time. I showed you how to use a Calendar method to get the weekday.
Here's a bare bones Movie class.
public class Movie {
private final String name;
private List<Schedule> showingList;
public Movie(String name) {
this.name = name;
this.showingList = new ArrayList<>();
}
public void addShowing(Schedule schedule) {
this.showingList.add(schedule);
}
public List<Schedule> getShowingList() {
return Collections.unmodifiableList(showingList);
}
public String getName() {
return name;
}
}
The Movie class knows about the Schedule class. The Schedule class does not know about the Movie class.
Finally, here's the Cinema class.
public class Cinema {
private final String name;
private List<Movie> currentMovieList;
public Cinema(String name) {
this.name = name;
this.currentMovieList = new ArrayList<>();
}
public void addCurrentMovie0(Movie movie) {
this.currentMovieList.add(movie);
}
public void removeMovie(Movie oldMovie) {
for (int index = currentMovieList.size() - 1; index >= 0; index--) {
Movie movie = currentMovieList.get(index);
if (movie.getName().equals(oldMovie.getName())) {
currentMovieList.remove(index);
}
}
}
public List<Movie> getCurrrentMovieList() {
return Collections.unmodifiableList(currentMovieList);
}
public String getName() {
return name;
}
}
The Cinema class knows about the Movie class, and indirectly, about the Schedule class. The Movie class does not know about the Cinema class.
I hope this has been helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With