package factory; //Main.java //========= public class Main { public static void main(String[] args) { Select factory = new Select(); Writer w = factory.getWriter(); w.write("hello","guy"); } } // Select.java // =========== public class Select { public Writer getWriter() { Math math = new Math(); if(math.write(math._msg1,math._msg2) == true) { return new Math(); } else { return new Text(); } } } // Writer // ================== package factory; public interface Writer { public boolean write(String msg1, String msg2); } // Math.java // ========= package factory; public class Math implements Writer { protected String _msg1; protected String _msg2; public boolean write(String msg1, String msg2) { _msg1 = msg1; _msg2 = msg2; try { Integer.parseInt(_msg1); Integer.parseInt(_msg2); return true; } catch (NumberFormatException ex) { return false; } } } // Text.java // ========= package factory; public class Text implements Writer { public boolean write(String msg1, String msg2) { return false; } }