Java Design Pattern: Template Method

The Template Method design pattern defines the workflow for achieving a specific operation. It allows the subclasses to modify certain steps without changing the workflow’s structure. The following example shows how Template Method pattern works. Class diagram Java Code Vehicle.java defines a vehicle and hot it works package com.programcreek.designpatterns.templatemethod;   abstract public class Vehicle { … Read more

Open Source Projects Using Spring Framework

Spring framework is difficult to learn, especially when you want to develop a real project with industry standards. Reading tutorials is a good way for learning at the beginning. But finally, you still need to read code from real projects. Luckily, there are some very good open source projects that use Spring framework. Here are … Read more

Interview Question – Use Java Thread to Do Math Calculation

This is an example for showing how to use join(). Interview question: Use Java multi-threading to calculate the expression 1*2/(1+2). Solution: Use one thread to do addition, one thread to do multiplication, and a main thread to do the division. Since there is no need to communicate data between threads, so only need to consider … Read more

Convert verbs to base form by using Wordnet in Java

A verb can occurs in a sentence in various format such as base form, past tense, past participle, 3rd person singular present, etc. To have a general idea of how each word occurs, we often need to get base form of a word. This examples shows how to convert a verb to its base form … Read more

Spring Framework Tutorial – Hello World

Latest Update: 2014/05/21 This post shows how to build a Spring Hello World application. The following are all steps required to make a Spring hello world program. 1. Set up development environment IDE: Eclipse IDE for Java EE Developers. The latest version is Eclipse Kepler (4.3.2). Here is the one you should download: 2. Create … Read more

Insert/Add Statements to Java Source Code by using Eclipse JDT ASTRewrite

This belongs to Eclipse JDT Tutorial Series. This code example is for inserting a statement to existing Java source code by using eclipse JDT. It works inside of a Plug-in. For simplicity purpose, the following code simple add a method invocation statement called “add” to the first line of a method. public class Main { … Read more

Eclipse JDT Tutorial: Find All References of a Method

When using JDT, you may want to find all references of a given method and then do something with the references. This can be done by using method JDTSearchProvider.searchMethodReference(). The following is sample code: // First of all, get all the type declaration of the class. IType [] typeDeclarationList = unit.getTypes();   for (IType typeDeclaration … Read more

Java Design Pattern: Decorator – Decorate your girlfriend

Decorator pattern adds additional features to an existing object dynamically. In this post, I will use a simple example – decorate your girlfriend – to illustrate how decorator pattern works. 1. Decorator Pattern Story Let’s assume you are looking for a girlfriend. There are girls from different countries such as America, China, Japan, France, etc. … Read more