Design Patterns

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1.

SINGLETON

final class SingletonClass {


private static SingletonClass instance;
private SingletonClass(){
}

public static synchronized SingletonClass getInstance() {


if (instance == null) {
instance = new SingletonClass();
}
return instance;
}
}

2. PROTOTYPE

● Specify the kinds of objects to create using a prototypical instance, and create new
objects by copying this prototype.
● In general, creating a new instance from scratch is a costly operation. Using the
prototype pattern, you can create new instances by copying or cloning an instance of
an existing one. This approach saves both time and money in creating a new
instance from scratch.
● Example: Object.clone()
● Implements Cloneable marker interface
● Example scenario: We need to get some data from DB and make manipulation on
that. Instead of querying the DB every time, we get the data in an object and then
clone that object to make manipulation on it.

3. BUILDER

● The builder pattern is useful for creating complex objects that have multiple parts.
● Separate the construction of a complex object from its representation so that the
same construction processes can create different representations.
● If you need to make a complex object that involves various steps in the construction
process, and at the same time, the products need to be immutable, the builder
pattern is a good choice.

4. FACTORY METHOD

● Define an interface for creating an object, but let subclasses decide which class to
instantiate. Factory method lets a class defer instantiation to subclasses.

5. PROXY

● A proxy is basically a substitute for an intended object. Access to the original object
is not always possible due to many factors. For example, it is expensive to create, it
is in need of being secured, it resides in a remote location, and so forth.
● A proxy is an intermediary that acts as an interface to another resource.
● Proxy is heavily used to implement lazy loading related use cases where we do not
want to create full object until it is actually needed.
● A proxy can be used to add an additional security layer around the original object as
well.
● The objective of a proxy object is to control the creation of and access to the real
object it represents. A common use of a proxy is to defer the cost of instantiating of
an object (that is expensive to create) until it is actually needed by clients.
● Since you are not directly talking to the actual object, it is possible that the response
time through these proxies is longer.
● A proxy can hide the actual responses from objects, which may create confusion in
special scenarios.
● Used by Spring AOP to take care of the cross cutting concern code.

6. DECORATOR

● Attach additional responsibilities to an object dynamically. Decorators provide a


flexible alternative to subclassing for extending functionality.
● This pattern says that the class must be closed for modification but open for
extension; that is, a new functionality can be added without disturbing existing
functionalities. The concept is very useful when we want to add special functionalities
to a specific object instead of the whole class.
● This pattern gives us this flexibility to add as many decorators as we want at runtime.
● Decorator pattern is used a lot in Java IO classes, such as FileReader,
BufferedReader etc.

7. ADAPTER

8. FACADE

9. COMPOSITE

10. BRIDGE

11. VISITOR

12. OBSERVER

13. SIMPLE FACTORY

14. MVC

You might also like