Java – many Boolean flags are entered into the class

I have a dialog box that displays various contents according to the status of the application and the security of the current user

new MyDialog(showOptionsTable,allowFooInput,allowBarInput,isSuperUser)

At first there were signs, which was good But now, with the change of demand, it has evolved into the input of five Boolean flags

What are the best practices for dealing with this behavior? Should this be subclassed according to the appearance of the dialog box?

Solution

Like many things, "it depends on."

>Ben Noland suggests a class to store configuration options This is feasible, but it is conducive to invariance, and you can choose to use the builder pattern Because Boolean values are built-in types, writing a small builder will really help people understand the code If you compare it with mydialog (real, real,...), you will know what I mean:

Options. allowThis(). Allowthat () Chris proposed bit fields, but as some commentators pointed out, bit fields are evil for many reasons listed in Josh Bloch's effective Java Basically they are hard to debug and error prone (you can pass in any int and it will still compile) So if you take this route, use real enums and enumsets. > If you can reasonably inherit (or write), which means that you usually use only a few combinations of all Boolean values, do so

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>