|
Design Pattern |
|
CoR object structure
Request handlers are extensions of a base
class that maintains a reference to the next
handler in the chain. This handler is known as
the successor. The code of the base class
implementing handleRequest() method is
shown as:
public abstract class BaseHandler {
...
public void
handleRequest(SomeRequestObject sro) {
if(successor != null)
successor.handleRequest(sro);
}
}
Thus, handlers pass the request to the next
handler in the chain by default. A concrete
class of BaseHandler might be implemented
like this:
public class SpamFilter extends BaseHandler {
public void
handleRequest(SomeRequestObject
mailMessage) {
if(isSpam(mailMessage))
{
// If the
message is spam
// take spam-related action. Do not
forward message.
}
else {
// Message is not spam.
super.handleRequest(mailMessage);
// Pass message to next filter in the chain.
}
}
}
|
|
The class SpamFilter handles the request
receiving a new email object. If the message is
spam then the request goes no further;
otherwise, messages are passed to the next
handler. Finally, the last filter in the chain might
store the message after moving through several
filters.
Now we can summarize the characteristics of
the Chain of responsibility design pattern. In
the CoR pattern multiple handlers are able to
handle a request but only one handler actually
handles the request because the requester
knows the reference of only one handler. The
requester also doesn’t know that how many
handlers are able to handle its request as well
as it also doesn’t know which handler handled
its request. The handler could be specified
dynamically. While CoR changing the handlers
list will not affect the requester’s code.
The Command Pattern
In the command pattern the client invokes a
particular module with the help of a
command. In this type of design pattern,
objects are used to represent actions; the
client passes a request that gets distributed
as a command. The command request maps
to particular modules and on behalf of the
command the module get invoked.
The command pattern is different from the
Chain of Responsibility pattern in a way that,
in Chain of Responsibility forwards requests by
each of the classes before finding an object
that can take the responsibility but the
command pattern finds the particular object
according to the command and invokes only
that one.
It encloses a request for a specific action inside
an object and provides it a known public
interface. It makes the client able to make
requests without knowing anything about the
actual action as well as allows us to change
that action without affecting the client program
in any way.
In a simple way we can say that command
pattern is work just like a server that has a
lot of services to be given, and on command,
|
|
Dec
2007 | Java Jazz Up | 66 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
73 ,
74 ,
75 ,
76 ,
77 ,
78 ,
79 ,
80 ,
81 ,
82 , Download PDF |
|
|
|
|
|
|
|
|
|