Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Clients: Use Observer Pattern ?

 
Daniel Breitner
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I was just wondering about the following:

Client A books a room, and a minute later Client B wants to book the same room.

Because my instructions say "Your server must be capable of handling multiple concurrent requests..." I was wondering how to tell Client B that this Room is already booked.

A) Use the Observer Pattern to pass the information about the booking from the Server on to Client B

B) Throw an Exception when a booked room should be booked again

C) Overwrite it


Did anyone of you implement A ?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

My business code to book a room is implemented like this:
  • lock record
  • read record
  • if room booked, throw exception
  • if room not booked, update record
  • unlock record

  • So that would represent your option B.

    Option C is a because 2 clients will be showing up at the hotel room and won't be pleased to see if the room is double-booked (unless it would be jennifer aniston and me, I don't have any problem with sharing my room with her, even if it is just a single bed )

    Option A is also a nice solution, but I think it will complicate your solution and as a downside will create a lot of (unnecessary) network traffic if you have a lot of clients. And what if your observer is a bit too late and client B is already in the booking process? So you will also have to implement another check (like option B) to be 100% certain that a room is not double-booked

    So I would go for B: simple, easy to understand and it will certainly prevent double-bookings (and not unimportant: you'll get full credit for it if you implement it correctly).

    Kind regards,
    Roel

     
    Roberto Perillo
    Bartender
    Posts: 2271
    3
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Howdy, Daniel!

    A) Use the Observer Pattern to pass the information about the booking from the Server on to Client B


    Well, that isn't necessary. Here's what I did: each client's window is only updated after each trip to the server. So, you have your window, and let's say that there were 5 updates that you don't know. When you go to the server (for booking a room, for instance), your window is updated. But if you want, you can use the Observer pattern (I know my good buddy Roel used it, and he did very well he got 400/400).

    B) Throw an Exception when a booked room should be booked again


    In my case, when you try to book a room that was already booked, I throw a RoomAlreadyBookedException.

    C) Overwrite it


    No! Otherwise, the locking mechanism wouldn't be necessary.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roberto Perillo wrote:But if you want, you can use the Observer pattern (I know my good buddy Roel used it, and he did very well he got 400/400).
    I did use the Observer pattern, but that had nothing to do with updating the window to see updates (from other clients) your client doesn't know about. That was just to notify a dialog window if the configuration settings panel were changed (so I only had to make 1 panel which I could reuse everywhere I needed), as described in Andrew's book.

    For trying to book an already booked room, I just throw a RoomAlreadyBookedException (just like you).

    Kind regards,
    Roel
     
    Daniel Breitner
    Ranch Hand
    Posts: 70
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, that sounds good

    I already started looking for Observer Patterns that don´t produce such big traffic ...

    I´ll do the ALreadyBookedException approach too

    Hooray to Coderanch forums

    daniel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic