Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Suppose I am to design an application which will handle all imcoming mails. There are various mail types like spam, thanks mail, order, complaint etc.
I have a seperate module which can tell me the type of mail received.
Using this module I will build my application in C++.

I designed a module using factory method where in the main() function the mail differntiation module will be called and appropriate class for a mail type will be instantiated using base class static function for factory method.

But some people suggest me to use chain of responsibility for this?
Now the question is why should I use chain of responsibility for this when I have a seperate module to find the type of mail arrived?
Using the seprate module I have I can directly find the type of mail and instanciate the appropriate mail handler class.
Then why to use chain of responsibility?
Posted
Updated 10-Nov-11 23:18pm
v3
Comments
Sergey Alexandrovich Kryukov 11-Nov-11 3:49am    
Just by the title: I cannot be a valid question. This is the same as the question "what's the difference between a class and System.Console.WriteLine?". :-)
--SA
rupeshkp728 11-Nov-11 5:18am    
I gues I have changed it to something more meaningful.

1 solution

Comparison of "Factory Method" and "Chain of Responsibility" is not very proper because of below reason.

"Factory Method" is a Creational Pattern and "Chain of Responsibility" is a Behavioral Pattern.

"Factory Method"
http://www.dofactory.com/Patterns/PatternFactory.aspx

If you read above article you will understand in "Factory Method" final outcome is "Product"(Interface) instantiated by "ConcreteProduct". This "Product" is created after specific decision made by "ConcreteCreator".

For Example - When you Order a Pizza(Product) to "PizzaStore"(ConcreteCreator) it can be "ThickCrustDough"(ConcreteProduct) ;P or "PlumTomatoSauce"(ConcreteProduct) ;P or something else.

"PizzaStore"(ConcreteCreator) will instantiate Pizza(Product) by either of "ConcreteProduct" based on your Order. One Pizza will be made at a time.(But you can order as many as you want :))


"Chain of Responsibility"
http://www.dofactory.com/Patterns/PatternChain.aspx

From the above article you will understand their is one "Handler" and their can be multiple "ConcreteHandler" inherited by it.

In one common Work/Goal, "ConcreteHandler" handles the responsibilities for the in scope tasks and then delegates remaining to its Successor(which is again a different "ConcreteHandler"). So eventually one common Work/Goal gets complete by contribution of multiple "ConcreteHandler".

Now considering your requirement. In case if you are expecting Bulk of emails at a time, then Chain of Responsibility is the valid choice.

You can have one "Handler"(Interface) which will be implemented in multiple "ConcreteHandler". Every "ConcreteHandler" will be responsible for specific Email Type and will delegate responsibility to its Successor for different email type.

When your application will traverse through these Emails. "ConcreteHandler" will handle the emails of its type and will delegate the responsibility to next Successor("ConcreteHandler") when their is a different type of email.

Hope it helps. :)
 
Share this answer
 
v2
Comments
rupeshkp728 11-Nov-11 9:13am    
Thanks raiskazi for the reply.
But still my question remains unanswered.
Since using the seprate module I have I can directly find the type of mail and instanciate the appropriate mail handler class.
Then why to use chain of responsibility?
RaisKazi 11-Nov-11 9:58am    
Their are two scenarios.
1) If you are expecting a single email once a while then you may keep your implementation as it is.
2) But as I mentioned in my Answer, if you are expecting Bulk of emails at the same time, then you should use "Chain of Responsibility", Because this design pattern has a concept of Successor. Every "ConcreteHandler" will be responsible for particular type of email, and it will be also responsible for delegating responsibility to other "ConcreteHandler" when the email is of other type.

It should clear your doubts if you go through the links I provided.

You may refer below article. Its code is in C# but I don't think syntax should be concern as long as you get the concept. In this article example is of PhoneCall handling which is closer to your Email Handling.

The GOF "Chain of Responsibility" Design Pattern

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900