Java composite pattern example

Composite pattern is used to handle a group of objects in the same way. Let's say we have two different kinds of File. The one is TxtFile and the other one is Directory. In composite pattern we treat them as File in the same way. The code is like following.

Java facade pattern example

Facade pattern is very easy to understand. Let's say you have to call different subclasses to implement a feature. You can use a facade class to hide the detail of bottom layer. The sample code is like following.

Java decorator pattern example

The aim of decorator pattern is adding new features without creating subclass. Subclass cannot add multiple features flexibly and dynamically. Decorator pattern can solve these problems. Let's say you want make special pie with different material.

Java adapter pattern example

Let's say you have an interface named "NewInterface"; you have two classes which implement the interface. Client will loop the list of NewInterface. The code of client is like following.

Maven build multiple projects without inheriting

If you build root maven project the children project will be built automatically. In this page I will show you how to build multiple maven projects without inheriting. Let's say you have multiple maven projects like following.

Design pattern tutorial

A tutorial about common design patterns. Using design patterns in your project can help you save your time and make your code clean and efficient.

Java bridge design pattern example

Bridge pattern is useful for add a common property to a series of objects. Let's say we have a series of vehicles; like car and train. Different vehicles has different kind of wheels. It is easy to change wheels of vehicles after making wheel as a property of vehicles.

Java lazy initialization example

Lazy initialization pattern is similar to lazy initialization singleton. The key point of lazy initialization is "no need no create". The sample of code is like following.

Java prototype pattern example

Sometimes creating a new object is expensive, you need use "new" keyword and set properties. Prototype pattern is a good way to create the instance without "new" keyword. You can change the value of properties when needed after cloning object. Example code is like following.

Java singleton pattern example

Singleton make sure only one instance in your program. There are two kinds of singleton: initialization first, lazy initialization. The first one initializes class in the beginning. The second one initializes class when getInstance method is first invoked. The codes of them are like following.

Java builder pattern example

There are two kinds of builder pattern on the internet. Both of them separate the construction of a complex object from its representation. In this page I will show you how to use them.

Java factory method example

Factory method pattern is used to create different "product" when you overridden the create method in subclass. Your business logic code is handle the "product" which created by overridden method. The diagram of it is like following.

Java abstract factory example

Abstract factory is one of the most popular design patterns. You can upgrade your program by adding concrete factory and without modifying the code. In this blog I will show you how it works. The diagram of it is like following.

Java comparable and comparator example

There are two ways to sort multiple objects. Create a Comparator or make objects implement Comparable interface. In this blog I will show you how to sort multiple objects.

Java maven thrift example

In last blog I have show you how to use thrift in java. You may found that you have to use command to generate class and move it into your package. Today I will show you how to use maven generate thrift class. It is easy and efficient.