What is the best way to organize Java code since you can't pass by reference?
I'm learning how to code in Java after after coming from C. In C I always separated everything into individual functions to make the code easier to follow and edit. I was trying to do this in java but now since I realized that you can't use pointers, I am a bit confused as to what the best way to do this is.
So for example I want to have a method that creates four alerts for me. So I pass it an alert builder that can then create the alerts. I can return them in an array, but in my code I already have the alerts individually named, and I would like to keep it that way so I wouldn't need to refer to them as alert[1], alert[2]... etc.
So that means I would have to rename them, which would add additional code which would probably be longer than the code in the actual method!
Am I thinking about this the right way? 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(thi开发者_Python百科s.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 have that code off somewhere to the side.
It sounds like you want to have a method create four objects for you and return them. You have a few options:
1. Modify your method to create only one object, and call it four times, e.g.
Alert foo = createOneAlert(x,y,z);
Alert bar = createOneAlert(a,b,c);
// etc.
2. Create a data structure to hold your alerts:
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));
}
It may be more verbose but a) that's Java and b) verbosity can be a good thing
Are these four alerts related? Could you create an appropriate class which encapsulates the four of them, including the code to create them?
I rarely find I need a method to return multiple unrelated items - although I occasionally miss C#'s out
parameters for things like int.TryParse
(where a method may "fail" in a non-exceptional way).
I would wrap these alerts in an Alert class. Depending on the difference, either as objects of the same class or as 4 different classes.
You can then pass these objects to your function to initialise them.
Then the structure of your could would not change a lot.
This is not the way things are done in Javaland, but it illustrates why the lack of pointers poses no significant issues. In Java classes are mostly configured in their constructor, so the complete lifecycle of creation, operation and destruction is encapsulated in the class. (This is somewhat of an approximation). This allows the class to "hide" implementation details from the outside world which helps building more robust applications.
Some good answers here, especially davetron5000's.
Another option would be to pass a list (or other collection) into your createAlerts();
method -- with or without the builder as well. 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 could also put on your Spring hat and wire all of the alerts in XML and then inject them as a collection.
精彩评论