Filemonitor - Strategy pattern?
Hi all.
I have an application that I think needs to use the strategy pattern but I'm not sure.
I'm fairly new to design patterns but I would like to be able to use the right one and as yet I can't see which one to use and where.
My first instance of the application places a listener on a file in the local file system and when the modified date of that file changes it notifies the application that the file has changed.
Fairly simple.
I have a need now to monitor files not only on the local file system but also on a ftp server.
I thought of adding this into the class but then I thought "what would happen if I wanted to monitor a file in a database, on a remote system, somewhere I hadn't thought of, etc".
So a design pattern was needed... but which one.
I thought the Strategy pattern looked best from my reading but I'm not sure. I don't want to use the wrong pattern and end up having a more complicated design than I started with.
I hope someone can help. If I haven't put enough information here then I do apologize and will put more if requested.
Thanks in advance.
All the best,
Tony
As of my knowledge ,startegy pattern would best fit.
You can have an abstract strategy class
public abstarct FileMontor {
abstract void checkFile(Object file);
}
public class LocalFileMonitor extends FileMonitor
{
//Override the method checkFile(Object file) and write code
}
public class DBFileMonitor
{
//Override the method checkFile(Object file) and write code
}
//In the same fashio we can keep on creating the strategies.
But now the problem is who decides which strategy to use at what point of time, is there a way i can register the various strategies ...
So you should have register *** controller class, where you can register all ur strategies.. and it should have intelligenece to choose the strategy at runtime from the input it is receving at runtime.
I thought so. It seemed right but I just wasn't sure. There are many patterns and there may have been a better fit.
As for which startegy to use, I know ahead of time where the file is to be stored and monitored. I could just put that in the application I'm writing but I guess then you're right. What happens if the file is moved somewhere else in the future? I'd have to change the application instead of maybe changing a properties file.
Anyway, thats very much for your help.
It's been greatly appreciated.
All the best,
Tony