4

The background

One of the functionality of a product that I am currently working on is to process a set of compressed files ( containing XML files ) that will be made available at a fixed location periodically (local or remote location - doesn't really matter for now) and dump the contents of each XML file in a database. I have taken care of the design for a generic parsing module that should be able to accommodate the parsing of any file type as I have explained in my question linked below. There is no need to take a look at the following link to answer my question but it would definitely provide a better context to the problem

Generic file parser design in Java using the Strategy pattern

The Goal

I want to be able to keep a track of the status of each XML file and the status of each compressed file containing the XML files. I can probably have different statuses defined for the XML files such as NEW, PROCESSING, LOADING, COMPLETE or FAILED. I can derive the status of a compressed file based on the status of the XML files within the compressed file. e.g status of the compressed file is COMPLETE if no XML file inside the compressed file is in a FAILED state or status of the compressed file is FAILED if the status of at-least one XML file inside the compressed file is FAILED.

A possible solution

The Model

I need to maintain the status of each XML file and the compressed file. I will have to define some POJOs for holding the information about an XML file as shown below. Note that there is no need to store the status of a compressed file as the status of a compressed file can be derived from the status of its XML files.

public class FileInformation {

    private String compressedFileName;
    private String xmlFileName;
    private long lastModifiedDate;
    private int status;

    public FileInformation(final String compressedFileName,
            final String xmlFileName, final long lastModified, final int status) {
        this.compressedFileName = compressedFileName;
        this.xmlFileName = xmlFileName;
        this.lastModifiedDate = lastModified;
        this.status = status;
    }
}

I can then have a class called StatusManager that aggregates a Map of FileInformation instances and provides me the status of a given file at any given time in the lifetime of the appliciation as shown below :

public class StatusManager {    
    private Map<String,FileInformation> processingMap = new HashMap<String,FileInformation>();  

    public void add(FileInformation fileInformation) {
        fileInformation.setStatus(0); // 0 will indicates that the file is in NEW state. 1 will indicate that the file is in process and so on..
        processingMap.put(fileInformation.getXmlFileName(),fileInformation);
    }

    public void update(String filename,int status) {        
        FileInformation fileInformation = processingMap.get(filename);
        fileInformation.setStatus(status);
    }   

}

That takes care of the model for the sake of explanation.

So whats my question?

Edited after comments from Loki and answer from Eric : -

I would like to know if there are any existing design patterns that I can refer to while coming up with a design. I would also like to know how I should go about designing the status management classes.

I am more interested in understanding how I can model the status management classes. I am not interested in how other components are going to be updated about a change in status at the moment as suggested by Eric.

9
  • 1
    for tracking the status, did you consider Observer or Publish–subscribe pattern?
    – gnat
    Feb 26, 2013 at 14:49
  • 1
    You are thinking about design patterns wrong. You don't ask yourself what design patterns can I use. But look at your code and say "That looks like a XXX Pattern". Now you can describe your code in those terms: programmers.stackexchange.com/a/70893/12917 Feb 26, 2013 at 16:54
  • Have you considered using different directories for files in different status? For example: after the process, the file will be move from processing to processed folder. Our company processes 10K+ files in and out everyday and I found this file system based approach is easy to understand and maintain.
    – Codism
    Feb 26, 2013 at 18:11
  • @Loki With all due respect, you are suggesting that I should reinvent the wheel so that someday when I decide to come out of my cave, I can compare my wheel with others and sa hey look my wheel llooks something like yours?
    – CKing
    Feb 26, 2013 at 18:27
  • @Loki Every object oriented programmer has heard of the book called "Design patterns - Elements of reusable object oriented software". If we should not reuse a design patterns, then what is the point in documenting them?
    – CKing
    Feb 26, 2013 at 18:53

1 Answer 1

1

The State pattern is used to define behavior of an object based on its current state, which isn't really a good fit for this problem if I read your question correctly (although I understood the instinct to look at that pattern). It'd work if you had code that was something like:

if (status.equals("COMPLETED"))
{
    doSomethingForCompleted();
}
else if (status.equals("FAILED"))
{
    doSomethingForFailure();
}
// You get the idea...

Given the way your example is set up, I think you'd want a combination of the Observer pattern like gnat suggested, and the Mediator pattern, which you're largely doing with your StatusManager already. StatusManager can notify your code with the new set of statuses after add() or update() calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.