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

Module-3

Design Patterns
A design patterns are well-proved solution for solving the
specific problem/task.
Core Java Design Patterns
Problem In core java, there are mainly three types of design patterns,
Suppose you want to create a class for which only a single which are further divided into their sub-parts:
instance (or object) should be created and that single object
can be used by all other classes.
1. Creational Design Pattern
Solution: 2. Factory Pattern
Singleton design pattern is the best solution of above 3. Abstract Factory Pattern
specific problem. So, every design pattern has some 4. Singleton Pattern
specification or set of rules for solving the problems.
5. Prototype Pattern
By using the design patterns you can make your code more
flexible, reusable and maintainable. It is the most important 6. Builder Pattern.
part because java internally follows design patterns.
2. Structural Design Pattern
Advantage of design pattern: 1. Adapter Pattern
1. They are reusable in multiple projects. 2. Bridge Pattern
2. They provide the solutions that help to define the 3. Composite Pattern
system architecture.
4. Decorator Pattern
3. They capture the software engineering
5. Facade Pattern
experiences.
6. Flyweight Pattern
4. They provide transparency to the design of an
application. 7. Proxy Pattern
5. They are well-proved and testified solutions since
they have been built upon the knowledge and 3. Behavioral Design Pattern
experience of expert software developers.
1. Chain Of Responsibility Pattern
6. Design patterns do not guarantee an absolute
solution to a problem. They provide clarity to the 2. Command Pattern
system architecture and the possibility of building 3. Interpreter Pattern
a better system 4. Iterator Pattern
5. Mediator Pattern
When should we use the design patterns? 6. Memento Pattern
7. Observer Pattern
We must use the design patterns during the analysis and 8. State Pattern
requirement phase of SDLC (Software Development Life 9. Strategy Pattern
Cycle). 10. Template Pattern
11. Visitor Pattern
Design patterns ease the analysis and requirement phase of
SDLC by providing information based on prior hands-on
experiences. 1. Creational design patterns
Categorization of design patterns: Creational design patterns are concerned with the way of
creating objects. These design patterns are used when a
decision must be made at the time of instantiation of a class
Basically, design patterns are categorized into two parts:
(i.e. creating an object of a class).

1. Core Java (or JSE) Design Patterns.


But everyone knows an object is created by using new
2. JEE Design Patterns. keyword in java. For example:

StudentRecord s1=new StudentRecord();


Hard-Coded code is not the good programming approach. Usage of Abstract Factory Pattern
Here, we are creating the instance by using the new keyword. o When the system needs to be independent of how
Sometimes, the nature of the object must be changed its object are created, composed, and represented.
according to the nature of the program. In such cases, we
must get the help of creational design patterns to provide o When the family of related objects has to be used
together, then this constraint needs to be enforced.
more general and flexible approach.
o When you want to provide a library of objects that
does not show implementations and only reveals
1.1 Factory Pattern interfaces.
o When the system needs to be configured with one
A Factory Pattern or Factory Method Pattern says that of a multiple family of objects.
just define an interface or abstract class for creating an
object but let the subclasses decide which class to
instantiate. In other words, subclasses are responsible to 1.3 Singleton design pattern
create the instance of the class.
Singleton Pattern says that just "define a class that has only
The Factory Method Pattern is also known as Virtual one instance and provides a global point of access to it".
Constructor.
In other words, a class must ensure that only single instance
should be created and single object can be used by all other
Advantage of Factory Design Pattern
classes.
o Factory Method Pattern allows the sub-classes to
choose the type of objects to create.
There are two forms of singleton design pattern
o It promotes the loose-coupling by eliminating the
need to bind application-specific classes into the
code. That means the code interacts solely with the o Early Instantiation: creation of instance at load
resultant interface or abstract class, so that it will time.
work with any classes that implement that o Lazy Instantiation: creation of instance when
interface or that extends that abstract class. required.

Usage of Factory Design Pattern


o When a class doesn't know what sub-classes will
be required to create Advantage of Singleton design pattern
o When a class wants that its sub-classes specify the o Saves memory because object is not created at
objects to be created. each request. Only single instance is reused again
o When the parent classes choose the creation of and again.
objects to its sub-classes.

1.2 Abstract Factory Pattern


Usage of Singleton design pattern
Abstract Factory Pattern says that just define an interface o Singleton pattern is mostly used in multi-threaded
or abstract class for creating families of related (or and database applications. It is used in logging,
dependent) objects but without specifying their concrete caching, thread pools, configuration settings etc.
sub-classes. That means Abstract Factory lets a class returns
a factory of classes. So, this is the reason that Abstract 2. Structural design patterns
Factory Pattern is one level higher than the Factory Pattern.

Structural design patterns are concerned with how classes


An Abstract Factory Pattern is also known as Kit.
and objects can be composed, to form larger structures.

Advantage of Abstract Factory Pattern The structural design patterns simplify the structure by
o Abstract Factory Pattern isolates the client code identifying the relationships.
from concrete (implementation) classes.
o It eases the exchanging of object families. These patterns focus on, how the classes inherit from each
o It promotes consistency among objects. other and how they are composed from other classes.

2.1 Bridge Pattern


A Bridge Pattern says that just "decouple the functional 3.1 Iterator Pattern
abstraction from the implementation so that the two can
vary independently".
Iterator Pattern is used "to access the elements of an
aggregate object sequentially without exposing its
The Bridge Pattern is also known as Handle or Body. underlying implementation".

Advantage of Bridge Pattern The Iterator pattern is also known as Cursor.


o It enables the separation of implementation from
the interface.
Advantage of Iterator Pattern
o It improves the extensibility.
o It supports variations in the traversal of a
o It allows the hiding of implementation details from collection.
the client.
o It simplifies the interface to the collection.

Usage of Bridge Pattern Usage of Iterator Pattern:


o When you don't want a permanent binding
between the functional abstraction and its
implementation. It is used:
o When both the functional abstraction and its
implementation need to extended using sub- o When you want to access a collection of objects
classes. without exposing its internal representation.
o It is mostly used in those places where changes are o When there are multiple traversals of objects need
made in the implementation does not affect the to be supported in the collection.
clients.
3.2 Observer Pattern
2.2 Flyweight Pattern
An Observer Pattern says that "just define a one-to-one
A Flyweight Pattern says that just "to reuse already dependency so that when one object changes state, all its
existing similar kind of objects by storing them and dependents are notified and updated automatically".
create new object when no matching object is found".
The observer pattern is also known as Dependents or
Advantage of Flyweight Pattern Publish-Subscribe.
o It reduces the number of objects.
o It reduces the amount of memory and storage
devices required if the objects are persisted
Benefits:
Usage of Flyweight Pattern o It describes the coupling between the objects and
the observer.
o When an application uses number of objects
o When the storage cost is high because of the o It provides the support for broadcast-type
quantity of objects. communication.
o When the application does not depend on object
identity.

Usage:
3. Behavioral Design Patterns o When the change of a state in one object must be
reflected in another object without keeping the
Behavioral design patterns are concerned with the objects tight coupled.
interaction and responsibility of objects. o When the framework we writes and needs to be
enhanced in future with new observers with
In these design patterns, the interaction between the minimal changes.
objects should be in such a way that they can easily talk
to each other and still should be loosely coupled.
3.3 Model view controller design pattern
That means the implementation and the client should be
loosely coupled in order to avoid hard coding and The Model-View-Controller (MVC) is a well-known design
dependencies. pattern in the web development field.
It is way to organize our code. It specifies that a program or
application shall consist of data model, presentation
information and control information. The MVC pattern
needs all these components to be separated as different
objects.

o Model: It represents the business layer of


application. It is an object to carry the data that can
also contain the logic to update controller if data is
changed.
o View: It represents the presentation layer of
application. It is used to visualize the data that the
model contains.
o Controller: It works on both the model and view.
It is used to manage the flow of application, i.e.
data flow in the model object and to update the
view whenever data is changed.

Advantages of MVC Architecture

The advantages of MVC architecture are as follows:

o MVC has the feature of scalability that in turn


helps the growth of application.
o The components are easy to maintain because
there is less dependency.
o A model can be reused by multiple views that
provide reusability of code.
o The developers can work with the three layers
(Model, View, and Controller) simultaneously.
o Using MVC, the application becomes more
understandable.
o Using MVC, each layer is maintained separately
therefore we do not require to deal with massive
code.
o The extending and testing of application is easier.

You might also like