Reputation: 363
I have been programming for a little while now, and I have a question on the use of public methods. I am working on a vending machine program, and I have a private method setUpMachine()
to initialize the game and set up the objects. I have another private method startMachine()
that starts the game and prompts the user for input. It then passes the input to yet another private method checkInput()
that checks to see if the input is valid... but this is where I run into not so much of a "problem", but a weird feeling of me not doing something correctly. I need access to the objects that are in my first method, setUpMachine()
for my third method checkInput()
. The problem is that I have many objects (candy, chips, soda, cookie), and passing them all to check perimeters just doesn't seem right. In other words, doing this:
checkInput(FoodType candy, FoodType chips, FoodType soda, FoodType cookie)
doesn't seem right. Does this mean, that if I use private methods, I have to pass objects every time I want to use them? I read that making public methods is bad practice.
An explanation on this would be nice, not so much an explanation telling me my coding is inefficient, but more an explanation describing when and how to use private methods, or if there is another way to go about doing this.
Upvotes: 5
Views: 4114
Reputation: 27687
If you don't want to pass the objects around, you can configure them as instance variables:
public class VendingMachine {
private FoodType candy;
private FoodType chips;
private FoodType sodas;
private FoodType cookies;
private static String errorMessage = "A really bad error occurred.";
public VendingMachine(){
this.setupMachine();
}
private void setUpMachine(){
this.candy = new FoodType();
this.chips = new FoodType();
this.sodas = new FoodType();
this.cookies = new FoodType();
}
private boolean checkInput(){
if (this.candy==null || this.chips==null || this.sodas==null || this.cookies==null)
return false;
else
return true;
}
public void doSomething() throws Exception() {
if (!this.checkInput()) throw new Exception(VendingMachine.errorMessage);
// do things
}
}
This class can then be called as
VendingMachine vendingMachine = new VendingMachine();
try {
//vendingMachine.checkInput() is not available because it is private
vendingMachine.doSomething(); // public method is available
} catch (Exception e){
// validation failed and threw an Exception
}
Upvotes: 3
Reputation: 51030
The private methods are not visible to the ouside world. You can only invoke the private methods from the methods of the same class.
Private methods can be used to logically break down a big method into a number of smaller pieces (steps).
Also if you have some code that seem to repeat in multiple methods in your class then you can make use a private method.
You don't need to define an object as a parementer of the private method if the object is also a field in the class, but if not then you have to.
Upvotes: 2
Reputation: 57222
In general, public methods should be used when you want to expose something to the outside world. Typically, it is a more high level method, where the private methods are used to provide the details. Private methods can be easily changed without impacting other classes, where public methods cannot.
Consider the example
public void run() {
setUpGame();
validateInput();
doSomethingInteresting();
endGame();
notifyUserGameIsComplete();
}
In this example, run
is public, but none of the other methods are. This is because callers of the public method only know they want to run the game but are not concerned with the details of exactly how it works.
Having many private methods is ok for sure. Methods should be single focused and each only do one thing. That allows you to read, debug and maintain the code more easily.
If multiple methods need to share data, you can declare them as member variables of the class. This means they are state of the class. For example:
public class Person {
private String name;
public String getName() { return name; }
public boolean nameMatches(String pattern) { // do some regex testing against the name }
}
In this example, the methds share access to name
, and name
is a part of the person.
If you really need to pass a bunch of parameters, you can create an object to hold all of them. So instead of passing param1, param2, param3, etc, you could create a data transfer object, such as
class MyStuff {
private final String param1;
private final String param2;
private final String param3;
}
Now you can pass MyStuff
around as a convenience rather than listing individual parameters.
Hope this helps add some clarity.
Upvotes: 2