Jeongho Suh
Jeongho Suh

Reputation: 3

Rewriting the code so that its readability is improved without changing its behaviour

public class A2 {

public class B implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Fing");
    }
}

public class C implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Fang");
    }
}

public class D implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Foom");
    }

}

public A2(){
    JButton a = new JButton("Fing");
    JButton b = new JButton("Fang");
    JButton c = new JButton("Foom");

    a.addActionListener(new B());
    b.addActionListener(new C());
    c.addActionListener(new D());


}

public static void main(String[] args) {
    A2 a2 = new A2();

}

The problem I encountered is quite simple, but complex. I want it to shorten the code without retouching its functionality. For example, the code is showing to many actionlisteners and actionperformed, and I was trying to make it one class pulling out System.out.println(); and putting in String value on it. However, the coding does not work in this simple ways. Please help me out to curtail this code as simple and increase the readability. Thanks.

Upvotes: 0

Views: 43

Answers (2)

Vasu
Vasu

Reputation: 22412

You can define single class MyActionListener which implements ActionListener as shown below:

public class MyActionListener implements ActionListener {

    private String input;

    public MyActionListener(String input) {
        this.input = input;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(input);
    }
}

public A2(){

    String[] inputs = {"Fing","Fang","Foom"};//Array of JButton inputs
    for(int i=0;i<inputs.length;i++) {
        JButton jButton = new JButton(inputs[i]);//create JButton instance
        jButton.addActionListener(new MyActionListener(inputs[i]));
    }
}

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347194

It's impossible to know what things you could do, I'm personally a fan of self documenting code, so sometimes, you need to be careful when trying to optimise solutions.

My first thought might be to start with the Action's API, which allows you to design a self contained unit of work

public class CommonAction extends AbstractAction {

    public CommonAction(String name) {
        putValue(NAME, name);
        putValue(SHORT_DESCRIPTION, "This is a tool tip for " + name);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(getValue(NAME));
    }

}

You could extend it further to provide more customisation if you needed, overriding the actionPerformed method, but, that's up to you.

Then you just need to apply to your buttons...

public class A2 {

    public A2() {
        JButton a = new JButton(new CommonAction("Fing"));
        JButton b = new JButton(new CommonAction("Fang"));
        JButton c = new JButton(new CommonAction("Foom"));
    }

}

Or your menu's or your key bindings, Action is a rather flexible API supported by a number of other components

Upvotes: 2

Related Questions