Strategy Pattern Q

Hi,

I've been reading about the Strategy Pattern and I would liek to implement into one of my own small projects.

I have an application called WatchDog, this application checks some directories (provided as parameter) and puts every item in a directory in an appropriate mysql table.

I have this section in my code:

privatevoid insertFile(File aFile, String arg){

if (arg.equals("/EBooks"))

qry.InsertEbook(aFile);

elseif (arg.equals("/MP3"))

qry.insertMP3(aFile);

elseif (arg.equals("/Videos"))

qry.insertVideo(aFile);

}

this looks like a perfect candidate for Strategy Pattern to me?

But I'm struggling with the design.

The concept is an insert query, or simpler a query right?

The strategy interface is DataType (EBooks, MP3, Videos, ...)

And Ebook, MP3, Video are ConcreteStrategy classes then?

Can anybody give some clarification on this one pls?

grtz

[1336 byte] By [dfvdfvdfvxfva] at [2007-11-27 5:16:47]
# 1

Not necessarily the Strategy Pattern proper or the best possible solution but you could do something like this:

interface FileHandler

{

public void handle(File file);

}

And then in in another class:

private Map<String, FileHandler> handlers = new HashMap<String, FileHandler>();

// fill the map with a handler for each directory

private void insertFile(File aFile, String arg) {

handlers.get(arg).handle(aFile);

}

dubwaia at 2007-7-12 10:39:36 > top of Java-index,Other Topics,Patterns & OO Design...