none
Using the template and strategy pattern. RRS feed

  • Question

  • I'm building a service that reads incoming files, these files are Dicom files (medical image format standard used in medical field)

    These files have fields in them that contain meta data, like patient name, ssn, weight, etc.

    Certain files will have specific methods called on them to clean and parse the data to prepare it for the db depending on the type of Dicom file it is and what the meta data says.

    I was looking for good patterns to use and I ran across the template pattern and the strategy pattern. The template pattern looked good because I would like to defer certain steps to other methods or other subclasses depending on what the meta data says.

    I was looking for a solution to the stage of the application just before the use of the template, the part where I read meta data and detect what class I will use.

    Can anyone give me any advice on this or any other good patterns to use?


    chuckdawit
    Tuesday, August 17, 2010 5:04 PM

Answers

  • Yes, that is a great way to do it.  I would also have each returned strategy implement the same interface.

    Then you could do:

    public class StrategyFactory()
    {
       public static IStrategy GetStrategy(IMyFileObject theFile)
    {
    // switch.... determine strategy
    
    return Strategy;
    }
    }
    
    
    
    

    Then your main code does this:

    IStrategy myStrategy = StrategyFactory.GetStrategy(myFile);
    myStrategy.Process(); //or whatever
    
    
    The logic for Process is in the class returned, it can be the same or different routines, it doesn't matter. A new strategy based on the file properties is easily changed or added.
    • Marked as answer by witdaj Tuesday, August 17, 2010 9:50 PM
    Tuesday, August 17, 2010 9:12 PM

All replies

  • Im a big fan of strategy. In Strategy you do not tie the implementation to the solution, it is deffered.

    For your case, the type of strategy is determined by the type of file, but your main processing engine shouldn't care. It gets the file, passes the file to a strategy decider, a strategy comes back, processing is done using that strategy.  New file needs a new strategy? Doesn't matter. Simply write your new strategy, have the decider return the new strategy based onthe new file, and the main processing does not need to be changed.

    Tuesday, August 17, 2010 7:33 PM
  • thanks  for the advise.

    what about a pattern to use for the strategy decider?

    Right now the only solution I have is to use a switch statement.

    Is there any type of pattern to use here?


    chuckdawit
    Tuesday, August 17, 2010 8:12 PM
  • Sounds fine to me! Start small, take it for a spin, then if you notice more encapsulation is needed, you could use a factory to create strategies based onthe file you get.

    Tuesday, August 17, 2010 8:22 PM
  • Once again, thanks.

    One more question though.

    In the strategy pattern the strategy class looks something like below. what if I have many commonalities between files that have the same methods. I don't want to rewrite teh same methods over and over again in each strategy, can I use a concrete base class instead and of a abstract class here so that I can implament methods in the base class? That's why I was looking at the template pattern instead of the strategy pattern. Any thoughts?

     

     

    abstract
    
     class
    
     SortStrategy
    
     {
    
     public
    
     abstract
    
     void
    
     Sort(List<string
    
    > list);
    
     }
    

     


    chuckdawit
    Tuesday, August 17, 2010 8:27 PM
  • Yes, that is a great way to do it.  I would also have each returned strategy implement the same interface.

    Then you could do:

    public class StrategyFactory()
    {
       public static IStrategy GetStrategy(IMyFileObject theFile)
    {
    // switch.... determine strategy
    
    return Strategy;
    }
    }
    
    
    
    

    Then your main code does this:

    IStrategy myStrategy = StrategyFactory.GetStrategy(myFile);
    myStrategy.Process(); //or whatever
    
    
    The logic for Process is in the class returned, it can be the same or different routines, it doesn't matter. A new strategy based on the file properties is easily changed or added.
    • Marked as answer by witdaj Tuesday, August 17, 2010 9:50 PM
    Tuesday, August 17, 2010 9:12 PM