You can use proxy pattern in your program, if you don't want use real object for some reason. For example, creating a real object is expensive or real object is in the remote machine. The key of proxy pattern is proxy object and real object implement the same interface and proxy object hold an instance of real object.
Flyweight pattern help you use as less memory as possible in your program. Let's say you want print a word (helloWorld) in your program. You can define 10 characters one by one (h, e, l, l, o, W, o, r, l, d). I think you certainly won't do this. When you define a string in java JVM won't new it directly it will check if this string exists or not. If this string is not defined then create a new one otherwise return the exist one. This is flyweight design pattern.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.