Java : Contract between checking data and using data
I am developing the UI for a Java desktop application with Swing. There is a "main page" frame that displays many clickable icons for viewing panels. For any given panel, the frame needs a way to check if at least one of the data items the panel will use is set; if so, it enables the button to instantiate the class. If not, it disables the button. Pretty simple, but I have the following constraints that make it difficult:
(1) The panel accepts an object in its constructor containing a lot of data, although it only uses some of it, and
(2) I am not allowed to instantiate the panel until the user clicks a button to view it.
Here are three ideas; A and B violate the constraints, while C works but creates maintenance problems.
(A) What I wish I could do is have the panel only accept the data it is expected to use, and instead have the caller be in charge of determining whether that data is set; however, this violates constraint 1.
(B) Another option would be to build and cache the panel in advance, and record whether each data element it needs is set in a boolean. Once it's done being constructed, the frame can check that boolean. However, this violates constraint 2.
(C) The simplest solution I can think of is to equip the panel with a static method called "isActive" accepting the large data object to check if the data it intends to use is set. If so, then the frame will enable the button, and the panel can be viewed. However, there is no contract between the data checked in the static method and the data actually accessed in the panel. This creates two problems:
- If the panel is constructed to display some data foo, and foo is the only data set, but I forget to check foo in "isActive," then the panel will be unviewable.
- If it is later decided that the panel doesn't need to display some data "bar," but "isActive" is still checking if "bar" is set, the panel will be viewable even when "bar" is the only data set, yielding an empty panel.
Is there a different solution I could use or a way to modify (C) to make it easier to maintain?
Aucun commentaire:
Enregistrer un commentaire