Вы находитесь на странице: 1из 7

Design Pattern

“Some body has already solve your problem”

 It is “a recurring solution to solve a common problem in a given context and


system of forces.”

Design patterns are divided into three fundamental groups:

1. Behavioral,
2. Creational, and
3. Structural

1. Creational design patterns

 Deal with one of the most commonly performed tasks in an OO application,


the creation of objects.
 Support a uniform, simple, and controlled mechanism to create objects.
 Allow the encapsulation of the details about what classes are instantiated
and how these instances are created.
 Encourage the use of interfaces, which reduces coupling.

Types of creational design patterns


There are following 6 types of creational design patterns.

1. Factory Method Pattern

“Factory Pattern defines an interface for creating the object but let the subclass
decide which class to instantiate. Factory pattern let the class defer instantiation to
the sub class “

It works on the principle of delegation

2. Abstract Factory Pattern

 Abstract Factory patterns work around a super-factory which creates


other factories. This factory is also called as factory of factories. This type
of design pattern comes under creational pattern as this pattern provides
one of the best ways to create an object.
 In Abstract Factory pattern an interface is responsible for creating a
factory of related objects without explicitly specifying their classes. Each
generated factory can give the objects as per the Factory pattern.

3. Singleton Pattern

Singleton Design Pattern ensures that there is only one instance of a class
and provides global point of access to it.

4. Prototype Pattern

Prototype pattern refers to creating duplicate object while keeping performance in mind.
This type of design pattern comes under creational pattern as this pattern provides one
of the best ways to create an object.

 When we talk about object creation we can find a better way to have new objects:
cloning
 Clone () is a method in the Java programming language for object duplication.

5. Builder Pattern

 Builder pattern builds a complex object using simple objects and using a
step by step approach.
 This type of design pattern comes under creational pattern as this pattern
provides one of the best ways to create an object.
 A Builder class builds the final object step by step. This builder is
independent of other objects.

6. Object Pool Pattern

The performance because it reuses the already created objects instead of creating new
ones. Example: Creating a database connection is a costly operation. So, it will try to use the
already existing database connections. Objects in the pool have a lifecycle:
creation, validation and destroy.
2. Structural design patterns
 They deal with how classes and objects deal with to form
large structures.
 Structural Design patterns use inheritance to compose
interfaces or implementations.

 Structural Design Patterns basically ease the design by

identifying the relationships between entities .

Types of structural design patterns


There are following 7 types of structural design patterns.

1. Adapter Pattern

Adapting an interface into another according to client expectation.

Adapter pattern convert the interface of the class into a form what client
expects. Adapter let the classes work together which couldn’t otherwise due
to incompatible interfaces.”

The Adapter Pattern is also known as Wrapper.

Type of Adapters:

 Object Adapters
 Class Adapters

2. Bridge Pattern

Separating abstraction (interface) from implementation.

 A Bridge Pattern says that just "decouple the functional abstraction from


the implementation so that the two can vary independently".

The Bridge Pattern is also known as Handle or Body.


Advantage of Bridge Pattern
o It enables the separation of implementation from the interface.
o It improves the extensibility.
o It allows the hiding of implementation details from the client.

3. Composite Pattern

Allowing clients to operate on hierarchy of objects.

Advantage of Composite Design Pattern


o It defines class hierarchies that contain primitive and complex objects.
o It makes easier to you to add new kinds of components.
o It provides flexibility of structure with manageable class or interface.

4. Decorator Pattern

Adding functionality to an object dynamically.

 A Decorator Pattern says that just "attach a flexible additional


responsibilities to an object dynamically".

 In other words, The Decorator Pattern uses composition instead of inheritance


to extend the functionality of an object at runtime.

 The Decorator Pattern is also known as Wrapper.

5. Facade Pattern

 Providing an interface to a set of interfaces.

 Facade Pattern says that just "just provide a unified and simplified interface to
a set of interfaces in a subsystem, therefore it hides the complexities of the
subsystem from the client".

 In other words, Facade Pattern describes a higher-level interface that makes the
sub-system easier to use.
 Practically, every Abstract Factory is a type of Facade.

Advantage of Facade Pattern


o It shields the clients from the complexities of the sub-system components.
o It promotes loose coupling between subsystems and its clients.

6. Flyweight Pattern

Reusing an object by sharing it.

 A Flyweight Pattern says that just "to reuse already existing similar kind of
objects by storing them and create new object when no matching object is
found".

Advantage of Flyweight Pattern


o It reduces the number of objects.
o It reduces the amount of memory and storage devices required if the objects are
persisted

7. proxy Pattern

Representing another object.

 Simply, proxy means an object representing another object.

 According to GoF, a Proxy Pattern "provides the control for accessing the


original object".

 So, we can perform many operations like hiding the information of original object, on
demand loading etc.

Advantage of Proxy Pattern


o It provides the protection to the original object from the outside world.
3. Behavioral Design Patterns
Behavioral design patterns are concerned with the interaction and responsibility of
objects.

In these design patterns,the interaction between the objects should be in such a way
that they can easily talk to each other and still should be loosely coupled.

That means the implementation and the client should be loosely coupled in order to avoid
hard coding and dependencies.

There are 12 types of structural design patterns:

1. Chain of Responsibility Pattern

In chain of responsibility, sender sends a request to a chain of objects. A chance to


handle the request. The request can be handled by any object in the chain.

2. Command Pattern

 "Encapsulate a request under an object as a command and pass it to invoker object.


Invoker object looks for the appropriate object which can handle this command and
pass the command to the corresponding object and that object executes the
command".

3. Interpreter Pattern

"To define a representation of grammar of a given language, along with an


interpreter that uses this representation to interpret sentences in the
language".

4. Iterator Pattern

To access the elements of an aggregate object sequentially without


exposing its underlying implementation".

5. Mediator Pattern

A Mediator Pattern says that "to define an object that encapsulates how a set of
objects interact".

6. Memento Pattern
A Memento Pattern says that "to restore the state of an object to its previous state".
But it must do this without violating Encapsulation. Such case is useful in case of
error or failure.

7. Observer Pattern

"Just define a one-to-one dependency so that when one object changes state, all its
dependents are notified and updated automatically".

8. State Pattern

A State Pattern says that "the class behavior changes based on its state". In State
Pattern, we create objects which represent various states and a context object
whose behavior varies as its state object changes

9. Strategy Pattern

Defines a family of functionality, encapsulate each one, and make them


interchangeable".

The Strategy Pattern is also known as Policy.

Template Pattern
1. A Template Pattern says that "just define the skeleton of a function in an operation,
deferring some steps to its subclasses".

Visitor Pattern

 Visitor design pattern is a way of separating an algorithm from an object structure on which it


operates. 

2. Null Objec

Вам также может понравиться