If I have a class such as ConfigReader which is able to store data from XML or Properties, usually with an initialize() method and getters for every field, where am I most likely supposed to implement it when using a classic MVC pattern?
In my opinion it would be a controller. However, when working with console applications, the configuration could be an essential part to display data in the console, therefore a view as well.
Related
Consider the below screen shot showing a custom application built using Java.
1) In this custom application, we can add People and Cars as shown in above screen in a "View Port".
2) I am trying to write a Plugin for this custom application which does the below
Reads all the Person object in view port
Reads all Car objects in view port
Reads all the attributes of person and car to see if there is a link, and produces
output if there is a link.
Now, i am trying to implement the plugin using below MVC model
Based on this model, i have placed all the view like JPanel, Buttons etc., in view file,
Button click actions in Controller. But when creating Models, i met up with a confusion.
In the plugin i create, i donot have direct access to any of the database tables. Instead the
custom application provides me below functions
- getObjectsInViewPort()
- getObjectType(object)
- getProperties(object) etc.,
Now, how do i design my model? should i just create some method in model which would use the above
inbuilt models and returns some result to my controller which then sent to view for update?
or please provide me how should i do the MVC in proper way in this scenario.
Simply start writing the model providing listeners, concentrate on the controller listening, invoking. Make abstract (mock-up) view classes that trigger events and such. Scenario based, with a "stories".
That would allow to write unit tests that play a scenario.
In a next phase take such a view class and extend it from JFrame or other GUI class. Or embed the abstract view class.
You are on a right track. To build a model, you don't have to have access to database.
1) Build your model
2) Call API functions from your model
3) Get the return result in your model
4) Give it to controller
5) Which in turn will present it to view.
Answer Updated
All business processing functions too will go in your model because this is your business model and not a POJO. Use data layer design pattern where you create POJOs to store your business data(API returned data) and then use them in your model classes to exchange with controllers.
I am working on a struts2 project that has interdependent forms.
I found struts2-conversation, stepped through their simple-example
and understood the conversation mechanism this far (please correct me if I got something wrong):
The Controller is mapped in the struts.xml
It holds the serializable ConversationContext and the Storing-Service
The ConversationContext holds POJOs mapped on forms by naming convention
Now my question is where to put the validation?
In this structure the controller is only one extending ConversationSupport and thereby ActionSupport supplying the validate, prepare and addField- & ActionError methods.
But validating within the controller would mean to validate the whole context, which does not really serve the issue.
I tried validation through annotation within the POJOs, within the context as described above which gives me some NullPointerException as if the context wasn't flushed and I think the xml-validation approach of struts2 is just too stiff. (btw how to let the generated javascripts be minified before being served? And why is there so many options?)
Mark's conversation-interceptor approach had similar problems coming up which's workarounds I didn't really get. Maybe you can help me there.
If you would like to use annotations on your model classes, it works fine with the plugin (as do the other validation approaches).
To validate your model, add #VisitorFieldValidator to the getModel() method in your controller. In the example app, you would then also add #VisitorFieldValidator to the getContact() and getPreferences() methods. Then you can use the validation annotations on the fields you wish to validate.
The service in the example is just there as a simple example of using an injected service in a Struts2 controller and how that can integrate easily with the conversation framework, but it is not directly related or needed (and I would recommend using either Spring, Guice, or CDI for dependency injection in the real world).
The ConversationContext class is intended mostly for internal use by the framework. You should be able to avoid interacting with it by using the annotations and conventions. Unless you simply wish to be adventurous.
To use XML validation in the example app, you would have to change the package name to remove the "struts2" word in order for the Struts2 resource loading tool to load the XML.
I have a Java model which is effectively a tree of Java beans. Different areas of my application can change different beans of the model. When finished, I want to save the model, which should be able to work out which beans have actually changed, and call there
I know I can implement save(), isDirty() and setDirty() methods in all the beans, and have the setter check whether there is a change and call setDirty(). But ideally I don't want to have to programmicatically do this for each setter. I want to just be able to add new properties to the beans with no additional coding.
I'm also aware of PropertyChangeListeners, but again I would have to programmatically fire a change in each setter.
Can anyone recommend a pattern/aspect/annotation that I might be able to use to make my life easier? I don't think what I'm trying to achieve is everything new or groundbreaking so hoping there's something out there I can use.
Note that I'm coding in basic Java, so no fancy frameworks to fall back on (expect Spring for bean management - outside of my model).
Thanks in advance.
If you would like more details please let me know, or refer to the last lines of this question. I've already read a lot and I feel I'm turning something simple into something complicated and I still get stuck here and there, so maybe you can help me in those very specific points.
I'm using Netbeans IDE 7 and JDK 7, and no frameworks. The first Window is a JFrame and all other windows are JDialogs with modal=true.
Questions:
How do I correctly implement the MVC pattern with swing?
From the ideas bellow Which one is better: (A) or (B)? Or maybe another one... Why is it better?
(A)
Main:
MyModel model
MyView view(model)
MyView:
MyController(this, model)
(B)
Main:
MyModel model
MyView View
MyController controller(view, model)
when I click jbutton1 in MainFrame I need it to open the SettingsFrame for editing settings. where should I instantiate the View, the Model and the Controller of the SettingsFrame? In MainFrame Controller?
In terms of MVC organization and implementation, how should I handle more specific features that (apparently) lacks one or two of the MVC "legs" (either Model or View or Controller)? Should I create empty classes for them?
a. The implementation of a TrayIcon
b. A URL connection class (an HttpsUrlConnection which will update data in the main jframe and also upload/download files)
c. A Directory Monitor (which will update data in the main jframe and also use the urlconnection to download a file)
d. My own implementation of TableModel
e. json
How to correctly keep and use an object with settings through the whole application? I will need it's information in different places (Views, Models, Controllers) but it might be altered by user during the runtime). Is it a good idea to make this model a singleton?
What should I do when:
a. View needs some data from the Model?
What I'm doing: using the reference of Model which I keep in the View
b. View needs some data from the Controller?
What I'm doing: using the reference of Controller which I keep in the View
c. Model needs some data from the Controller?
Still didn't happen but I have no idea how to do correctly
d. Model needs some data from the View?
What I'm doing: pulling all my hair from my head...
e. Controller needs some data from the View?
What I'm doing: using the reference of the View which I keep in the Controller
f. Controller needs some data from the Model?
What I'm doing: using the reference of the Model which I keep in the Controller
g. One of FooModel, FooView or FooController needs data from one of BarModel, BarView or BarController?
What I'm doing: thinking of jumping from the highest building...
Any hints on how to know if I implemented MVC correctly? Should I process massive data in Model or Controller?
I'm also using a DAO, what I'm doing is: my model has a
ArrayList MyModel load()
method which creates an instance of the DAO and returns the ArrayList of Models returned by the DAO, and then sometimes I process this ArrayList of Models in the Model and sometimes I allow the Controller to process it. Is this a good practice or is there a better way? By Process I mean: iterate through the ArrayList and get the data from the models.
I Have a PasswordCheck jDialog to restrict access to some Views. How can I reuse it in terms of MVC, so that I can use the same PasswordCheck dialog for allowing/restricting access to different Views without doing a mess in the code?
Any other tips, hints, ideas, suggestions?
Context:
I'm required to develop a Java Swing MVC software in a short time, although by default I'm not a Java developer and not so used to implement the MVC pattern, specially in Java (I get the idea but sometimes it lacks me knowledge to implement the relationship between classes).
The applications is basically a monitor for local/online files with a JTable in the main frame to show this data. I'm using the new WatchService API to keep track of the local files and saving their info in a h2 database using a DAO, and them reload this data in the main frame jtable. I must also notify the user about new files (which for I'm using TrayIcon). For the online files monitoring/uploading/downloading I'm using HttpsUrlConnection and json. It may also allow Settings customization.
Thanks in advance for you time and help.
Have a look at Sun's (Oracle's) suggestions.
As one simplification, you could have each component (model, view, controller) register with a top-level application component to provide a single point of reference rather than individual references between each component (your A or B). The article I cite provides ideas for push and pull design; I'd recommend push as a more popular modern approach. Disclosure: I have experience with Java and MVC but not MVC in Swing per se.
where should I instantiate the View, the Model and the Controller of
the SettingsFrame?
Sure, yes, or in a top-level application component.
how should I handle more specific features that (apparently) lacks one
or two of the MVC "legs" (either Model or View or Controller)?
I would implement GUI-only pieces as your own GUI library. And purely algorithm/service pieces as a services library.
Should I process massive data in Model or Controller?
Data processing algorithms would fit nicely in a controller or even service library; your model should not do much processing at all beyond possibly data type conversion or validation.
How to correctly keep and use an object with settings through the whole application?
See my note on registration; a singleton may be appropriate.
I was wondering if Spring has a properties files mechanism similar to Struts2 where it looks for a properties file first in the same package as the controller and then moves up the package structure until it finds the properties file or property.
I want to define a property file per controller, but I rather not have to wire them up together, if possible. Is there some convention that can be followed that would associate the properties file with the controller? The properties file resolution should also work correctly when resolving locales.
For example, if I define a property called "title" in several prop files, I want the correct one to resolve in the JSP based on which controller handled the request.
ControllerA RETURNS ViewA USES PropA.title
ControllerB RETURNS ViewB USES PropB.title
I was successful in auto wiring a Property file to a controller's Model attribute and display values in JSP. I was also able to specify a ResourceBundleMessageSource in the configuration and then display values from it in JSP.
Out of the box, not that I know of.... but you can easily write one, if that's what you really want!
Spring has things like BeanFactoryPostProcessor which lets you do things like this with your BeanFactory/ApplicationContext. I'm thinking BeanFactoryPostProcessor would fit the bill here - you could 'post process' a bean by looking for a properties file on the classpath, grabbing the properties, and applying them to the bean.
I'll say this though - Spring is usually meant to be fairly non-invasive. If you want something like this to become part of your design, you might want to think of a way to do this in pure Java, rather than using Spring. For example, create your own Factory and implement and unit test it separately. Require your app to use this Factory to get your business objects.
In other words, you can probably do it in Spring - but it isn't always the best approach. It does sound like it could be the most convenient, in your case, but I haven't see the details of what you're setting and where. Just food for thought..