Category Archives: Code Snippets

Short and usefull code snippets and tutorials used in day-to-day programing

Design Pattern Series – Part Four: Observer pattern

Today I’m adding another important design pattern to the toolbox. Observer Pattern is commonly used and quite powerful. The Observer Pattern defines one to many dependency between objects so that when one object changes state, all of it dependents are notified and updated. We can create a class structure for the pattern ourselves from scratch [...]

Read More →

The Biggest Java WTF

Today one of my co-workers send me the following link. As I couldn’t believe my eyes, I have tested it myself. WTF? 1234567891011121314151617package wtfjava; /**  * This program really HANGS!!!!  * @author kris  */ public class Main {     /**      * @param args the command line arguments      */     [...]

Read More →

Design Patterns – Part Three: Singleton

Today I will describe the Singleton pattern, one of the simplest, yet generating active discussions all over the net. Some people consider it to be an antipattern. We have many different flavours of this pattern, singleton can be thread safe, lazy loaded, eagerly loaded, implemented as a static class or enum. Simple thread safe implementation: [...]

Read More →

Design Patterns Series – Part Two: Template Method

A typical template method pattern consists of the abstract base class which defines one or more template methods (usually set as final) and some abstract methods which the subclass has to implement. Template method defines common algorithm where some of the steps are implemented by subclasses. A Template method pattern provides a skeleton for performing [...]

Read More →

Design Patterns Series – Part One: Strategy Pattern

The Strategy Pattern is a first pattern described in a series of post I’m planning to make regarding this topic. The Strategy pattern allows us to select a specific algorithm at runtime and its one of the simples design patterns to grasp. The scenario described on the diagram below shows a simple usage of the [...]

Read More →

Google App Engine – Querying related table using Key object

This is just a short note on how to query related entities in JDO. As I have found out, we cannot use the entity key as its shown in the Data Store Viewer on the GAE admin console eg. zzR0ZXN0cgkLEgNGb28XGQw and compare that using someRelatedEntityKey == zR0ZXN0cgkLEgNGb28XGQw, instead we just provide the whole Key object [...]

Read More →

Android Series: GET, POST and Multipart POST requests

Today I will describe shortly how to execute http GET and POST request on Android platform. The code is very simple and straightforward, for post request with attachement, you would have to add apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your build path. HTTP GET 12345678910111213try {         HttpClient client = new DefaultHttpClient();     [...]

Read More →

Android Series: Using GPS data

In this tutorial i will show you how to quickly get the gps data using your android device version 1.6+. All you have to do is to implement one interface and acquire LocationMangager from the adnroid system service. 12345678910111213141516171819202122232425262728293031323334353637383940public class LocationExampleAct extends Activity implements LocationListener{     private double latitude;     private double longitude; [...]

Read More →

Android Series: Parsing JSON data with GSON

Today I will describe how to parse json data using Gson open source library and how to convert it at the same time to java objects. This is a typical cookbook example and should be very easy to follow. 1. Create an Android application with one activity, name it as you wish, if you are [...]

Read More →

Getting started with Apache Click and Eclipse Galileo

Last couple of days I read here and there about Apache Click, which is now in version 2.1.0. It looks preety cool and easy to learn, so I decided to give it a try and see for myself. I understand that its cool to learn things using vi and shell only, but when you tried [...]

Read More →