What are helper objects in java?

I come across several points called auxiliary objects ... can anyone clarify what these auxiliary objects are and why we need them?

+32
java
Jan 25
source share
6 answers

Some operations that are common to several classes can be transferred to auxiliary classes, which are then used using object composition:

public class OrderService { private PriceHelper priceHelper = new PriceHelper(); public double calculateOrderPrice(order) { double price = 0; for (Item item : order.getItems()) { double += priceHelper.calculatePrice(item.getProduct()); } } } public class ProductService { private PriceHelper priceHelper = new PriceHelper(); public double getProductPrice(Product product) { return priceHelper.calculatePrice(product); } } 

Using helper classes can be done in several ways:

  • Executing them directly (as indicated above)
  • through dependency injection
  • By creating your own static methods and accessing them in a static way, for example, IOUtils.closeQuietly(inputStream) closes InputStream exceptions that throw an exception.
  • at least my convention is to call classes only static methods, not XUtils dependencies, and classes, which in turn have dependencies / should be managed by the XHelper DI XHelper

(The example above is just a sample - it should not be discussed in terms of Driven Design)

+47
Jan 25 '10 at 21:30
source share

These are objects that "sit apart" from the main part of the code and do part of the work for the object. They "help" the object do the work.

For example, many people have a Closer helper object. This will take various lockable objects, for example, java.sql.Statement, java.sql.Connection, etc., and will close the object and ignore any errors that come out of it. This is because if you get an error while closing the object, there is nothing you can do about it, so people just ignore it.

Instead of having this template:

 try { connection.close(); } catch (SQLException e) { // just ignore… what can you do when you can't close the connection? log.warn("couldn't close connection", e); } 

scattered around the code base, they simply call:

 Closer.close(connection); 

instead. For example, look at guava closeQuietly .

+11
Jan 25 '10 at 9:28 a.m.
source share

The helper method is usually a way to make something easier, whatever it is. Sometimes they are used to make things more readable / clearly organized (some may argue about this, but are ultimately very subjective):

 public void doStuff() { wakeUp(); drinkCoffee(); drive(); work(); goHome(); } 

Where each "helper method" in itself is quite complex ... the concept becomes very clear and simple.

Another very good use of helper methods is to provide common functionality in many different classes. The best example of this is the Math class, which contains a ton of static helper methods to help you calculate things like a number log, a number exponent ... etc.

Where do you draw a line regarding the helper method and the fact that the usual method is quite subjective, but its essence. Other answers here are pretty good too.

+1
Jan 25 '10 at 21:33
source share

The helper class, in my opinion, is similar to ordinary functions declared outside of classes in C ++. For example, if you need a global constant for many classes, then you can define a helper class that includes a final static constant variable.

0
May 16 '13 at 2:10
source share
 import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Helpers { public static String getDate() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); return dateFormat.format(new Date()); } public static boolean isTimeABeforeTimeB(String timeA, String timeB) { try { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa"); Date dA = dateFormat.parse(timeA); Date dB = dateFormat.parse(timeB); if (dA.getTime() < dB.getTime()) { return true; } else { return false; } } catch (Exception e) { // } return false; } public static String getDateAndTimeInput(String prompt) { Scanner input = new Scanner(System.in); String ans; SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa"); dateFormat.setLenient(false); boolean dateValid; do { System.out.print(prompt); ans = input.nextLine(); ans = ans.trim(); dateValid = true; try { Date d = dateFormat.parse(ans); } catch (Exception e) { dateValid = false; } } while (!dateValid); return ans; } public static String getStringInput(String prompt) { Scanner input = new Scanner(System.in); String ans; do { System.out.print(prompt); ans = input.nextLine(); ans = ans.trim(); } while (ans.length() == 0); return ans; } public static double getDoubleInput(String prompt) { Scanner input = new Scanner(System.in); double ans = 0; boolean inputValid; do { System.out.print(prompt); String s = input.nextLine(); //Convert string input to integer try { ans = Double.parseDouble(s); inputValid = true; } catch (Exception e) { inputValid = false; } } while (!inputValid); return ans; } public static int getIntegerInput(String prompt) { Scanner input = new Scanner(System.in); int ans = 0; boolean inputValid; do { System.out.print(prompt); String s = input.nextLine(); // Convert string input to integer try { ans = Integer.parseInt(s); inputValid = true; } catch (Exception e) { inputValid = false; } } while (!inputValid); return ans; } public static int getIntegerInput(String prompt, int lowerBound, int upperBound) { Scanner input = new Scanner(System.in); int ans = 0; boolean inputValid; do { System.out.print(prompt); String s = input.nextLine(); // Convert string input to integer try { ans = Integer.parseInt(s); if (ans >= lowerBound && ans <= upperBound) { inputValid = true; } else { inputValid = false; } } catch (Exception e) { inputValid = false; } } while (!inputValid); return ans; } } 

which is an example helper class. It contains a method that is a common use of other classes in a project.

Example, if someone wants to enter an integer from a class that needs to enter the following: String num = Helpers.getIntegerInput ("enter your number");

A prompt is the output that is displayed to the user. Other examples of input string, double, date and time, etc.

0
Feb 03 '15 at 10:33
source share

You can see the helper class as a toolbox that can be used by other classes to perform tasks such as testing if the string is a palindrome, if the given number is prime, the array contains a negative number, etc. You can create a helper class by executing all its static methods and its private constructor, and you can also make the class final. Thus, it cannot be created, and you can easily access all of its methods directly.

 public final class HelperClass{ private HelperClass(){ } public static boolean isPositive(int number) { if (number >= 0) return true; else return false; } } 

Here the function can be used directly to check the number:

  HelperClass.isPositive(5); 
0
Dec 19 '18 at 11:54
source share



All Articles