CREATIONAL
Singleton
This design pattern is to have only once instance of class for the whole application running on the JVM. This can be done via static keyword in java. In java, each static variable is only one to the java classloader of JVM executes java application. The most popular datasource is implemented singleton pattern.
Factory method
We have different instances of class at runtime by passing parameter. This design pattern is used in almost technology or frameworks, servlet and servlet url mapping for example. Simple example to describe this design pattern: a person can do a task, but by a way not such a way other do (teacher teaches, student learns). So we can make Person is abstract class, which has doTask as a behavior. Then we create two classes: student, and teacher implements Person, and overriding method doTask. Now we have differences implementations of person. On the factory, we can return a teacher object, or student object depends on the input parameter.
Abstract factory
The same factory method, but it is abstraction for factory method.
Prototype
This design pattern is to copy an object with lower cost. There are two type of copy: shallow copy and deep copy (read here: ….).
Builder
Builder pattern is creational it means it solves problem of creation. Constructors in java are used to construct object, there are some mandatory and also optional parameters. We can use builder pattern to construct object with much more simpler, no missing in the order of parameter, no missing on assign value to what parameter we want.
STRUCTURAL
Adapter
This design pattern is used so two unrelated interfaces could work together. The joining between them called adapter. This pattern is useful when working with third-party library. A very simple example is WindowListener, and WindowAdapter.
Bridge
This design pattern is to separate out the interfaces from its implement. For example, we have an interface, but two classes can implement it in different way.
Façade
Simple interface/class represents an entire subsystem. With this pattern, we can hide the complexities of the system.
Composite
A tree structure of simple and composite objects. It can be an individual object or can be a collection also. We can use vector, arraylist… in collction framework.
Flyweight
This pattern is to avoid creating large number of instance at runtime.
Proxy
An object representing another object.
BEHAVIORAL
Observer
This design pattern is a way to notify number of classes. This can be easily if you concern to Observer class in java. You can also self-writing implementation of Observer design pattern. Example: Student after joined a course, he/she will receives all the information from the course info, or stops receiving information after the course done, or deregister the course. Therefore, you can design Student class contains collection of observer, and register/deregister an observer to add/remove observer into/from collection. In the Student class, you also specific notify with call all the observer’s action.
Chain of responsibilities
If you have worked with Java Servlet before, you will know how to Chain of responsibilities pattern works after used ServletFilter. Sometime, we can call this design pattern as wrapper pattern. A simple example to demonstrate this design pattern: you can create a class with need to parse XML, Text plain, CSV… Normally, each time you parse the data file, you will need writing each own parser for the specific. There an abstract way is, you create a handler for each type of file, then process in other method of other class.
Strategy
Encapsulate algorithms inside a class.
No comments:
Post a Comment