Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Comparison of Maps in Java Collections Framework

March 26, 2015

Maps, Hash tables(in general sense), Dictionaries whatever you call them, in every well known programming language there are similar data structures. In java we have Maps as a part of the collections framework.
Although Maps do not implement Collection interface they are still one of the most important part of the framework. Java SE library provides several implementations of Map data structures. Of them, some of the commonly used are HashMap, LinkedHashMap, TreeMap, etc.

Note that the following diagram corresponds to the java 7 & 8 collections framework and it is an abstract view of the relevant classes and interfaces. AbstractMap class which is implemented by several concrete classes has been omitted.

Method Overriding with Exceptions in Java

March 17, 2015

Understanding method overriding with Exceptions has made some confusions in my mind. So I tried to list all the possible cases in terms of declarations of exceptions in super class method. The list of 3 main cases are as follows.

Case 1: Super class method declares NO exceptions

Here there are only 2 possible sub cases.
  1. Sub class method does not declare any Exceptions. This is the most obvious one since there are no Exceptions involved. So I'll omit the code sample.

JSP Basics

December 30, 2014

Java Server Pages or JSPs are very similar to typical html pages except they are embedded with java code. This simplifies the development of dynamic web applications.

How JSPs are related to Servlets

JSPs are basically an abstraction to Servlets. Before compilation, JSPs are translated into Servlets and then compiled by the servlet container.

JSP Lifecycle

  1. JSP is translated into a Servlet (creating equivalent Java source code for a servlet) and Compiled by the servlet container.
  2. An instance of the corresponding Servlet class is created and initialized by the by calling jspInit() method.
  3. This servlet object stays in the memory and for each request _jspService() method is invoked, passing the request and response objects.
  4. When the container needs to remove the servlet instance from service jspDestroy() method is called.

JSP Syntax

Abstract Factory Pattern in Java

June 5, 2014

Abstract Factory pattern, another GoF pattern which falls under the category of creational design patterns, deals with a factory of factories (sometimes called a "super factory"). Main intent of this pattern is to instantiate families of related objects without exposing their concrete classes to the client and thereby reducing the complexity when dealing with large systems.

Factory Method pattern may seem quite similar to this except Abstract Factory deals with multiple families of related products where as in Factory Method pattern we have only one main product.

Basic Structure of Abstract Factory pattern



Observer Pattern in Java

June 1, 2014

When it comes to programming it is quite common to hear the terms like "Event handling and listeners". This is indeed one area where Observer design pattern is extensively used.  Observer is a GoF design pattern which falls under the category of Behavioral patterns. These types of patterns describe the process of communication between objects.

Let's take look at an example where messages are saved to a database as it receives and the user is notified whenever a new message is received. Before I learned Observer pattern, the first thing which came to mind to solve this is to monitor messages using Threads and when it comes to multi-threading things get messy but this pattern involves no threading mechanism at all.

Factory Method Pattern in Java

April 25, 2014

Factory method is a GoF design pattern which is widely used. The official definition says
The Factory Method Pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate and Factory Method lets a class defer instantiation to subclasses.

Basically Factory method pattern provides the solution for what the Simple Factory couldn't achieve as  I mentioned in the previous post, ie. fulfilling the Open-Close Principle. This is achieved by providing an interface with a method for creating objects between Client and Concrete Creator classes. This particular method is known as the "factory method". (Here the interface I'm referring to is from a design view point, technically it can be a class or an interface in java)

Simple Factory in Java

April 23, 2014

Factories are used to handle the details of creating objects. Formally put, factories are used to encapsulate object creation. There are 3 variants of implementing factories namely Simple Factory, Factory Method and Abstract Factory.

Although Simple Factory is not an official GoF design pattern it's still worth to know since It'll be useful when understanding Factory Method pattern and Abstract Factory which will be explained in the next post.

Singleton Pattern in Java: Best Practices

March 11, 2014

Creational patterns such as Singleton provide mechanisms for object instantiation. The purpose of the Singleton is to ensure that only one instance of a particular class is created and provide global access to it.

There are many opinionated views about the approaches of  implementing Singleton among developers in terms of efficiency and java memory model. The way I see it there are 4 main approaches of implementing this pattern in java which are both efficient and thread-safe.

1. Early/eager initialization with a static factory method
2. Lazy initialization with double-checked locking
3. Initialization on demand holder
4. Enum singleton