General Design Question

olsonpm

Well-known member
Joined
Aug 24, 2010
Messages
46
Programming Experience
Beginner
I was wondering what the correct solution would be to this simple problem.

I am receiving requests, all of base class 'clsRequest'. Each request is simply a data holder of sorts, with zero logic. However, each class needs to be handled differently. In order to keep the separation of data and logic, I would need to create a handler class corresponding with each type of request. Further, I don't know how I would call the handlers without creating a hashtable linking each request to its correct handler. All this could be avoided if I simply put the handler logic inside the request class itself - voiding it of the data/logic separation. I'm assuming it is a simple solution that I am missing.

thank you for your help,
Phil
 
One potential course of action is to create a factory class. Let's assume that you have base class Request and derived classes RequestX, RequestY and RequestZ. You would then create a base class RequestHandler and derived classes RequestXHandler, RequestYHandler and RequestZHandler. RequestHandler would be declared MustInherit and declare various members MustOverride. Each derived class would then provide an implementation for those members. You would then add a factory method to the RequestHandler class like so:
VB.NET:
Public Shared Function Create(ByVal request As Request) As RequestHandler
    Dim handler As RequestHandler

    If TypeOf request Is RequestX Then
        handler = New ReuqestXHandler
    ElseIf TypeOf request Is RequestY Then
        handler = New RequestYHandler
    ElseIf TypeOf request Is RequestZ Then
        handler = New RequestZHandler
    End If

    Return handler
End Function
You can then create and use a handler for a request something like this:
VB.NET:
Dim handler As RequestHandler = RequestHandler.Create(request)

handler.DoSomething(request)
 
Thank you. For some reason I thought I could stray from a larger case statement (or a hash table which does the same thing). However, if this is the correct way to go about it, then I have no problems. I just like to know the 'correct' way to go about things when I'm not comfortable with my own solution.
 
Back
Top