How to organise my code in order to be able to use two different view type? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this questionI have setup my models 开发者_开发问答and now I would like to build the application so that I can easly switch between swing views and html views (2 differents builds).
Could you recommand me any library that let me do that ? or a Controller/View architecture that let me do that myself ?
edit: MVC architecture in java is the point of my question, if I use SWING my controller call the method of a View class to show the results, whereas (at least in python django) the controller returns a "View" class (and something takes care of rooting the resulting view to the user, possibly getting the view from cache directly if it's already there.) which is 2 different ways to implement MVC. I don't know how html views are sent to the user with Java (I suspect that there is different methods) that's why I'm asking the question to get the general idea behind MVC architecture in Java as it may have it's own ideomatic/reference interface to achieve this job.
Oracle's Application Development Framework (ADF) does just this.
You develop your application modules, database entities, and view objects. Then you can drag and drop them onto the page or frame. This allows you to develop the business logic once and keep the logic for handling each view separate.
JDeveloper 11g (the IDE for developing software using ADF) handles AJAX well. It also allows you to declaritively create custom components that can be reused via drag and drop.
The application's visual layout is somewhat abstracted through custom tags, but you cannot, for example, create a web page and then click a button to create a corresponding JPanel. (To my knowledge.)
I don't know of any library, but I can suggest a kind of architecture.
I suggest using (at least) three projects: one for the core functionality, one for the HTML view and one for the Swing. The views depend on the core functionality project, but the two views do not know each other.
You will need some kind of Factory in both view projects, that can instantiate the classes of the selected views using the data from the core projects - if you want to handle this instantiation from the core project, then you will need either a common interface and some kind of dynamic instantiation, or use the same class in the same package in both project.
If you choose a solution like this you cannot use both views in the same time (at least easily :) ).
OSGi with its updated classloader provides support for such scenarios, where your Java projects are OSGi bundles, their dependencies are explicitely set, and the framework can handle these ones. And you can use services to describe the view factories, and then you could decouple these parts in a formal, well-defined way.
I hope the base idea was clear enough.
I'm not sure any library will achieve what you wish. After all, each technology (swing and html) will require different 'view' layer code. What's important here is to keep all business logic out of the view layer and keep it in a control layer which is then used by each view (otherwise known as the MVC architechture, see this link for more info or Sun's more detailed example.
Basically it will come down to your dedication in maintaining separation of responsibilities which will enable you to gain the most reuse out of your code.
I googled for 'swing' and 'ajax' - because I knew of eclipse RAP project, the rich ajax platform which would help if you weren't focussed on swing - and this blog entry looked promising: http://www.javalobby.org/java/forums/t101605.html
The 'RAP' idea is to have one GUI code base and build it either for a standalone client application or a web application (ajax). The blog discusses a different approach: you build a swing application and transform it to a web application.
Hope it helped! (hope I got your question right ;) )
MVC, if followed to the dot can accomplish this! Keep all your business and model layer separate, then have struts/spring controller decide the view. Maintain separate swing and html code. The key again is to remove all possible business logic from presentation layer and making sure your business layer is independent of the presentation layer.
Another option is to just use Swing to display html. If you use JEditorPane or JTextPane and just show html. The downside is that you can't do very sophisticated html using this method. You might also consider using a browser in your app. Here are a couple of possibilities though I have not tried them:
- http://jrex.mozdev.org
- http://lobobrowser.org/java-browser.jsp
- http://www.webrenderer.com/
精彩评论