aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Choosing better class to use, without if Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Choosing better class to use, without if" Watch "Choosing better class to use, without if" New topic
Author

Choosing better class to use, without if

Jean Marcel Bacan
Greenhorn

Joined: Dec 04, 2012
Posts: 15
Hi guys.

I've a situation and I would like to read some opinion from the experience people.

I'm trying to decide which approach to use, if possible, without using IF.

I've a Servlet that receive messages from many servers, each server has a different pattern of message. e. g. Server 1, pattern of msg 1. Server 2, pattern of msg 2.
For each pattern I've a Decode class, e. g. Pattern 1: Pat1Decode, Pattern 2: Pat2Decode and each message has the server name on it.

How should I implement this on my Servlet? Using a Factory Pattern?
I would like to remove the logic from the Servlet and a clean solution without use:

If message is from server 1: Use Pat1Decode
If message is from server 2: Use Pat2Decode

Thanks

OCPJP 6
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2451
    
  28

Do you know without decoding the message if it's from server 1 or server 2? If the source is known before decoding, I would use Strategy pattern. If you need to decode to know the source, I would use the Chain Of Responsibility pattern.

Edit: And welcome to coderanch!
Jean Marcel Bacan
Greenhorn

Joined: Dec 04, 2012
Posts: 15
Yes, the String message came with title in the first line Ex:


Server 1
tststststststs
tststststs
tststst


I've read the Strategy pattern in the Head First Design Pattern, looks like I've to read again hehehehe.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2451
    
  28


Strategy pattern sounds more sophisticated than it is. Basically, you declare an interface that has one parse method. For each server, you implement a class that implements this interface. During init time, create a Map that stores instance of the interface keyed by the server name. When you need to parse the message, you retreive the instance of parser using the server name, and use it.

The advantage of this pattern is that you can always extend it to add more implementations of the Parser. Also, if you at some point, 2 differrent servers need the same Parser, you can just initialize the Map that way.
Jean Marcel Bacan
Greenhorn

Joined: Dec 04, 2012
Posts: 15
I liked the idea of the map, sounds like I already have a Strategy.

I created an interface with the getDecodedMessage(), and a concrete class that decode it.

Now I'm going to use the Map to decide which instance I should use in my Servlet.

Thanks
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

1. Why does it matter which server it comes from? I would design this such that I am only concerned with the format of the message coming in. If there is a need to identify the source, then maybe include that information as part of the incoming message.

2. If there are a known number of message formats I have to deal with, I would just use a different dispatch method for each message format. I'd probably not think about the Strategy pattern until I see a need for it. It would not be a big effort to refactor dispatch methods into the Strategy pattern anyway.

3. If you're looking for a pattern name, Front Controller comes to my mind before Strategy, since you did say that you are using a Servlet to receive requests.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Choosing better class to use, without if