The Factory pattern is much like dynamic polymorphism where the base
class is an abstract class and the instantiation is done of the deriving
classes. The Factory pattern is a Creational pattern.
The Chain of responsibility pattern is much like the TCP/IP networking
where the receiver throws packets with specific headers across a domain
until the machine with the specific address catches it. This pattern
delegates responsibility and the objects are chained. Hence, when a task
needs to be implemented, the chain of resp. is traversed until an
object handles it. The Chain of responsibility pattern is a behavioral
pattern.
I hope the above can help you take a decision on which to use and when.
The following is an extract of code, in C#, for each of the patterns.
The Factory pattern
-------------------
using System;
using System.Collections;
abstract class Product
{
}
class ConcreteProductA : Product
{
}
class ConcreteProductB : Product
{
}
abstract class Creator
{
abstract public Product FactoryMethod();
}
class ConcreteCreatorA : Creator
{
override public Product FactoryMethod()
{
return new ConcreteProductA();
} }
The Chain of responsibility pattern
-----------------------------------
using System;
abstract class Handler
{
// Fields
protected Handler successor;
// Methods
public void SetSuccessor( Handler successor )
{
this.successor = successor;
}
abstract public void HandleRequest( int request );
}
// "ConcreteHandler1"
class ConcreteHandler1 : Handler
{
// Methods
override public void HandleRequest( int request )
{
if( request >= 0 && request < 10 )
Console.WriteLine("{0} handled request {1}",
this, request );
else
if( successor != null )
successor.HandleRequest( request );
}
}
// "ConcreteHandler2"
class ConcreteHandler2 : Handler
{
// Methods
override public void HandleRequest( int request )
{
if( request >= 10 && request < 20 )
Console.WriteLine("{0} handled request {1}",
this, request );
else
if( successor != null )
successor.HandleRequest( request );
}
}
// "ConcreteHandler3"
class ConcreteHandler3 : Handler
{
// Methods
override public void HandleRequest( int request )
{
if( request >= 20 && request < 30 )
Console.WriteLine("{0} handled request {1}",
this, request );
else
if( successor != null )
successor.HandleRequest( request );
}
}
// "Request"
class Request
{
// Fields
private int iRequestType;
private string strRequestParameters;
// Constructors
public Request(int requestType, string requestParameters)
{
iRequestType = requestType;
strRequestParameters = requestParameters;
}
// Properties
public int RequestType
{
get{ return iRequestType; }
set{iRequestType = value; }
}
}
/// <summary>
/// Client test
/// </summary>
public class Client
{
public static void Main( string[] args )
{
// Setup Chain of Responsibility
Handler h1 = new ConcreteHandler1();
Handler h2 = new ConcreteHandler2();
Handler h3 = new ConcreteHandler3();
h1.SetSuccessor(h2);
h2.SetSuccessor(h3);
// Generate and process request
int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
foreach( int request in requests )
h1.HandleRequest( request );
}
}
Code Source:dofactory.com
-------------------------
with regards,
J.V.Ravichandran
-
http://www.geocities.com/
jvravichandran
-
http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
-
http://www.southasianoutlook.com
-
http://www.MSDNAA.Net
-
http://www.csharphelp.com
-
http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at
http://www.Google.com