简体   繁体   中英

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

No, your current design is incorrect.

Your code is clearly violating open closed principle ie, you will end up with switch statements at too many places , which is NOT a good design because if you wanted to add HTML parser (or some other parser later), you need to add the switch statements into two Factory classes.

The solution is you need to use abstract factory pattern as shown below:

interface ContentHandler {
     public void parse();
     public void map();
}


public class XMLContentHandler implements ContentHandler {

    public void parse() { }

    public void map() { }
}


public class JSONContentHandler implements ContentHandler {

    public void parse() { }

    public void map() { }
}

class ContentFactory {
    public static ContentHandler getInstance(FILE_TYPE fileType) {
        switch(fileType) {
        case XML:
            return new XMLContentHandler();
        case JSON:
            return new JSONContentHandler();
        }
        return null;
    }
}

I don't want to share the mapper implementation to the system which reusing the parser.

Parsing and Mapping responsibilities should be handled by their own classes (like XMLParser , XMLMapper , JSonParser , etc..) as shown below, which would then complied to single responsibility principle.

public class XMLContentHandler implements ContentHandler {

        @Inject
        private XMLParser xmlParser;

        @Inject
        private XMLMapper xmlMapper;


        public void parse() { 
            xmlParser.parse();
        }

        public void map() { 
            xmlMapper.map();
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM