2. DESIGN PATTERNS IN JAVA
A design patterns are well-proved solution for solving the specific
problem/task.
Design patterns represent the best practices used by experienced object-
oriented software developers. Design patterns are solutions to general
problems that software developers faced during software development. These
solutions were obtained by trial and error by numerous software developers
over quite a substantial period of time.
A design pattern systematically names, motivates, and explains a general
design that addresses a recurring design problem in object-oriented systems.
It describes the problem, the solution, when to apply the solution, and its
consequences. It also gives implementation hints and examples.
3. WHAT IS GANG OF FOUR (GOF)?
In 1994, four authors Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides published a book titled Design
Patterns - Elements of Reusable Object-Oriented
Software which initiated the concept of Design Pattern in
Software development.
These authors are collectively known as Gang of Four
(GOF).
According to these authors design patterns are primarily
based on the following principles of object orientated design.
Program to an interface not an implementation
Favor object composition over inheritance
4. USAGE OF DESIGN PATTERN
Design Patterns have two main usages in software development.
Common platform for developers
Design patterns provide a standard terminology and are specific to particular scenario. For
example, a singleton design pattern signifies use of single object so all developers familiar
with single design pattern will make use of single object and they can tell each other that
program is following a singleton pattern.
Best Practices
Design patterns have been evolved over a long period of time and they provide best solutions
to certain problems faced during software development. Learning these patterns helps
unexperienced developers to learn software design in an easy and faster way.
5. WHAT ARE DESIGN PATTERNS IN JAVA?
Design Patterns in Java are very talked-about among
software system developers. A design pattern could be a
well-described answer to a typical software system problem.
Some of the advantages of using design patterns are:
Java Design Patterns provide a traditional business approach
to resolve a recurring problem. Therefore it saves time if we
use the design pattern. There are several Java design
patterns that we can use in our Java projects.
6. Using design patterns in Java promotes reusability that ends
up in a lot of robust and highly maintainable code. It helps in
reducing Total Cost of Ownership (TCO) of the software
package.
Since design patterns are already defined, it makes our code
straightforward to understand and debug. It ends up in
quicker development and new members of the team are
aware of it simply
7. ADVANTAGE OF DESIGN PATTERN:
They are reusable in multiple projects.
They provide the solutions that help to define the system architecture.
They capture the software engineering experiences.
They provide transparency to the design of an application.
They are well-proved and testified solutions since they have been built
upon the knowledge and experience of expert software developers.
Design patterns don’t guarantee an absolute solution to a problem. They
provide clarity to the system architecture and the possibility of building a
better system.
8. WHEN SHOULD WE USE THE DESIGN PATTERNS?
We must use the design patterns during the analysis and requirement
phase of SDLC(Software Development Life Cycle).
Design Patterns are already defined and provides industry standard
approach to solve a recurring problem, so it saves time if we sensibly
use the design pattern. There are many java design patterns that we can
use in our java based projects.
Using design patterns promotes reusability that leads to
more robust and highly maintainable code. It helps in reducing total cost
of ownership (TCO) of the software product.
Since design patterns are already defined, it makes our code easy to
understand and debug. It leads to faster development and new members
of team understand it easily.
10. TYPES OF DESIGN PATTERNS IN JAVA
These Java design patterns have 3 categories – Creational, Structural, and
Behavioural design patterns.
a) Creational Design Patterns in Java
Creational design patterns in Java give an answer to instantiate an object within
the very best approach for specific things.
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 (i.e. creating an object of a class).
Creational patterns often used in place of direct instantiation with constructors.
They make the creation process more adaptable and dynamic.
In particular, they can provide a great deal of flexibility about which objects are
created, how those objects are created, and how they are initialized.
These design patterns provide a way to create objects while hiding the creation
logic, rather than instantiating objects directly using new operator. This gives
program more flexibility in deciding which objects need to be created for a given
use case.
11. TYPES OF DESIGN PATTERNS IN JAVA CONTD.
i. Singleton Pattern
Singleton design patterns in Java restricts the instantiation of a class and ensures that just one
instance of the class exists within the Java virtual machine. It looks to be a really easy design
pattern, however, once it involves implementation, it comes with a lot of implementation
concerns. The implementation of the Singleton pattern has always been a disputable topic
among developers.
Singleton Pattern says that just "define a class that has only one instance and
provides a global point of access to it".
In other words, a class must ensure that only single instance should be created and
single object can be used by all other classes.
There are two forms of singleton design pattern
Early Instantiation: creation of instance at load time.
Lazy Instantiation: creation of instance when required.
12. TYPES OF DESIGN PATTERNS IN JAVA CONTD.
(ii) Factory Method Pattern
A Factory Pattern or Factory Method Pattern says that 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 create the instance of the class.
The Factory Method Pattern is also known as Virtual Constructor.
Factory design pattern is most suitable when complex object creation steps are involved.
To ensure that these steps are centralized and not exposed to composing classes.
Factory design pattern is used when we have a super class with multiple sub-classes and
based on input, we need to return one of the sub-class. This pattern take out the
responsibility of instantiation of a class from client program to the factory class.
Factory design patterns in Java is used once we have an excellent class with multiple
sub-classes and a super input. In this, we need to define the interface or abstract class
for creating an object. Subclasses are responsible for the creation of an instance of the
class.
13. TYPES OF DESIGN PATTERNS IN JAVA CONTD.
(iii.) Abstract Factory Pattern
Abstract factory Java pattern can compare to factory pattern and its
factory of factories. A single factory category that returns the various
sub-classes supported the input provided and factory class uses if-else
or switch statement to attain this. An Abstract Factory Pattern is also
known as Kit.
In Abstract factory pattern, we tend to eliminate if-else block and have a
factory class for every sub-class and so an Abstract factory class which
will return the sub-class supported the input factory category.
Abstract Factory Pattern says that just define an interface or abstract
class for creating families of related (or dependent) objects but
without specifying their concrete sub-classes. That means Abstract
Factory lets a class returns a factory of classes. So, this is the reason
that Abstract Factory Pattern is one level higher than the Factory
Pattern.
14. B) STRUCTURAL DESIGN PATTERNS IN JAVA
Structural Java patterns give alternative ways to form a class
structure, as an example mistreatment inheritance and
composition to form an outsized object from small objects.
Structural design patterns are concerned with how classes
and objects can be composed, to form larger structures.
The structural design patterns simplifies the structure by
identifying the relationships.
These patterns focus on, how the classes inherit from each
other and how they are composed from other classes.
15. STRUCTURAL DESIGN PATTERNS IN JAVA CONTD.
(i) Flyweight Pattern
Flyweight design pattern is used when we need to create a lot of Objects of a class. Since every
object consumes memory space that can be crucial for low memory devices, such as mobile
devices or embedded systems, flyweight design pattern can be applied to reduce the load on
memory by sharing objects. String Pool implementation in java is one of the best example of
Flyweight pattern implementation.
(ii) Bridge Pattern
When we have interface hierarchies in both interfaces as well as implementations, then bridge
design pattern is used to decouple the interfaces from implementation and hiding the
implementation details from the client programs. Like Adapter pattern, it’s one of the Structural
design pattern.
The implementation of bridge design pattern follows the notion to prefer Composition over
inheritance.
Bridge design pattern is used to decouple a class into two parts – abstraction and
it’s implementation – so that both can evolve in future without affecting each other. It increases the
loose coupling between class abstraction and it’s 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.
16. C) BEHAVIORAL DESIGN PATTERNS
These design patterns are specifically concerned with communication
between objects.
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.
17. (I) OBSERVER PATTERN
Observer pattern defines a one-to-many dependency
between objects so that when one object changes
state, all its dependents are notified and updated
automatically.
It is also referred to as the publish-subscribe pattern.
18. (ii) Iterator
According to GoF, Iterator Pattern is used "to access the
elements of an aggregate object sequentially without
exposing its underlying implementation".
The Iterator pattern is also known as Cursor.
In collection framework, we are now using Iterator that is
preferred over Enumeration.
Iterator pattern provides a way to access the elements of
an aggregate object sequentially without exposing its
underlying representation.