Java Reference
In-Depth Information
description, and the price (the booked field is initially set as false). This is given in the fol-
lowing code:
public class Seat {
public Seat(int id, String name, int price) {
this(id, name, price, false);
private Seat(int id, String name, int price, boolean
booked) { = id; = name;
this.price = price;
this.booked = booked;
public Seat getBookedSeat() {
return new Seat(getId(), getName(), getPrice(),
// Other Constructors, Fields and Getters omitted for
Note that our Seat object is an immutable one. After we create an instance, we will not
be able to change its state (the value of the fields, all of them are final, and no setters are
exposed). This means that when we return a Seat object to the client (local or remote), it
will be only available for reading.
Next, the singleton bean exposes four public methods; the getSeats method returns an
unmodifiable collection of Seat objects, which will return the information regarding
whether they have been reserved or not to the user. The collection must be unmodifiable
because our Singleton exposes a no-interface view, which means that we are using the
pass-by-reference semantic. If we will not protect the collection, then every change on an
element of the returned collection will be done on our cache. What's more, the client can
add or remove elements to our internal collection!
The getSeatPrice method is an utility method, which will pick up the seat price and
return it as int , so it can be used to verify whether the user can afford to buy the ticket.
Search WWH ::

Custom Search