Let’s do it with an example. Let’s consider a Company entity that has a functionality to retrieve the employees – getEmployees(). A company may have lots of employees so the employees are not loaded when the Company is loaded. But, once the employees were loaded, they are saved in the cache and will be retrieved immediately upon next invocation.
Now I would like to present the list of employees in a list view – GUI.
I would like to provide a ‘loading’ indication, while the employees are loaded from the internet.
But, when loaded from the cache (very fast) it would be an annoyance to present the ‘loading’ indicator and drop it immediately. So, when already loaded, I would NOT want to show the ‘loading’ indicator at all.
How would you tackle this design question? Would you add a method getCachedEmployees()? Perhaps isEmployeesCached()? Perhaps change the method to getEmployees(boolean cache)?
I don’t like any of the above because instead of encapsulating the cache, the cache is emphasized – thus complicating the software.
Any other ideas?
I assume that you have your GUI and code for displaying and managing your GUI elements decoupled from your application logic and data.
I understand that if some action in the GUI triggers the application logic to load the employees, you just want to invoke the getEmployees() method. Your GUI should not control if data is loaded from the cache or internet. But you need a logic in the GUI to decide if the loading indication needs to be displayed.
The best solution I can think of would be that your GUI has something like a timer – you invoke the getEmployees() method, wait for 500ms and if data was already returned you display them directly. If not data was returned so far you need to display the loading indicator. You will need an asynchrous call and callback here, so you probably want to have something like a separate class doing the synchronization here, but it probably will be worth the efford.
I have implented something similar in .NET/C#, where an action is triggered only after 500ms of wait time if special other conditions are also true. When using the application the user will recognize a little bit delay in the reaction of the GUI, but it won’t disturb your overall look & feel.
By the way, I like the approach to have only one getEmployees() method shown to your GUI. This way you make sure that you have a strict separation between GUI and application logic…