• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Choosing better class to use, without if

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic