Monday, December 30, 2013

Object oriented design for a library to store books, movie dvds

Question: Design a library shelf which can store books or digital media also like CD/DVD. The design should be accommodate features so that  responding to user queries would be easy and the library has the ability to store new content types in a easy way.

Lets attempt to describe the library we want to design. Our library can store books, movie dvds to start with. Books and DVDs are different content types. Each content type can have different genres. For e.g in books we have fiction, romance, non fiction etc. Similarly in DVDS we have action, drama, documentary etc. Lets assume for simplicity that the books and DVDs the library gets are already tagged with that genre information.

Now i can visualize the library as one that contains different sections(one for content type). Each section has different genres, and each genre has a stand where the items(books, dvds) are stored.

Typical user queries would be 
 * search books by author xxx
 * search books with title containing certain words 
 * search movies with title containing certain words
 * search movies directed by yyy

User searches for the contents by specifying details about what is it he is looking for. Like which genre, which content type, author details, or any keywords to search in title etc. All these details are specified via SearchFilter. Based on the above description, below is the design for the library




This design extends itself to be able to support new content types, new genre types easily
Given that we have been asked to design a library to just store the items above design should work.
(If you want to add the feature of renting the items, we should store information about how many items we have, available/lent status for each item etc. The design for library where users can borrow books is discussed in Object oriented design for a library to borrow and renew books)


Friday, December 27, 2013

Object oriented design for a Restaurant

New Post I have done another OO-design for slightly different restaurant model here. You might check out that independent of this post.

Let us do the OO-design for a Restaurant today. Let me first describe how the restaurant we want to model works. These are the different actors in the model and i have listed the different actions against each actor

* Customer
  • Selects the dish from the menu and call upon a waiter
  • Places the order
  • Enjoys his meal once the dish is served on his plate
  • Asks for the bill
  • Pays for the services
* Waiter
  • Responds to the customers calls on the tables he is waiting
  • Takes the customer's order
  • Places the order in the pending order queue
  • Waits for the order ready notifications
  • Once notification is received, collects the dish and serves the dish to the corresponding customer
  • Receives the bill request from customer
  • Asks the Cashier to prepare the bill
  • Gives the bill to the customer and accepts the payment
* Cashier
  • Accepts the prepare bill request from the waiter for the given order details
  • Prepares the bills and hands it over to the waiter
  • Accepts the cash from the waiter towards the order
* Chef
  • Gets the next order from the pending order queue
  • Prepares the dish and push the order to finished order queue
  • Sends a notification that the order is ready
Please take a look at the Coffee Shop Design  as the restaurant design we are attempting here is derived from that. The differences between a Coffee shop and Restaurant are
  1. In  a coffee shop there is no waiter. Customer selects the dish and directly places the order with the cashier. In a restaurant there is the waiter who handles all the interaction with the customer. 
  2. In a coffee shop, customer upon getting notified that the order is ready, he collects the coffee himself. He waits for the order ready notification. In a restaurant, waiter waits for the order ready notification and once ready serves the dishes to the customer.

Object oriented design for a restaurant
OO design for a restaurant


Above is the class diagram for the restaurant we described above. To keep the matter simple i have assumed certain things like customer not changing his order once placed, customer not cancelling the order, unlimited queue size for pending queue and finished queue. However accommodating these exceptions is relatively easy. Just like the Coffee shop design here also there is asynchronous nature to the order processing which can neatly handled via messaging.

OO design for a coffee shop



Let us do the design for a coffee shop today. Let me start with the description of how the coffee shop we want to design works first. There are three different actors in our scenario and i have listed the different actions they do also below

* Customer
  - Pays the cash to the cashier and places his order, get a token number back
  - Waits for the intimation that order for his token is ready
  - Upon intimation/notification he collects the coffee and enjoys his drink
  ( Assumption:  Customer waits till the coffee is done, he wont timeout and cancel the order. Customer always likes the drink served. Exceptions like he not liking his coffee, he getting wrong coffee are not considered to keep the design simple.)

* Cashier
  - Takes an order and payment from the customer
  - Upon payment, creates an order and places it into the order queue
  - Intimates the customer that he has to wait for his token and gives him his token
  ( Assumption: Token returned to the customer is the order id. Order queue is unlimited. With a simple modification, we can design for a limited queue size)

* Barista
 - Gets the next order from the queue
 - Prepares the coffee
 - Places the coffee in the completed order queue
 - Places a notification that order for token is ready





object oriented design for coffee shop
Coffee Shop Class diagram
Above is the class diagram for the above description of the Coffee shop. Notice that the order processing is done in an asynchronous way. Customer places the order and goes to some table and reads newspaper/checks his mails etc. Once the order is done, he is intimated about the order ready via an announcement (or even smart way is sending a sms, as you have taken his contact number). From an implementation perspective these asynchronous communication can be handled using JMS

Please take a look Restaurant Design once you are done here. The restaurant is slightly different from the coffee shop in which it has a waiter to handle all your needs. Please leave your comments.

Distributed Transactions and 2 Phase commit protocol

Distributed Transactions and 2 Phase commit protocol

To start with let us a have a very very simple definition for a distributed system. A distributed system would be one where the systems's resources are located in different locations (for large distributed systems different  applies at geographical scales. For. E.g amazon has its data centers, web servers are spawned across multiple geographic locations.)

Let us also define in simple terms a transaction. Transaction is a set of operations on a recoverable data item ( i.e. data item that can be taken back to its previous state should the operations fail. Typical example of recoverable data item is database records)

Distributed transaction is a transaction we intend to perform on a distributed system. Given the nature of the system the operations in the transaction might have to be executed on the resources spawned at different locations. The basic requirement of any transaction is ACID (Atomicity, Consistency, Isolation, Durability) and these same apply to distributed transactions as well.

To achieve atomic commit in a distributed transaction scenario, one of the popular protocol is the 2-phase commit protocol.

Assumptions of the 2 Phase commit protocol:

* A master/coordinator host is selected which coordinates the transaction
* All other nodes are called cohorts/participants
* Each of these nodes have stable storage and can perform write ahead logging of the operations

The 2 phase commit is named so as it has 2 phases called voting phase, commit phase. We will see how the protocol works.

1) The master coordinates the transaction by invoking the operations in the transaction on the appropriate cohort
2) Once the final operation of the transaction is executed, the master starts the voting phase

Voting phase:
3) Master sends a query to commit message to all cohorts
4) Upon receipt of the message each cohort completes their set of operations and write the details(undo and redo information in their logs)
5) Each cohort replies back saying Yes(for commit) or No(for rollback) based on its situation.

Commit phase:
 * If master gets yes from all cohorts, he sends a commit message
 * Each cohorts commits his side of changes, releases locks on resources and write commit info to log
 * Sends Ack to master who then releases the resources and writes commit info to log

Rollback phase:
 * If master gets at least one No from any cohort, he sends a rollback message
 * Each cohorts rollback his side of changes using undo log, releases locks on resources
 * Sends Ack to master who then undoes his side of things and releases the resources

Cons:
 If the master fails, some of the cohorts can not complete their transactions and get blocked on the master to send commit/rollback message.