Well the life of a fairly new Java developer is interestingly complex. I guess that is why I love computer programming its like solving little puzzles all day long. Studying the concepts of OO using Java has been very interesting and has given my skill set a new edge. Originally I come from an engineering background and have learned computer programming on-the-job. This technique has gotten me through the last ten years of investment banks, universities and blue-chip companies but it hadn’t left me with a very good design metholody when it comes to applications. I utilised OO rudimentarily, using classes as containers for similar functionality. In the main still running a procedural based programming paradigm where most parts of the application have to know what the other parts are doing for it to work correctly. Highly coupled you might say….
So where am I now? Well through a process of SCJP training, examining example code, watching Google IO sessions, and attempting to program a small GWT application. I am a lot clearer on some aspects, but on others, well this adventure has opened up a great many doors on how to design my programs.
Two design patterns which are paramount in my opinion for good design are Dependency Injection and a MVP architecture? Why you may ask? In short Google uses them and that’s good enough for me.
MVP stands for Model View Presenter and is similar to MVC except in our case the view knows nothing of the presenter, so you can swap out the views for different ones, i.e. a web page or an Android application, and your application still works. As long as your view implements its relevant presenters interface.
As a design pattern this approach appears to be very good and the example Contacts application that Google provides helps clear up any confusion from purely reading the articles. What is interesting with the example is that the core of the application is demonstrated very clearly, allowing other developers to easily pick it up, and event bus and rpc handling is centralised, removing the ugly spaghetti code, that I currently, but have not yet talked about.
At the moment I have developed a very simple CRM application using GWT located at: http://sohocrm.appspot.com. Completing this project allows me to get my feet dirty and by doing so has lead me to these design methodologies which I now have to attempt to embrace and learn. I can feel my brain whirring as I try to understand these concepts.
So MVP is a good pattern because it gives a very clear architecture on how your application works. However in order to clear up more of the spaghetti code we also need to utilise dependency injection. Now I only have a basic concept of this, so please correct me if I am wrong in what I am about to say, but DI is this. When instantiating an object you create additional objects which are utilised in order for the object to complete its class. This might be any object which is declared with a new keyword. What this means is that the current class is dependent on these other classes in order for it to work. The dependency component. Now in order to write modular code and to make testing easier, it would be good to have any dependencies injected into the constructor as an instance when the object is created. That way you can clearly see what classes your objects are dependent on and it allows modular testing to be a lot clearer. I think it applies to only classes which exist within your current project as I don’t think you will be injecting in int's, textboxes etc, but the exact delimination of this area is unsure at the moment – for myself – and I hope to clear that up over the coming weeks. Google uses a product developed by itself called GUICE to do this on the server side java and another called GIN which is a GWT version for the client side.
How did I come across these technologies? Well after building that relatively small application I noticed how spaghetti looking my code was and realised early on that this can’t be the way much larger programs such as Google Wave are constructed so realised, intuitively knew as we all do when we are accomplishing a task we aren’t quite proficient at, that there must be another way.
So the next stage of my application is to re-write it in a MVP style. Then take a look at how I can implement Gin and Guice to build a scalable, understandable and modular application.
Nice summary of current best practices. Not a huge fan of DI yet. I use a bit of IOC (Spring-style), but DI seems to sacrifice elegant code too much at the altar of testability.
ReplyDeleteThanks for the comment. Yes only came across DI last week, after 10 years computer programming, have never come across it, but from an OCD / perfect way of programming it seems very interesting. However I too struggle with the pursuit of perfection with what can actually be achieved in reality without over-engineering everything.
ReplyDeleteGreat post, very interesting.
ReplyDeleteI have found that DI (with lightweight annotation-based frameworks such as GIN and Guice) to make my code a lot more elegant. Dependencies become instantly very clear and it's a breeze to do thing like a server-side and client-side implementation of the a service with a specific interface.
In other words, DI makes it possible to use the factory pattern without all the boilerplate this pattern is usually synonymous with.
Gene, if you're interested in some concrete example of all these technologies (+ history management, UIBinder, internationalization, codesplitting, etc.) then you'll probably want to take a look at the examples provided with GWTP (http://code.google.com/p/gwt-platform/).
Philippe,
ReplyDeleteThanks for the comment. On Friday after a week of getting lost, then found, I needed to get my thoughts down to share. This week I am converting my small project into MVP, as a learning exercise to see what falls out, then adding the GIN/GUICE functionality after that. Should be an interesting week.
I had seen the project in http://code.google.com/p/gwt-platform/ and was quite interested in it. Will this code get replaced by GWT 2.1 when Google release there own MVP classes?
Cheers
Gene
Hi Gene,
ReplyDeleteI've discussed a bit with Ben Alex (who gave the SpringRoo presentation at GoogleIO, I believe) and for the moment, the feature gap between GWTP and GWT's 2.1 MVP classes is a bit too large. GWTP supports a lot more features than what seems to be planned for the first few iterations of GWT's MVP. Some of these features are quite useful too. (See http://forum.springsource.org/showthread.php?p=301397 for a short discussion.)
Since gwt-platform is used in many projects (including mine) we will keep maintaining and upgrading it. I would love to be able to do a "SpringRoo plugin" for it, but it doesn't look like something we can do this at the moment.
Cheers,
Philippe
Philippe,
ReplyDeleteSounds great thats good to know. Thanks for finding that information out. I was at Google IO, first time, was a great event.
I am currently in a period of re-training in Java-GWT, and would love to contribute to the project if I can. I have sent you a message on the discussion forum as a possible start, as documentation is the way to go. I am in the process of building various GWT applications and for my own piece of mind it helps for me to document my progress. So if you think there is an opportunity to help let me know.
Cheers
Gene
Nice blog post Philippe. I haven't started using GIN or GUICE yet, but your post reminded me that I should give it a try.
ReplyDeleteAs far as MVP is concerned, you may find this blog post I did earlier in the year worthwhile. I created this diagram after studying the MVP tutorial and the Contacts app that you refer to.
http://www.nieleyde.org/SkywayBlog/post.htm?postid=37782056-c4e1-4dfb-9caa-40ab9552ca3b
You may also be interested in checking out the new GWT scaffolding functionality from MyEclipse for Spring which generates MVP-based applications for GWT 2.0.4 from your domain model (either db tables, Java Beans, JPA entities).
http://www.genuitec.com/blog/?p=1604
Regards,
Niel
Nice post blog philippe.. Thanks a lot!
ReplyDeleteThis blog post was actually by Gene. Give the man credit! :)
ReplyDeleteI'm in exactly the same situation atm. Learning MVP and trying to learn DI at the same time. It's hard to know what order to try and learn/implement things in.