Implementation of Factory Method pattern for XML and Excel documents I started learning factory pattern, and also implemented factory method pattern.

My factory-method pattern classes are as follows:


There are two major parts, an interface "Document.java" and and abstract class DocumentCreator.java which has an abstract method createDocument.
Classes XMLDocumentCreator and ExcelDocumentCreator are subclasses of DocumentCreator.
Classes XMLDoc and ExcelDoc both are implementing Document interface.


Interface Document.java

public interface Document { 
  String parse(String parser);
}


Classes implementing above interface

public class XMLDoc implements Document{

  @Override
  public String parse(String parser) {      
    return "This XML document can be parsed by \""+parser+"\" parser";
  }

}


public class ExcelDoc implements Document{

  @Override
  public String parse(String parser) {      
    return "This Excel document can be parsed by \""+parser+"\" parser";
  }

}


Abstract class DocumentCreator.java is as follows:

public abstract class DocumentCreator {
  public abstract Document createDocument();
}


Classes extending the above class is:

public class ExcelDocumentCreator extends DocumentCreator{

  @Override
  public Document createDocument() {        
    return new ExcelDoc();
  }

}

public class XMLDocumentCreator extends DocumentCreator{


  @Override
  public Document createDocument() {        
    return new XMLDoc();
  }

}


This is my MainClass.java from where I run the application:

public class MainClass {
  public static void main(String [] args){
    DocumentCreator docCreator = new XMLDocumentCreator();
    System.out.println(docCreator.createDocument().parse("SAX"));
    System.out.println(docCreator.createDocument().parse("DOM"));

    docCreator = new ExcelDocumentCreator();
    System.out.println(docCreator.createDocument().parse("MSDoc"));
    System.out.println(docCreator.createDocument().parse("OpenOffice"));
  }
}


When I run the class, I get:




This XML document can be parsed by "SAX" parser
This XML document can be parsed by "DOM" parser
This Excel document can be parsed by "MSDoc" parser
This Excel document can be parsed by "OpenOffice" parser



Now, after looking at my class hierarchy, is the implementation a proper usage of factory-method pattern? Is the "DocumentCreator creates document" problem solved properly by the pattern? Can you please give me any suggestions like where the factory-method pattern can be applied?
Although your implementation is superficially correct, my guess is that it is missing some of the finer points of factory method implementation . 

A good example that you can look at is URLStreamHandlerFactory in java.

Some minor changes that I would make to your code would be to Introduce an Enum such as 

public enum TYPE {
    EXCEL,
    XML

}


Modify my DocumentCreator to 

public abstract class DocumentCreator {
    public static Document createDocument(TYPE type) {

        if (type.equals(TYPE.EXCEL)) {
            return new ExcelDoc();
        } else if (type.equals(TYPE.XML)) {
            return new XMLDoc();
        } else {
            // Default Implementation
            return new XMLDoc();
        }

    }

    public static Document createDocument() {

        return new XMLDoc();

    }
}


And my main Class would invoke as follows : 

public class MainClass {

    public static void main(String [] args){
       // DocumentCreator docCreator = new XMLDocumentCreator();
        System.out.println(DocumentCreator.createDocument().parse("SOMETHING"));
        System.out.println(DocumentCreator.createDocument(TYPE.XML).parse("SAX"));
        System.out.println(DocumentCreator.createDocument(TYPE.EXCEL).parse("DOM"));
        System.out.println(DocumentCreator.createDocument(TYPE.XML).parse("MSDoc"));
        System.out.println(DocumentCreator.createDocument(TYPE.EXCEL).parse("OpenOffice"));


      }
}


These are just a few minor modifications to your code . I would suggest you to go though this Factory Pattern Explained to understand the finer aspects  of 1) Instantiation control 2) loose coupling 3) improving readability by encapsulation  which is feel is the primary need of using the factory pattern.