What is the best way to organize Java code since you cannot follow the link?

I’m learning how to code in Java after you come from C. In C, I have always divided everything into separate functions to make the code more convenient for further editing and editing. I tried to do this in java, but now, since I realized that you cannot use pointers, I'm a little confused that the best way to do this is.

So, for example, I want to have a method that generates four warnings for me. Therefore, I give him the alert creator, who can then generate alerts. I can return them to an array, but in my code I already have alerts individually named, and I would like to save it that way, so I would not need to refer to them as a warning [1], alert [2]. .. etc.

So this means that I would have to rename them, which would add additional code, which would probably be longer than the code in the real method!

I think about it right? Is there anything I can do?

-Edit-

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(this.getString(R.string.ache_Q_text))
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {dialog.cancel();}
               });

        final AlertDialog ache_Q_alert = builder.create();

        builder.setMessage(this.getString(R.string.food_Q_text));
        final AlertDialog food_Q_alert = builder.create();

        builder.setMessage(this.getString(R.string.event_Q_text));
        final AlertDialog event_Q_alert = builder.create();

        builder.setMessage(this.getString(R.string.ache_Q_text));
        final AlertDialog ache_type_Q_alert = builder.create();

and instead replace it with

createAlerts();

and this code is off somewhere on the side.

+5
source share
4 answers

, , . :

1. ,

Alert foo = createOneAlert(x,y,z);
Alert bar = createOneAlert(a,b,c);
// etc.

2. :

public class Alerts {
  private final Alert foo;
  private final Alert bar;
  private final Alert baz;
  private final Alert quux;

  public Alerts(Alert foo, Alert bar, Alert baz, Alert quux) {
    this.foo = foo;
    this.bar = bar;
    this.baz = baz;
    this.quux = quux;
  }

  public Alert getFoo() { return foo; }
  // etc.

}

public Alerts makeAlerts(AlertBuilder builder) {
  return new Alerts(
    builder.buildAlert(a,b,c),
    builder.buildAlert(d,e,f),
    builder.buildAlert(g,h,i),
    builder.buildAlert(x,y,z));  
}

, a) Java b)

+6

? , , ?

, , # out , int.TryParse ( "" - ).

+1

Alert. , , 4 .

, .

.

, , , . Java , , . ( ). "" , .

0

, davetron5000's.

Another option is to pass a list (or other collection) to your method createAlerts();- with or without a builder. Maybe something like:

List<Alert> alerts = new ArrayList<Alert>();
createAlerts(builder, alerts);
...

private void createAlerts(AlertBuilder builder, List<Alert> alerts) {
    alerts.add(builder.buildAlert(a,b,c));
    alerts.add(builder.buildAlert(d,e,f));
    alerts.add(builder.buildAlert(g,h,i));
    alerts.add(builder.buildAlert(x,y,z));
}

You can also put on your Spring hat and bind all the warnings in XML, and then enter them as a collection.

0
source

All Articles