How to initialize input fields without writing "if statements"?

I have an enumeration like

public enum Field {
     A, B, C, D, E ....;

     private Field(){
     }
}

I have class Panelone that takes Field arrayto initialize fields:

public class Panel {
     TextBox A; 
     TextBox B;
     TextBox C;
     TextBox D;
     TextBox E;
     ...


     public Panel(Field[] fields){
          this.fields = fields;
          init();
     }

     public void initA(){}
     public void initB(){}
     public void initC(){}
     public void initD(){}
     public void initE(){}
}

My question is, how can I initialize fields that are set without writing many if statements?

I cannot find any solution, and now I initialize it as follows:

public void init(){
      for(int i = 0 ; i < fields.length; i++){
          if(fields[i] == Field.A){
              initA();
          } else if(fields[i] == Field.B){
              initB();
          } else if(fields[i] == Field.C){
              initC();
          } else if(fields[i] == Field.D){
              initD();
          } else if(fields[i] == Field.E){
              initE();
          }  ....
      }
}
+3
source share
10 answers

It looks like your design might need a look. A few tips:

  • Add the init method to your listing. So you can iterate over the array of your enums and call the init method on it, so the enumeration knows how to perform its own initialization
  • Command, . , .
  • - . , .

TextBox, , .

TextBox A = new TextBox(Field.A);
TextBox B = new TextBox(Field.B);

, TextBox , A, B, C, D, E, [], TextBox, ( enum). , TextBox -, , , API .

, . Java , . , , Google API ( , , Go...). (), (ifs switch) . TextBox, .

+5

Factory pattern, Singleton pattern (enum based) Command pattern. , . A Factory (Singleton) - . , if/switch Factory ( ..).

// the init command
public interface PanelInitializer {
  public init(Panel p);
}

// the factory
public enum PanelInitializerFactory {
  INSTANCE;

  public PanelInitializer create(Field field) {
    switch (field) {
      case A: return new TypeAInitializer();
      case B: return new TypeBInitializer();
      case C: return new TypeCInitializer();
      //..
    }
  }
}

, / , .

+1

, @planetjones, init() enum. init TextBox (enum) . , this, .

, , TextBox es

public void init(){
  for(int i = 0 ; i < fields.length; i++){
      F[i] = fields[i].init(this);
  }
}

.

public void init(){
  TextBox F[5];
  for(int i = 0 ; i < fields.length; i++){
      F[i] = fields[i].init(this);
  }
  A = F[0];
  B = F[1];
  C = F[2];
  D = F[3];
  E = F[4];
}

, .

+1

, init . Field init initX(). init init .

enum Field
{
  A{public void init(){initA();}},
  B{public void init(){initB();}},
  C{public void init(){initC();}},

  public abstract void init();
}
+1

java- , initN().

0

, , . , TextBox .

, TextBox .

, enum . , , .

, , . .

, . . "", .. , - ifs switch.

, , , ? , ifs .., .

0

? , ""? if:

  public void init(){
    for(int i = 0 ; i < fields.length; i++){
        switch(fields(i)){
          case A:
            initA();
            break
          case B:
          ...
        }
    }
  }

initA, initB... ? 20 20 init , ...

0

, init, TextBox, , - . , Panel, , .

, java.util.EnumMap. :

  • TextBoxes ,
  • initA,...
  • - -.

.

0

:

import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FooPanelMain {

    public static void main(String[] args) {

        FooPanel panel = new FooPanel();
        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }

}    

class FooPanel extends JPanel {

    // fields are dynamically created, so we put them into a map
    private Map<PanelField, JLabel> fields = new HashMap<PanelField, JLabel>();

    // enum to configure the fields
    private enum PanelField {
        FIRST("first text"), 
        SECOND("second text"), 
        LAST("last text");

        private String text;

        private PanelField(String text) {
            this.text = text;
        }

        public String getLabelName() {
            return text;
        }

    }
        // constructor uses the enum configuration to create the fields
    public FooPanel() {
        for (PanelField fooPanelField : PanelField.values()) {
            createLabel(fooPanelField);
        }
    }

    private void createLabel(PanelField field) {

        JLabel label = new JLabel(field.getLabelName());
        this.add(label);
        fields.put(field, label);
    }
}    

This example can be easily turned into an abstract solution by defining an interface for PanelField, which is implemented by enumerations. FooPanel can be used as a base class for panels.

0
source

best u move init to enumeration like:

public enum Example{ 
  value1(1), 
  value2(2), 
  value3(66);

  private final int internalValue;

  Example(int value){ 
       this.internalValue = value; 
  }

  public int getInternalValue(){ 
       return this.internalValue; 
  }
}

Although this is a really simple example, you can add code later to the constructor and make more complex decisions based on the actual object itself.

-1
source

All Articles