Today, i will write some tiny-tutorial how to create a portable REST using Spark MVC Framework, http://sparkjava.com/. Spark is very lite and would be a good choice -and an alternative of SpringMVC- if you are developing a small project of MVC/REST.

Create your Maven project and add this dependency. Notes here, i’m using Java 6 for my development and i’m too lazy to point to Java 8, so i’m using the version 0.9.9.3-SNAPSHOT of Spark. You are welcomed to use the newest version of Spark. Please refer to http://sparkjava.com/download.html.

We can see the interval difference between primitive and wrapper on this example is quite huge ~ 7500ms . But, unfortunately, many developers today didnt and dont aware of this, because of the auto boxing-unboxing features since Java 1.5, the process is hidden on the background.

Well, my colleague and I, were having this discussion before. He was a bit confused with best practice (in performance matter) to declare the variable, whether it’s as a member, inside loop or outside loop.

As we can see, there is no significant difference with where did you place the variable – at least for this test cases. Thanks for the magic of JIT feature that Java has on it, so the optimization can be done at the runtime of the application and made this achieved.

Of course, this cant be taken generally, especially for non-primitive data type, String, Thread, etc. And depends of what operations that you do for that Object.

I had a job to check our project code quality. And have to report it back to my team leader for any obstacle that i found in the project. I found a lot of leaks and i think would be good to be discussed on the blog. Not to mock the author, but to learn and improve ourselves together.

The code works fine, and do its job pretty well. But some problem will appear by using this code-style. This class will grow tailing the biz changes, as usual, the bigger one class, the “merrier” to maintain it is. And most likely this class, will be having more than one purpose, can be called low-cohesion.

Better OOP Approach

Well the better approach for the case above would be using the Factory Design Pattern. We can let the factory of READER to generate every single instance according to their type. It would be easier to grow the instance type, since we just need to create a new class and do a little modification in the Factory class. The caller class, wont grow and will stand still at its current shape.

With the Factory Design Pattern to handle this case, we can achieve some benefits,

Specifying a class for one task, means, easier to maintain since one class is for one purpose only (modularity/High Cohesion). i.e: Operational Reader is only to read data for Operational only, no other purpose. Just in case, one day in the future we need another Reader (say: NonOperationalReader). We just need create a new Class that extends (or implements) the InfoReader class and then we can override our own readInfo() function. This Caller class will have no impact. We just need to do some modification in the Factory code (and the Enum of course.

Higher Reusability of Parent’s Component (Inheritance): Since we have parent class (InfoReader), we can put common functions and thingies inside this InfoReader class, and later all of the derivative classes (ExecutiveReader and OperationalReader) can reuse the common components from InfoReader . Avoid code redundancy and can minimize coding time. Eventhough this one depends on how you do the code and cant be guaranteed 😀

But, It’s Run Perfectly, Should We Change It?

Obviously the answer is big NO. This is only the case study and for your further experience and knowledge. OOP is good, do it anywhere it’s applicable. But the most important thing is, if it’s running, dont change it. It would be ridiculous if you ruin the entire working code just to pursue some OOP approach. Dont be naive also, no one can achieve the perfect code. The most important is we know what is the better approach.

This blog has some modifications and improvements caused by some comments in here. Personally, thanks for great input so i can keep the quality of this post.

We Java developers, known 4 access modifiers in Java: private, protected, public, and package. Well, except for the private, the last three, can be called from outside of the class by inheritance, same package or from the instance.

Now, the common question, can private be called publicly (from outside class)? well the answer is NO and YES. No when you use ‘usual’ way to access it, and YES when you ‘hack’ into it using the Reflection API provided by Java itself.

Well okay, now just write the code that we will hack into. I called it as “TheVictim”

Now after that, just follow my code and try to run it. I guarantee that if you followed it right, you will get TheVictim to call both of the hackTest and hackTestStatic. And you can see the output on your screen.

Okay, this tutorial has met its purpose. Now you know the Reflection API of java is very powerful feature of programming language. And it’s all up to you to modify or even extend it for your own purpose. Have fun with Java 🙂

I had a job to design and develop a new framework for my own development team. We made the decision that we will adopt the Struts 2 as my MVC framework.

Yes i see the benefit from Struts 2 very well, easy to implement and has so many features. Until, my team member said “Hey, i hate Struts very much, Because i have to put many exception-handling and/or debugger inside many events/methods. Can we use another framework? or just use the servlet mixed with the scriptlet?”

Well i know his concern, it would be easy to read and maintain the code with their exception-handling when your code was still small, but along with the time, you will have your own trouble to maintain the code to stay in a good shape, believe me, i have had this kind of experience before.

So, i think and try to find a better solution and lucky for me, i found the much better solution with the Task-Driven approach. Maybe for some of you this approach will look like a tricky, but no problem as long as my team could gain a benefit from it and keep the code stay in healthy shape :).

Well, as a brief introduction for Struts 2. This Action code,perhaps, is the common way that almost every single Struts 2 developer will do. As you can see, you can have many methods that indicates as an “EVENT”

As you can see, i have the events/methods execute(), save(), fetch(), and delete().

and you can see on the above code, that you have to do the exception-handling four times, one each method. For me — and team — one is more than enough, easy to read and maintain the code, especially when exception occur.

Now, my approach is to convert the familiarity of the event-driven into the task-driven. So, i just have to add a new parameter that i gave name task, as a flagging parameter. In here i will have only 4 tasks to subtitute the 4 methods, “execute“, “save“, “fetch” and “delete“.

Implementing the code is easy, just put one extra attribute on the Action code, i named it task.

And on the JSP, only add a minor change, and i will give you the comparison between the event-driven and task-driven.

As you can see the difference between those 2 slices of code. I dont know how about you, but i love the task-driven Struts more than the event-driven. Maybe it’s relative, the answer could be different for another developer, but the most important for me is now my developers are happier to use Struts.

These several months, has been tremendous year for me and my NamingException blog. Last year, around October 2011, ive been asked to be contributor of Java Code Geeks, and not only that. According to my private email with JCG founder, it turns out i’m the only and the first Indonesian Contributor at that time.

And now, in March 2012, ive been asked by Curator of Dzone.com for them to repost my blog content at their JavaLobby lounge and got an offer to join their Most Valuable Blogger (MVB) program. Of course, i accepted that offer.

Too bad, i havent updated my NamingException yet for several months, not because i dont want to, and im not too busy too, but honestly i have no idea about new and quality topic. I dont want my blog only act as “reposter” site, just to fulfill my post-target, for me, quantity is important but quality is still at the top.

If you have any idea to be discussed about, please let me know and help me to make this NamingException better and better in quality. I expect no commercial reason in here, all of my post are always and always will free and open-source. Never did, never do and never will about money. Let share about our skill and experience, from community, by community and to community. Let the benefit come to us by itself 🙂

Recent Posts

Archives

Archives

Categories

Categories

The Author

Ronald Daniel is software developer, specialize in J2EE Programming and its technologies and frameworks.

Has been in this IT World since 2008, still many to learn yet. Been a trainer for a while, specialization in training subjects: Java, J2EE, Sql-ing in Oracle, Sql Server and MySQL. Has been involved in numerous IT Software Development Projects and still want to be the better than yesterday.