Sunday, July 24, 2016

Chain of responsibility pattern

Chain of Responsibility

If you have a scenario where you need to chain multiple handlers to handle an incoming request or command, you better use Chain Of Responsibility.

Workflow examples:
1. Lets say we have an organization where Team members when apply for leave, the request goes to the Team Leader. Team leader can approve all the leave request of less than 10 days. If the leave request is of more than 10 days then the request will be passed on to the Project Leader. Project leader is able to approve leaves of upto 20 days. If the leave is applied for more than 20 days then this requests will be passed to the HR. HR can approve upto 30 days of leave. If the leave is of more than 30 days then the leave application cannot be approved by the system and it needs a manual process for approval.

2. A typical example is your girlfriend requesting you something – If she is requesting/commanding you something like “Do you want to come with me for my best friend’s Bachelorette party?”, you will handle it directly. But if she is requesting/commanding you some thing like “Buy me a Porsche”, you say “Sorry Honey, I don’t have the money. Better you ask your dad for this, I’ll call him for you.” –i.e, you pass the request to the next handler, in this case your girl friend’s father. To sum up, in the above example, your girl friend is the client who is making the request, and you and your future father-in-law are handlers/approvers who handle/approve her requests. If you cannot handle it, you pass that responsibility to the next handler/approver in the chain.

Components:

1. Client : The class that request for something. In our case, its an employee, who will be requesting for a leave.
2. Request Handling Class : Could be an interface or an abstract class(preferably abstract class). Will discuss why to use abstract class. This class will be have the abstract method to handle the requests it receives, and a concrete method, to set the next request handler, in case the current handler cannot handle the request.
3. Concrete Request Handlers : These are the actual request handlers implementing the request handling class method, to manage the request which they receive, in their own ways. So let’s get started.




Sunday, May 29, 2016

POUCH DB

PouchDB is an open-source JavaScript database inspired by Apache CouchDB that is designed to run well within the browser.
PouchDB was created to help web developers build applications that work as well offline as they do online.
It enables applications to store data locally while offline, then synchronize it with CouchDB and compatible servers when the application is back online, keeping the user's data in sync no matter where they next login.



Reference: https://pouchdb.com/learn.html