Sunday, December 6, 2015

Restaurent design another round.

There is an earlier post related to Restaurant here. I wanted to attempt this design challenge again now with some more complexity.

I will describe the challenge here first followed by the assumptions.




We are going to discuss about a restaurant with
  1. some limited(m>0) tables
  2. one chef
  3. one waiter
  4. one cashier
  5. some chairs for customers to wait when the tables are busy.

Assumptions being made are all payments are made electronically.
  1. The restaurant does not allow table reservation policy. You come and if there is a table sit and dine else wait for a table to free. 
  2. All orders are going to be accepted i.e. there is no way the chef would say ingredients for menu item 29 are over and hence cant accept the order for that item. Poor chef :( 
  3. Any new items ordered by the customer are treated as a different coming from same customer.
  4. The waiter gets the name of the customer as part of welcoming him and will use it along with table number to identify the customer

Let us try to model this restaurant using Object oriented techniques

The different players are
  • Customer
  • Waiter
  • Chef
  • Cashier
Below use case diagram depicts the typical interactions in the restaurant model. I developed the below use case diagram using an open source uml tool called ArgoUML. You can also try this tool. It is a good one.

http://thought-works.blogspot.in
























Our next milestone is to design classes to support the above use cases. Without much difficulty we can arrive the need for classes Customer, Cashier, Chef and Waiter and their operations(Actors in the use cases have their classes).

In the use case model we are talking about adding order to queue, adding cheque request to queue. These queue classes are also part of the class diagram accordingly. I did not mention the operations and attributes of *Queue classes as they are obvious.

Restaurant's operations typically are table centric(what i mean is they typically talk in terms like a  barbecued chicken for table no 1, prepare the cheque for table no 5 etc). In this system we just need to store the details like customer name (this also is not mandatory at least would be helpful to address him in the bill/custom feedback form).

But any point of time we should have information of what orders are coming from which table, which customer is seated at which table. (Putting the table id into the customer object is not the right thing to track the customer table association as table id does not naturally look to be an attribute of a customer object).

Similarly we need to know which orders are coming from which table( it is actually customers seated at that table). Here also storing table id in order to track the association does not seem correct.

We use two maps to track the association between customers and tables, tables and orders.


Below is how the class diagram looks for the Restaurant system now





Cartoon courtesy: here

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.

Sunday, November 11, 2012

Object oriented design for an online auction site

Object oriented design for an online auction site

Typical set of operations on the online auction site from the user perspective are

  • Users should register themselves and sign in to participate or start auctions in the online auction site. 
  • A user can create a new item category to be auctioned in the site or else he can search ongoing auctions on the item and participate in one or more of these auctions simultaneously. 
  • Upon participating in auction he/she can place the bids.
  • He/she can come out of the auction at any time.
  • If he wins the auction by placing the highest bid, he has to pay the bid price to the payment sub system
  • If he is the owner of the auction which was complete with a winner, he should send the items to the delivery sub system.
Based on the above description the main components of the system would be
User -> can act as bidder/seller
Item -> Things which are sold and bought in the system
Auction -> Process in which seller wants to sell the item
Bid -> users quotes for the item being sold
Auctioneer -> one who oversees the process of start, stop and conducts the whole process
PaymentHandler -> component which accepts the payments from winning bidder and transfers the payment to the seller


Based on the above description this is the class diagram of the system:



Saturday, November 10, 2012

Object oriented design for a library to borrow and renew books

Object oriented design for a library to borrow and renew books

A typical library has a set of books which the users can borrow for a certain period of time and return back. Users may choose to renew the return date if they feel they need to more time to read the book. We are trying here to design an online library.

The typical user actions with this online library would be

  •  sign in/register
  •  search books
  •  borrow books
  •  renew the return date for any of the books borrowed
  •  view his profile(check his details, list of books he borrowed )

The online library should support all the above actions. It must keep track of the different books in the library currently available for users to borrow and also the books already borrowed bu users. Put it simply the inventory should be managed.

Going through the above description we can think of these components in the system:

  1. User
  2. Book
  3. BorrowTxn -> record for the event for book b is borrowed by user u
  4. InventoryManager -> manages the books in the library. Adds new books, remove books and respond to book search requests
  5. LibraryManager-> Allows user interaction with the library like logging in, borrowing, renewal and return.

Below is the class diagram which depicts how these components inter-operate 



The Library Manager(LibManager) interacts with the inventory manager for responding to user search queries. Each borrow operation would result in the creation of a BorrowTxn which encapsulates the book details, borrowing user details and the return date. The inventory state is updated accordingly(i.e. the borrowed book is no longer available for borrowing). Similarly when a book is returned the inventory state is updated.

When the book return date is renewed the inventory state is not changed but the borrow transaction is updated accordingly.