Friday, November 21, 2014

How to loop a HashMap or Hashtable in JSP

Though there are number of ways to loop over HashMap in JSP, or any other Map implementation e.g. Hashtable, I personally prefer JSTL foreach tag for this. As a Java programmer, I often have urge to use Java code directly in JSP using scriptlet, but that's a bad coding practice, and one should always avoid that. Infact by smart use of expression language and JSTL core tag library, you can reduce lot of Java code from JSP. In our last post, we have seen example of JSTL foreach tag to loop over List, but not a Map, and that creates a doubt in one of my readers mind that foreach tag doesn't support Map implementation like HashMap or Hashtable as they are not Collection, but that's not true. JSTL foreach tag has special support for looping over Map, it provides you both key and value by using var attribute. In case of HashMap, object exported using var, contains Map.Entry object. Since Map.Entry has getKey() and getValue() method, you can access them using expression language $(entry.key) and $(entry.value), as we will seen in our example. You can iterate over Map to create table of key and value, or any HTML element e.g. <select>, which needs text and value.</select>

Wednesday, November 19, 2014

Strategy Design Pattern in Java using Enum - Tutorial Example

I have said this before that Java Enum is very versatile and can do lot more than you normally expect from it. We have seen lot of examples of Enum in my earlier posts e.g. writing thread-safe Singleton using Enum and 10 ways to use Enum in Java. In this article, we will  learn a new way to use Enum, for implementing Strategy design pattern. Strategy pattern is one of the famous pattern, which takes advantage of polymorphism, to remove switch cases and strive for open close design principle. Formally it encapsulate related algorithm, known as strategy and make them interchangeable. So your Client, also known as Context, can use different algorithm or strategy, without any modification. One of the key advantage of Strategy pattern is it's extensibility, i.e. introducing new Strategy is as easy as writing a new class and implementing Strategy interface, with Enum, instead of creating separate class, you creates a separate Enum instance, which means less number of classes and full benefit if Strategy pattern.

Monday, November 17, 2014

Why Use Interface in Java or Object Oriented Programming

Many times, I have seen questions like why should we use interface in Java, if we can not define any concrete methods inside interface? Or even more common, What is the real use of interface in Java? I can understand beginners asking this question, when they just see name of the method inside interface and nothing else. It takes time to realize real goodness or actual use of interface or abstraction in Java or any object oriented programming. One reason of this is lack of experience in really modelling something real in program using object oriented analysis and design. In this article, I will try to answer this question and give you couple of reason to use interface in your code. If you have good understanding of Object oriented basics e.g. Polymorphism, then you know that it allows you to write flexible code. Interface or abstraction are key to achieve polymorphism, when a caller use interface for calling a method, he introduce flexibility and dynamism in code, as that code will work with any implementation of that interface, not just the present concrete implementation. You will never going to get this flexibility, if you use concrete classes for calling methods, we will see this in more details in next section. Also Programming for Interfaces, is also well recognized and one of the key object oriented design principle for coding. Another use of interface in Java is that, it opens new opportunities for other goodies e.g. design patterns. Lot's of design patterns are heavily relied on interfaces and Polymorphism e.g. Decorator, Composite, Proxy or Adapter pattern, all implements same interface, as there target, and because they are based on interfaces, they can be used interchangeably.