How do JavaBeans design square with hidden information?
Two semesters ago, I had a professor who said:
Some of you have been told to always include setter and getter methods for all private instance variables. I say this violates information hiding and often results in systems in which invariants cannot be applied.
Now it sounds right to me. But don't these types of setters / getters include the bulk of JavaBeans creation? If so, why? If not, what am I misunderstanding about JavaBeans?
source to share
In the Java Bean class, Getters and Setters are not required. All that is required is that the class must be public, it must have a public argument arguments constructor, and must implement Serializable. However, in order for the variables to be automatically detected when using the Bean, you must provide getters and setters according to the standard naming convention (getVarname, setVarname ...).
In any case, you only want to make variables externally visible that have a business outside of your class.
source to share
You can read Why getter and setter methods are evil :
You might argue by saying, "But what about JavaBeans?" What about them? can certainly create JavaBeans without getters and setters.
BeanCustomizer
,BeanInfo
andBeanDescriptor
classes exist for just that purpose. JavaBean hardware designers threw the getter / setter idiom into the picture because they thought it would be an easy way to quickly make a bean - what you can do while you learn how to do it right. Unfortunately, nobody did it.Accessors were created solely as a way to mark certain properties so that the UI-builder or its equivalent can identify them. You don't have to call these methods yourself. They exist for the use of an automated tool. This tool uses the class introspection API
Class
to find methods and extrapolate the existence of certain method property names. In practice, this idiom based on introspection is not designed. He made the code significantly too complex and procedural. Programmers who do not understand the abstraction data actually call accessory, and as a result, the code is less maintainable.
source to share
Your professor is right. You don't want to blindly create getters and setters for all instance variables. You want to create them where you need them.
If you have a class BankAccount
with an instance variable balance
for creating getters and setters, it makes sense if you want to check and balance.
Even here information is hiding - in the form of implementation encapsulation. The getter double getBalance()
could simply return the value of the underlying instance variable if it is double
, or it could return the value it got from BigDecimal
if it is an implementation choice for the variable, or it could invoke a remote web service and return a result. This way the getter / setter still allows implementation changes and thus does not break encapsulation (and also does not support JavaBeans).
What JavaBeans is for you defines an interface ( getXXX(), setXXX()
) for getting and setting "properties", class attributes that users would typically like to examine or change. If your class has information that is not considered a "property", there is no need to expose it. For example, let's say the class BankAccount
has an instance variable used to check for checkout. If the client doesn't need access or change it, there is no point in creating a getter or setter for it.
source to share