To Interface or NOT for Value Beans in Java
What is a Java Interface
From java.sun.com
There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a “contract” that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group’s code is written. Generally speaking, interfaces are such contracts.
In this blog, I am trying to present very common design pattern in projects, which I kind of don’t agree with.
This pattern is to define Interface for all data beans and value beans. In most of the projects, I have worked in past (and current one too), I have observed, Architects have defined interface for any kind of bean flowing between layers of software.
For example ‘Person’ and associated ‘Address’ bean. So, there will be a Person interface and an Address interface. And there will be ‘PersonImpl’ and ‘AddressImpl’ object implementing respective interfaces. Now Person interface has common getters and setters for properties like name, age, height and Address interface has getters & setters for addressLine1, addressLine2, city, zipcode, country etc. So we can imagine implementing classes have defined class variables for corresponding each getter and setter. Now big question comes to mind, Why we have defined interface for this object? Why not to keep single class Person having getter/setter and it’s class variables.
Common Reasons (and why I think, they are wrong)
- It can have multiple, different implementations. As I have heard from architects, they just want to expose interface to integrating layers/client. And if later on, we change implementation, it won’t visible or require any code change on their side. Well, why we will have different implementations, in first place? These beans have getter and setter of certain property. How come you can have different implementation?
- We want integrating client to write their own implementation. Again, why? It’s simple data bean carrying data around. I think, this is another example of ‘abuse’ of interfaces by architects who are following more of text books version, instead of thinking out of box. Interfaces are contracts of expected behavior of operations, not data beans. Data beans doesn’t have behavior, they are mere payload objects. If they are more than payload, then I would put your design in question.
- To support legacy objects. Argument is, their legacy code of databeans have behavior. Now to bridge to new version of software, they need to use interfaces for databeans. Hence they provide bridged data beans implementing new data beans. And later on, when they move away from legacy, we will just use new implementation of interface. This is clear sign of ‘design’ smell. If you want to bridge to legacy code, then use bridge services objects. Bridged services will take care of handling legacy operations on legacy bean. Your new beans shouldn’t have any kind of behavior, encapsulating or hiding legacy smell. That was main reason to start new project, right?
- So that, we can have multiple inheritance. If your data bean’s getters/setters are not suppose to have different kind of implementation, hence why different objects will implement same implementation. Those different objects rather extend common super implementing class.
- (write in comments, I will add here)
Problems I have faced and I hate it
- Whenever I need to add/remove any property, first you will have to fix interface. Hence doubling my effort in refactoring.
- If same interface is implemented somewhere else, then changing interface (in prev step), I will have to change all other classes too. Question comes, why other implementing class, just simple extends original class. They all are data beans, no behavior.
Only behavior, data beans/value beans (must) contain, implementing equals() and hashCode() methods. Well, we get those as part of Object class. Another possible ‘operations’ can from other interfaces like ‘Comparable’ or ‘Serializable’. If you get gist of it, none of interfaces defined operations as accessors for properties.
In end, I am trying to advocate, to keep java programming simple and productive. Yes, we can boost of designing complex mutli-layered system, but at the cost of productivity and maintainability. Lets keep any kind of operability or logic in business classes. Any logic or code in data beans, is destined for future refactoring some time.
Why I decided to host website using Amazon EC2
Summary
Cloud computing is something, we been listening and reading about from past couple of years. From my perspective, it is virtual machines running on virtualized hardware. Hence giving us dual benefit of running any image on any kind of hardware. We can dynamically launch any image and also dynamically expand/reduce available hardware capacity. Hence the word ‘Elastic’ & ‘Cloud’. In cloud computing world, amazon is more established compare to others. So rest of article will use Amazon EC2. Amazon elastic computing allows user to launch any image (from their growing library of public images). They are called ‘AMI’s. You pay per use of images. (hourly usage rate plans). Hence it makes it easy to choose any image w/o binding to any contract.
Why cloud computing is better than tradition web hosting?
Answer is, it is not. If you looking to run simple apache web server and few php based applications then web hosting companies give best pricing and pre-configured applications. Decision comes, if you looking for better hardware and more RAM for your applications.
Pros, Comparing amazon EC2 smallest image & virtual private server (VPS) plans.
- Amazon EC2 guarantees one CPU core whereas 4-5 VPS images run per CPU core.
- RAM available is 1.7GB for EC2 instance and VPS gives maximum of 512MB to 756MB (for plans >$40/mo) (for simultaneous multiple J2EE web applications, you wish for more RAM.)
- Available capacity is elastic. You can expand it as required, compare to fixed capacity available in VPS/Dedicated plans.
- Once you finished configuring your images, you can make current installation as another image incase you want multiple instances running.
- Amazon EC2 instance are running with better reliable infrastructure comparing.
- If you pre-pay for EC2 account, your average cost of running website 24×7, for whole month comes to $42 something, which is comparable to good VPS plans with lots of memory.
Hence comparing to dedicated hosting, amazon ec2 seems to be better option, if we start comparing relability factor, ability to instantly increase computing capacity, price and many other options.
I decided to give it try to move my website.
Other factors to consider
- 1.7 GB of available RAM and 160 GB of drive capacity, gives me chance to run java apps w/o worrying about available memory.
- It was learning experience for me, (as average user of linux OS).
- It’s easy to install applications on ubuntu image. (same goes for other OS like fedora/centOS).
- I can run MySQL & PostGres DB server together, as required.
- Java tomcat web server seems to be good enough, but I can try some other servers like glassfish or jboss for better experience.
- Using it as subversion server. Ping time & bandwidth is better than other servers.
- Will be using as build server & agent both. (Most probably open source project ‘Hudson’).
Java Products BUT PHP based website ?
- http://www.springframework.org – Running in drupal (PHP based content management site)
- I recently read blog about “Java Parallel Processing Framework”. When I visited their site, again it was PHP.
- And many many others.
- There is no simple content management system (CMS) in JSP world
- There are not enough JSP/Tomcat hosting available.
- We are using 3rd party hosting team, which prefers to use PHP
- There are many tools available in PHP, hence running whole website makes more sense.
Lets try to dissect each one by one.
- OpenEdit
- dotCMS
- LifeRay
- Clearspace
There are not enough JSP/Tomcat hosting available.
- http://www.eapps.com/ManagedHosting/TomcatJSP.jsp
- http://rimuhosting.com/javahosting.jsp
- http://www.hostjava.net
- http://www.DailyRazor.com
- http://www.lunarpages.com
- http://www.javaservlethosting.com/index.php
- http://www.oxxus.net/java-hosting/features.htm
- http://neospire.net/products.services/j2ee.application.hosting/tomcat.php
- http://www.westhost.com/jsp-hosting.html
- http://www.smedia.info/hosting-j2ee-jsp.asp
- https://www.assortedinternet.com/hosting/jsp-servlet-hosting/
- http://www.capital-internet.net/hosting_shared.jsp
- http://www.hub.org/hosting.php
- http://www.kattare.com/java-servlet-hosting.kvws
- http://www.metawerx.net/
- http://www.mmaweb.net
- http://www.visionwebhosting.net/index.html
- http://jsp-servlet.net/Gold.jsp
- http://www.jwebhosting.net/service.jsp
- https://www.visionwebhosting.net/services.html
- http://www.blacksun.ca/hosting.html
- http://www.echomountain.com/ApplicationInfrastructures.html
- http://www.svwh.net/dedicated_hosting.php
- http://www.kgbinternet.com/index.jsp
We are using 3rd party hosting team, which prefers to use PHP
There are many tools available in PHP, hence running whole website makes more sense.
This is one of most important factor, which deciding between JSP and PHP based hosting. I did some research, I found tools which are not great as PHP counterpart, but they solve the purpose. For Forums
- jforum.net
- javabb.org
- http://www.jivesoftware.com/products/jive-forums
For wikis
- xwiki.org
- JSPWiki
- SnipSnap
For Blogs
- Apache Roller
- Peeble
Issue Tracking
- JIRA
- TrackPlus (I just read their announcement on theserverside.com)
Continous Integration Tools
- Cruise Control
- Continuum
- Luntbuild
- Hudson
Wishlist:
Now, I try to look at whole website hosting task at 35,000 feet. Basically, you want to spend minimal resources to get maximum UI experience. So if using vBulletin or phpBB makes my life easier then why I should stick to idealism of using j2ee? I can try to answer this by using these criteria
Q: I am selling/open sourcing j2ee product which is related to web-app development?
A: You should use JSP based hosting. It definitely puts good impression. Even better use your product in that webapp if possible.
Q: I am open source evangelist for java and j2ee but I still want PHP site?
A: Again, you should use JSP. If you can code in PHP, then you can code same functionality in JSP (assuming you are JSP developer)
Q: I need to use a tool which is perfect in PHP or Python (example Trac or vBulletin)
A: In this situation, using tool like Trac or anything else, shouldn’t matter. Basically, productivity matters. I would suggest to use hybrid solution. (Ofcourse SSO is nearly impossible)
References:
- http://www.javaworld.com/javaworld/jw-11-2006/jw-1101-ci.html
- http://www.javaworld.com/javaworld/jw-03-2007/jw-03-bugs.html?page=2
- http://java.dzone.com/news/open-source-web-applications-p
- http://java.dzone.com/news/open-source-web-applications-p-0
- http://raibledesigns.com/rd/entry/building_a_website_with_an
I just read few more user comments. Almost every other user is recommending Drupal for CMS/Portal website. I wish, if something like that comes up in JSP world. If that is made on Spring, it’s even better.
Recent Comments