Search code examples
javaxmlweb-servicesjframejdom

how to toggle the right class for checkbox listener?


Class B extends Class A

I have a GUI with a start button and a checkbox that creates multiple instances of Class A (A blueprint of my web service) based on given text input. The checkbox if checked writes some additional elements to the XML file (which is the main output of the entire application). Now I want to add an additional checkbox which will provide the GUI to create instances of Class B which extends Class A but provides some additional fields and logic.

My question is that how can I implement this required functionality?

Given there is a createMyXML() method in class C which is doing the same for hardcoded arguments as Class A or Class B for its methods, should I extend it to take one of the class as a parameter and create instances for required elements in the XML?

P.S. It is probable that this question may be too subjective but I wonder what could be the approach or the 'right way' to do it.

Class A Anatomy

public class A {

    private String id;

    private Vector<String> inputs;

    private Vector<String> outputs;

    //***Getters and Setters for above.***

}

Class C Anatomy

public class C {
        void createMyXML(){
            for (A a : this.parser.getAttributes()){
                createFirstElement(A a);
                createSecondElement(A a);
            // Or (This behavior should be triggered by the checkbox)
                createFirstElement(B b);
                createSecondElement(B b);}
    }

Solution

  • If I understand your question right, you want a way of creating different instances that would create XML files based on some logic that differs from class to class, preferably easily extendable later on.

    In this case the Factory Design Pattern seems like a reasonable solution. You define an interface with the craeteMyXML() method:

    public interface I {
        public void createMyXML();
    }
    

    Add a class for each XML creation logic. In this example I've added two classes: A and B, which implement the I interface.

    Class A:

    import java.util.Vector;
    
    public class A implements I {
    
        private String id;
        private Vector<String> inputs;
        private Vector<String> outputs;
    
        @Override
        public void createMyXML() {
            System.out.println("Create XML by the rules of A.");
        }
    
        /* Getters and setters and other methods needed*/
    }
    

    Class B:

    public class B implements I {
    
        @Override
        public void createMyXML() {
            System.out.println("Create XML by the rules of B.");
        }
    }
    

    You can use an enum as a parameter for the factory based on which it creates instances. You can use other options as well, for example a String value or int, it depends on your solution. Here, I define the available instances as an enum:

    public enum Instance {
        A, B
    }
    

    The Factory class creates instance of A or B using the super-type I.

    public class Factory {
        public static I createInstance(Instance i) {
            if (i == Instance.A) {
                return new A();
            } else if (i == Instance.B) {
                return new B();
            } else {
                return null;
            }
        }
    }
    

    I'm adding a Test class to quickly test this solution, you can incorporate it in your project.

    public class Test {
        public static void main(String[] args) {
            I a = Factory.createInstance(Instance.A);
            I b = Factory.createInstance(Instance.B);
            
            a.createMyXML();
            b.createMyXML();
        }
    }
    

    When you execute this, it will produce the output:

    Create XML by the rules of A.

    Create XML by the rules of B.