Design Pattern : Chain of Responsibility

Before getting into the definitions, I can tell you one simple example that can give you enough understanding on Chain of Responsibility pattern, For example, Let’s take the below problem,

An order entry will be created in the database once user adds line-items and also a mail will be sent to the customer and a mail to admin dept. of the store. Once the order is received by admin. dept. the order detail will be sent for approval to Store Supervisor. But there are certain products jewels, ornaments can be only approved by Store Head, whereas, the products like Shoes, Pencils can be approved by Section Head himself.

This is a simple and a well known requirement. If you look details of the requirement, following are behaviors we need to keep in our mind :-

  • if order needs to be created you might need to check the inventory for all the line items
  • for each line item,
    • we need to make sure whether that item can be ordered in first hand,
    • if it can be ordered, then we need to inform the appropriate approving authorities
    • and wait until we receive approvals from them
      • if we don’t receive approvals with in certain time,
        • send a mail to customer that order is in hold and not yet processed
        • and also send a mail to admin. dept. that an order is moved into hold because of no feed back (or response from so and so approving authority)
  • upon the successful creation of order we should send the mail to customer as well as admin. dept that the order is successfully created now it can be then processed for shipment (or whatsoever)

Looks like we went into very much details!. But, anyway, we are not going to implement the entire requirement here. But we will try to develop the code/method signatures that can obey Chain of Responsibility rules.

Let’s talk some theory here, because we need to understand little bit before we proceed to solve the problem. In the pattern terms, here is what have,

  • Context is something that defines a platform for a request. So, as per our example
  • ConcreteHandler handles the request and if it cannot satisfy the request it passes the request to its Successor or Predecessor (A,B,C,D)

( continues .. )

General

Comments (0)

Permalink

The Seek or Show Paradigm

Seek or Show I just found something very interesting about two design paradigms that are basically for handling large amount of data. Have a look at this weblog.

The Seek or Show paradigm has 7 common patterns. In a nutshell, the Seek pattern is something that you’ll have the user to ask what they want and Show paradigm is something you display everything that you get out of the first pattern and let the user to explore what ever they want.

A very interesting read for today. Don’t miss it.

Blogging
General
Programming

Comments (0)

Permalink

No time !!!

Oh !! It’s been a long time I wrote something on my blog.  It feels like I am really living in end of the world. I will make sure that I will at least write a post in a day. I know, I have so many things to share but writing is such a hell it needs more attention. Hmm .. let’s see how 2008 takes me

Blogging
General

Comments (0)

Permalink

W3C Proposal for Cross Domain Scripting

Cross domain scripting was an nightmare until now. W3C has a new proposal which defines access control primitives could be used by cross domain scripts.

One way of doing it is setting up an HTTP header,

Access-Control: allow <*.vadivelk.net> exclude <*.global.vadivelk.net>

Or if you want to process an XML,

<?access-control allow=”allow.example.org” deny=”deny.example.org”?>

Looks great!!

Javascript
Programming

Comments (1)

Permalink

Tip: Showing a Software Demo

I was going through Joel’s one of an post, that explains about his experience in software demos that he did recently on various cities. His recent blog post describes about how you have to show demo of your application:

As you go along, be sure to accidentally bump into all the nice little ?fit and finish? features of your product. Oh look, that column is halfway off screen. No problem. I?ll just drag it over. (?Wha!? the audience gasps, ?you dragged a column in HTML??) Oh, look, this feature is supposed to be done by next Tuesday. I?ll type ?next tuesday? in the due date box. (?OMG!? they squeal. You typed ?next tuesday? and it was replaced with ?11/20/2007?). Those nice little touches you put so much hard work into are not the meat of the demo, so don?t talk about them, just act nonchalant. What, doesn?t every web app let you resize and drag columns?

I completely agree with Joel. All the tiny features you built with hard work that is not a normal thing to do with the technologies you used will be a great thing to explain (eg., dragging a column in HTML page as he mentioned) but not worth to explain in a demo as a “center-of-attraction”. As he mentions, it should go along nicely with your main talks. Don’t miss to read.

Blogging
General

Comments (0)

Permalink

Returning Anonymous Methods from a Function

Today, I was flabbergasted after tussling with a problem — I realized there is something that I don’t know yet or I am too tired to write a good piece of code.

Well, here is the problem. I have to return multiple values from a function, meaning, a function that may return mixed bag of booleans, ints, chars etc., but I caller method does not know what type it returns otherwise it of course knows it is an System.Object. You might think, a class or struct  can be used as a return type for those functions that would be returning multiple values. But, I really don’t want to create a class or struct and use it as a return type as the scenario is not worth for to create a class or struct. The data is not at all processed by any means or the data is not related to any of the Business requirements.

So, Basically — “A function that returns an System.Object type, and the returned object might have a list of various types of variables and also tells the caller how to retrieve the values (a type inferred key and value pair of objects)“.

How to achieve this?

There comes thy “Anonymous Methods” for rescue. Look at the below code,

public object getValues()
{
    return new anonymous object[] { secondVal = 12, firstVal = “My dear lisa” };
}

It just returns an object with the type inferred automatically. One caveat is, you will have to cast the members if you want to use it directly eg., string s = getValues().secondVal won’t work. The code should be self-explanatory.

.NET
C#

Comments (0)

Permalink

Chai Wallahs and Customer Relations

An excerpt from BenPoint.com’s post:

A Chai Wallah is a person who sells tea on the street corner in India pouring it in a tall narrow stream from pot to clay cup with great aplomb. India is the world mecca of small business and there the business owner whether selling chai, fruit, fish or clothing, often does it with evident glee. That is because (there especially) if you don’t have great customer relations someone else will gladly take your place. But aside from that, there really is something universally exciting about selling a product for significantly more than the cost of the raw expenses and having happy participants on both sides of the transaction. There is also an accompanying joy in displaying your craft to your customers.

Blogging
General

Comments (0)

Permalink

Exceptions: Effects of Exceptions in Functions

I have been working on an article that elaborates about Exception management. I just came across certain new topics which took my interest in reading through about it elaborately. Below are those,

What is Abrahams Guarantees?

In C++, a function implementation should guarantee any of the below guarantee techniques and this is what called as Abrahams guarantees:

  • Basic Guarantee. If an exception is thrown, no resources are leaked and objects remain in a destructible and usable?but not necessarily predictable?state.
  • Strong Guarantee. If an exception is thrown, the program state remains unchanged.
  • Nothrow Guarantee. The function will not emit an exception under any circumstances.

What is Scope-Guard?

Very interesting topic. Basically, these comes in when you want to write a transactional function. What you will do if your function raises exception in middle of it and gives no guarantees that it is predictable. There is a way to solve this problem. Read through this article.

General
Programming

Comments (0)

Permalink

FreeRice.com

120_240_Vertical

I think, I should definitely put it over here. You learn, You donate ..

General

Comments (0)

Permalink

Prototype: HTML Templates

In the course of time, I have become a big fan of Prototype JavaScript library. It really cool and very light-weighted JavaScript component out there. One of its powerful templating feature I just came across is worth to mention here,

If you want to build a table (or a div/panel) through making an Ajax request (or may be generating a HTML through JavaScript) it is always a big task to assign the values coming from the server request. Prototype has its easy way to do it. That’s called Templates

You define a template like this, enclosed with the HTML element Id’s that needs to get the values:

var rowTemplate = new Template (
  ‘<tr><td>#{id} #{name}</td></tr>’ +
  ‘<tr><td>#{id} #{price}</td></tr>’
);

The above code creates a template object with the tokens name, price etc., which are the HTML element ID’s. Now, you can use this template everywhere to generate as many as rows you want to have. I can just update the values from the server in the following manner:

$(‘productTable’).update(
 rowTemplate.evaluate({
 id: productId,
 name: productName,
 price: productPrice
}));

Very cool right!! :)

Source: http://thinkweb2.com/projects/prototype-checklist/

Javascript

Comments (0)

Permalink