Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Choosing better class to use, without if

 
Jean Marcel Bacan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7328
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic