Skip to content

Statement

Consider the online auction site described in Problem 2.31 (Chapter 2, Page no 165). Suppose you want to employ the Publish-Subscribe (also known as Observer) design pattern in your design solution for Problem 2.31.

Problem 2.31 statement

  • You are to develop an online auction site, with selling, bidding, and buying services. The buying service should allow the users to find an item, bid for it and/or buy it, and pay for it.

  • the use case diagram for the system: Use Case Diagram

  • The class diagram for the system may look as follows: class diagram for problem 2.31

  • In the use case CloseAuction, the Seller reviews the existing bids for a given item, selects the and notifies the Buyer associated with the highest bid about the decision (this is why «participate» link between the use case CloseAuction and Buyer).

  • Assume that there are more than one bids posted for the selected item.

Questions

  1. Which classes should implement the Publisher interface?
  2. Which classes should implement the Subscriber interface?
  3. Explain your answer.

(Note: You can introduce new classes or additional methods on the existing classes if you feel it necessary for solution.).

solution

  • In the use class diagram, the Controller class has the ability to choose a specific bid by firing Controller.closeAuction() function, then there are multiple classes that interested in this event:
    • ItemsCatalog to list this item as not available for bidding anymore.
    • ItemInfo to be marked as reserved.
    • Bid list to be archived as it is no longer relevant (maybe can be saved for later auditing).
    • Seller to be notified about the decision.
    • Buyer to get this bid saved on his account.
  • There may also be other classes and objects that are interested in this event, like Payment, Logger, Accounting, etc. But the above examples are enough for the discussion in place.
  • To implement all of this logic into Controller.closeAuction(), the controller needs to know the extensive information about the objects that are interested in this event, like interfaces of the various functions that need to be called, then get and shape the arguments for each function call. Finally, the logic is executed sequentially.
  • By introducing the Publish-Subscribe design pattern, the controller can be implemented as a Publisher, and then all interested objects can subscribe to CloseAuction (SelectBid) event.
  • the Publisher (Controller) will be the one that is publishing the event, and the Subscribers (ItemsCatalog, ItemInfo, BidList, Seller, Buyer) will be the ones that are interested in this event.
  • The Publisher (Controller) needs to keep track of all its subscribers, using a list of Subscribers objects.
  • The Publisher (Controller) needs a function that registers a new Subscriber to the list of subscribers.
  • The Publisher (Controller) needs a function that unregister a Subscriber from the list of subscribers (if the subscriber no longer wishes to be notified about then event, or the subscriber is being destroyed).
  • The Publisher (Controller) needs a function that notifies all subscribers about the event.
  • So in summary, the Publisher (Controller) will have 4 functions:
    • registerSubscriber()
    • unregisterSubscriber()
    • notifySubscribers(), this should be a private function.
    • closeAuction()
  • On the other hand, all other subscribers need the following functions:
    • subscribe(): function that registers with the Publisher (Controller), this may happen on the constructor of the subscriber, and it needs to call the Publisher.registerSubscriber(this).
    • receive(): general function that receives all possible event from the Publisher, and then filter depending on the event type, and call more more specific event handler.
    • handleCloseAuction(): function that handles the CloseAuction event.
    • unsubscribe(): function that unregister with the Publisher, this may happen on the destructor of the subscriber, and it needs to call the Publisher.unregisterSubscriber(this).
  • The Publisher may send an event similar to this:
{
    "type": "CloseAuction",
    "item": {
        "id": "123",
        "name": "Item 1",
        "amount": 100
    }
}
  • Each subscriber needs to implement the handleCloseAuction() function depending on the responsibility of the subscriber, while the three other functions are generic and can be implemented by any subscriber.

References