Tuesday, August 26, 2008

Chain of Responsibility Pattern

The classic Chain of Responsibility(CoR) pattern defined by Gang of Four(GoF) in Design Patterns:

"Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it."


Following Figure illustrates the class diagram.

Figure 1. CoR class diagram

A typical object structure might look like following Figure .

Figure 2. CoR object structure

From the above illustrations, we can summarize that:

  • Multiple handlers may be able to handle a request
  • Only one handler actually handles the request
  • The requester knows only a reference to one handler
  • The requester doesn't know how many handlers are able to handle its request
  • The requester doesn't know which handler handled its request
  • The requester doesn't have any control over the handlers
  • The handlers could be specified dynamically
  • Changing the handlers list will not affect the requester's code
Uses of CoR:
1. Microsoft Windows global hook framework
2. Java servlet filter framework


Details are explained by Michael Xinsheng Huang, JavaWorld.com, 08/16/04

Monday, August 18, 2008

Difference between Association, Aggregation and composition

Difference between Association, Aggregation and composition:

Association:

1. A relationship between 2 objects that is not whole-part i.e.
A contains a reference of B and uses B through the reference
2. Generally implemented as a public property of A,
where B may be set in to A by an external party (say Spring)

Dependency:

1. A week association, where A doesnt contain a reference of B but uses B
2. Generally implemented as method parameter in A

Composition:

1. A strong whole-part relationship, i.e. B (child) has no existence
outside A (parent)
2. A creates and contains B but B can not contain A
2. B must not be visible outside of A since if A dies, B must die

Aggregation:

1. A week whole-part relationship, i.e. B (child) may exist exist
outside A (parent)
2. A contains B but B can not contain A
3. B must be visible outside of A since B may still exist when A dies
4. Generally 1-many relationship

With Real Life Examples:

Accosiation:
1. Create a folder called "Links"
2. create a shortcut inside this folder and link it to www.yahoo.com
3. create another shortcut instide this folder and link it to www.google.com
4. Ask your friend to do the same on another machine using same links
(www.yahoo.com and www.google.com)
5. Delete the "Links" folder, and open your browser to check if
www.yahoo.com and www.google.com still exist or not ;)

Briefly, Association is a relationship where all the objects have different lifecycles.
There is no owner.



Composition:
1. Open a new Document name it as test.txt
2. Write this sentence in iside this document "This is a composition".
3. Save the document.
4. Now, delete this document.

This is what is called composition, you can't move the sentence "This is a
composition" from the document because its lifecycle is linked to the
parent (the document)

Aggregation:
1. Create a file called file.txt
2. make a simple application to open the file.txt (rw), but don't program
it close the connection.
3. Run an instance of this application (it should work ok and can open the file for rw)
4. Keep the first instance, and run another instance of this application
(In theory it should complain that it can't open the file in rw mode
because it is already used by other application).
5. Close the 2 instances (make sure you close the connection).

From the above application, we knew that the Application and the file has a
separate lifecycles, however this file can be opened only by one
application simuletanously (there is only one parent at the same time,
however, this parent can move the child to another parent or can make
it orphan).


View Details: http://www.artima.com/forums/flat.jsp?forum=17&thread=24715