Skip to main content


Showing posts from 2014

Object cloning in Java with example

In this post, we will discuss following items. What is object cloning in Java? How to implement cloning in Java? What is Cloneable interface and its main flaw? Shallow Cloning. Deep Cloning. Alternate to Cloning. What is object cloning in Java? Object cloning is the process of creating exact copy of that object. To do this, there are two pre-requisite in Java: 1) implement Cloneable interface 2) and, override the clone() defined in java.lang.Object class. The clone() is defined in java.lang.Object class and have protected access by default. You need to set its visibility to public by overriding it, so that you will be able to call this method from outside the class overriding it. General contract of clone() Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression: x.clone() != x will be true, and that the expression x.clone().getClass()

Open Close Principle with example in Java

Open close principle Bertrand Meyer coined the term open close principle in his 1988 book Object Oriented Software Construction . Software entities like classes, modules and functions should be " open for extension but closed for modifications ". It is a generic principle. You can consider it when writing your classes to make sure that when you need to extend their behaviour you don't have to change the class but to extend it. When referring to the classes Open Close principle can be ensured by the use of Abstract classes and/ or Interfaces and concrete classes implementing their behaviour. This will enforce having concrete classes implementing Abstract classes/ Interfaces instead of changing them.  Some particular cases where this principle is used are Template Design Pattern, Strategy design pattern . We want to draw different kind of images. For this, We wrote a generic class ImageEditer which can draw shapes. See the below code snippet. package javawi

Single Responsibility principle with example

Single Responsibility Principle Single responsibility principle was introduced by Tom DeMarco in his book " Structured Analysis and Systems Specification, 1979 ". Robert Martin reinterpreted the concept and defined the responsibility as a reason to change. A class should have only one reason to change. In this context, responsibility is considered as reason to change. This principle states that if we have two reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and on future if we need to make one change we are going to make it in the class which handles it. When we need to make a change in the class having more responsibilities the change might affect the other functionality of the classes. Let's see this by example. package com.gauravbytes.srp.parser ; import ; class FileParser { public void parseFile ( File file ) { // parse file logic for xml, csv, json

Introduction to Software Design Principles

What are Software design principles? Software design principles represent a set of guidelines that helps us to avoid having a bad design. Associated to Robert Martin who gathered them in “Agile Software Development: Principles, Patterns, and Practices". According to Robert Martin there are 3 important characteristics of a bad design that should be avoided: Rigidity:  It is hard to change because every change affects too many other parts of the system. Fragility:  When you make a change, unexpected parts of the system break. Immobility:  It is hard to reuse in another application because it can't be disentangled from the current application. What does SOLID stands for? S: Single responsibility principle . O: Open close principle . L: Liskov's substitution principle. I: Interface segregation principle. D: Dependency inversion principle.

Indexing and Searching with Apache Lucene 4.7 with Example

This article is about Indexing and Searching documents with Apache Lucene version 4.7. Before jumping to example and explanation, let's see what Apache Lucene is. Introduction to Apache Lucene Lucene is a high-performance, scalable information retrieval (IR) library. IR refers to the process of searching for documents, information within documents, or metadata about documents. Lucene lets you add searching capabilities to your application. [ ref. Apache Lucene in Action Second edition covers Apache Lucene v3.0 ] The main reason for popularity of Lucene is its simplicity. You don't require in-depth knowledge of indexing and searching process to get started with Lucene. You can start with learning handful of classes which actually do the indexing and searching for Lucene. The latest version released is 4.7 and books are only available for v3.0. Important note Lucene is not ready-to-use application like file-search program, web-crawler or search engine. It is a software t