Let me say I have a simple Java program, which reads some files and displays some data.
I am confused, I think a pattern should be applied but I can't decide which one to implement.
Here is the scenario:
Whenever I choose a directory I want my program to display the directory name, all the files list and other data (like file sizes, file extensions etc.) Whenever I click a filename I want my program to display its' path, preview it if it is an image file and some other stuff.
The program would keep keep the data in a dataHolder class: File selectedDirectory, List filesInthatDirectory, File selectedFile, JSONObject fileData etc...
That program would have many GUI objects for display; textfields, panels, labels, lists.
Classic old approach would be writing all the code in a single method *press a button -> inside the actionListener *get files from the disk, *read their names and other stuff, *fill the GUI objects with data. This is kinda bad.
So I decided to use a design pattern. Then whenever I click this button and read from the disk, I would only update the dataHolder class (setSelectedDirectory, setFileData, fillCurrentFilesList.. methods like these) And these update operations would trigger classes which will do the needed GUI updates.
But I stayed between two approaches;
-With Observer pattern, I create some observers, for example when dataHolder's fileData (JSON) object is updated I would notify related textFields, then they will display the proper data.
-But with Visitor pattern, a visitor class will handle different objects, it would run different codes for different GUI classes. JList will try to display a List or some textFields will try to parse a JSONObject and display only the related String. But I do not need much different functions, I mean I only want the GUI objects to display some data, my only visitor class would be a DisplayVisitor which will expect the GUI display object's only job - display the data. If I needed doSomethingVisitor, doAnotherThingVisitor, doAmazingThingVisitor... classes that approach would be nice but I don't need another functions.
Do both approaches do the trick? I just can't get the difference for this scenario. Both of them offer a solution, Observer is more simple but does it offer handling different GUI classes like filling a JList with Arraylist or making a JLabel displaying an jpeg File?
Or Can I combine these patterns? Thanks for reading.
Aucun commentaire:
Enregistrer un commentaire