wood burning stoves 2.0
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes do we realy need to overweight code with patterns? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "do we realy need to overweight code with patterns?" Watch "do we realy need to overweight code with patterns?" New topic

do we realy need to overweight code with patterns?

Gytis Jakutonis
Ranch Hand

Joined: Feb 02, 2004
Posts: 76
I have noticed two patterns, heavily used in this forum - Factory and MVC. Do we really need them?
1. Factory pattern. Consider following architecture: Business interface with methods like book() and search() is implemented as local(for alone mode) and remote(for cleint mode) objects. Is it worth to have 4 classes(BusinessFactory interface and 3 implementations for each mode) instead of 3 static initializers? In either case we need to check for input argument(mode) and either call static initializer or instantiate appropriate factory. And what do we win from the latter approach? Instead of passing Business instance to GUI(or whatever) constructor, we pass factory isntance.
2. MVC pattern. IMO we need to implement it for main GUI since it is indirectly required by Sun. But how about those connection dialogs(host, port) - do we really need MVC for them? Take a look at JFileChooser - it has no MVC and it's ok, since it is too simple.
Conclusion: IMO design pattern is only a tool to build smth, not a goal itself.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11776

Hi Gytis,
I agree that design patterns are a tool which may be used, not a requirement. There will be times when you will look at a problem and the obvious solution will use a particular pattern. There are other times when the solution is similar to a particular pattern but requires slight modifications (look at how many variations of MVC exist). And there are times when forcing usage of a pattern will add complexity, confuse the issues, and gain nothing.
Using design patterns can help with how you think about a problem, how you explain it to someone else, and how easy it is for someone else to maintain (or mark) your code.
But if the pattern does not meet your requirements, don't try and force it.
Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Ian Roberts
Ranch Hand

Joined: Aug 20, 2003
Posts: 46
I agree with Andrew about not forcing a solution around a pattern.
Patterns, used correctly, are not heavyweight and the MVC pattern is used within the JTable as the default structure. Not using the MVC pattern in the JTable would mean that you have created your own table structure rather than reusing the Sun API. MVC is a very diverse pattern in that it is used as an architecture level pattern and application level pattern. How and where you use it (if at all) depends upon your solution.
So long as you have detailed your decisions extensively in your documentation and have good reasons why you have diverted away from the MVC, which was a requirement in my assignment, Sun should judge it fairly. However, your reasons will have to be very good as the MVC is the standard Sun approach to architecturally solutions.
Good luck.
I agree. Here's the link: http://aspose.com/file-tools
subject: do we realy need to overweight code with patterns?
jQuery in Action, 3rd edition