samedi 8 avril 2017

Two factory class to generate different interface instances with same condtion

Java Program

enum FILE_TYPE {
    XML, JSON;
}

interface Parser {
    void parse();
}
class XMLparser implements Parser {
    public void parse() { }
}
class JSONparser implements Parser {
    public void parse() { }
}

interface Mapper {
    void map();
}
class XMLmapper implements Mapper {
    public void map() { }
}
class JSONmapper implements Mapper {
    public void map() { }
}

class ParserFactory {
    public static Parser getInstance(FILE_TYPE fileType) {
        switch(fileType) {
        case XML:
            return new XMLparser();
        case JSON:
            return new JSONparser();
        }
        return null;
    }
}

class MapperFactory {
    public static Mapper getInstance(FILE_TYPE fileType) {
        switch(fileType) {
        case XML:
            return new XMLmapper();
        case JSON:
            return new JSONmapper();
        }
        return null;
    }
}

In above java program both factory method generate different interface instance depends on same condition here, i,e both uses same enum FILE_TYPE.

Is it right to use two factory method for this case? constraint is i can't combine both interface into one.

I am very new to java design, Kindly help me

Aucun commentaire:

Enregistrer un commentaire