patternjavaMinor
ToastUtils - helper class for Android SDK Toast
Viewed 0 times
toastutilsandroidhelpersdkfortoastclass
Problem
Using:
Code:
In my opinion, there are some disadvantages of using
What do you think? Maybe this class is useless?
Can you suggest any improvements?
ToastUtils.LONG.show(context, R.string.greeting);Code:
public class ToastUtils {
public static ToastUtils LONG = new ToastUtils(Toast.LENGTH_LONG);
public static ToastUtils SHORT = new ToastUtils(Toast.LENGTH_SHORT);
private final int duration;
private ToastUtils(int duration) {
this.duration = duration;
}
public Toast makeText(Context context, String text) {
return Toast.makeText(context, text, duration);
}
public void show(Context context, String text) {
makeText(context, text).show();
}
public Toast makeText(Context context, int resId) {
return Toast.makeText(context, resId, duration);
}
public void show(Context context, int resId) {
makeText(context, resId).show();
}
}In my opinion, there are some disadvantages of using
Toast class directly:- In
Toastclass I often miss the call toshow()
- Parameter
lengthhas typeint. So a compiler couldn't detect invalid argument value
What do you think? Maybe this class is useless?
Can you suggest any improvements?
Solution
IMO this class isn't very useful (at least in a current shape).
In Toast class I often miss the call to show()
Well, it's not a bug, it's a feature. There are many situations when you only want to prepare your Toast using
Parameter length has type int. So a compiler couldn't detect invalid argument value
I wouldn't say that this is a bug, too. And you're fixing it in a bad way, again:
So there can only be single instances of
To sum up, this is usage example of your class:
And this is the same without using your class:
It's not a big improvement, is it?
In Toast class I often miss the call to show()
Well, it's not a bug, it's a feature. There are many situations when you only want to prepare your Toast using
makeText() and show it later with show(). Moreover, you don't fix this "bug" with your class - there is still a makeText() method which does exactly what Toast used to do. And you still can forget to call show(). Parameter length has type int. So a compiler couldn't detect invalid argument value
I wouldn't say that this is a bug, too. And you're fixing it in a bad way, again:
public static ToastUtils LONG = new ToastUtils(Toast.LENGTH_LONG);
public static ToastUtils SHORT = new ToastUtils(Toast.LENGTH_SHORT);So there can only be single instances of
LONG and SHORT at the time. This will make many things complicated. To sum up, this is usage example of your class:
ToastUtils.LONG.show(context, R.string.greeting);And this is the same without using your class:
Toast.makeText(context, R.string.greeting, Toast.LENGTH_LONG).show();It's not a big improvement, is it?
Code Snippets
public static ToastUtils LONG = new ToastUtils(Toast.LENGTH_LONG);
public static ToastUtils SHORT = new ToastUtils(Toast.LENGTH_SHORT);Context
StackExchange Code Review Q#61435, answer score: 5
Revisions (0)
No revisions yet.