5
\$\begingroup\$

Using:

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 Toast class I often miss the call to show()
  • Parameter length has type int. So a compiler couldn't detect invalid argument value

What do you think? Maybe this class is useless?

Can you suggest any improvements?

Simon Forsberg
59.7k9 gold badges157 silver badges311 bronze badges
asked Aug 29, 2014 at 8:45
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

There is one important thing missing on your LONG and SHORT fields.

What if I do ToastUtils.LONG = null; ?

That's right, I would break it.

You must specify them as final

public class ToastUtils {
 public static final ToastUtils LONG = new ToastUtils(Toast.LENGTH_LONG);
 public static final ToastUtils SHORT = new ToastUtils(Toast.LENGTH_SHORT);

However, you might just as well change this one and make it an enum

public enum ToastUtils {
 LONG(Toast.LENGTH_LONG), SHORT(Toast.LENGTH_SHORT);
 ... // the rest of your code can look the same

But I have to agree with @Kao that your class does not seem to be very useful.

It is better to learn the existing API than creating your own API to avoid having to learn the existing API (Yes, I just made that up. It makes sense though, doesn't it?)

answered Aug 29, 2014 at 9:46
\$\endgroup\$
5
\$\begingroup\$

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 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?

answered Aug 29, 2014 at 9:03
\$\endgroup\$
3
  • \$\begingroup\$ That there can only be one instance of LONG and SHORT ToastUtils should not matter as they are simply factories for creating the real toast. \$\endgroup\$ Commented Aug 29, 2014 at 9:49
  • \$\begingroup\$ Yes, you're right. But this still will make things complicated. \$\endgroup\$ Commented Aug 29, 2014 at 9:55
  • \$\begingroup\$ Yes, overall the class is quite useless \$\endgroup\$ Commented Aug 29, 2014 at 10:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.