0% found this document useful (0 votes)
141 views

Design Patterns: - Shridhar Hegde

This is a presentation about Design Patterns in Software Engineering. It is specifically about Creational Patterns.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

Design Patterns: - Shridhar Hegde

This is a presentation about Design Patterns in Software Engineering. It is specifically about Creational Patterns.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DESIGN PATTERNS

- Shridhar Hegde

Contents in the presentation are referred from - http://www.baeldung.com/creational-design-patterns


What is Design Pattern ?

■ Design patterns are solutions to general problems that software developers face
during software development

■ All these design patterns were obtained by trial and error by large number of
developers over a long span of time.

■ These design patterns suggest the best practices to be followed by a software


developer for extensive scalability and functionality.
Do you know him ? You don’t ?
Unfortunately, even I don’t know !
These 4 scientist
published a book in
1994 named:

Design Patterns –
Erich Gamma Richard Helms Elements of Object
Oriented Software

GOF

Gang
Of
Four
Ralph Johnson John Vlissides
Classifications of Design Pattern

■ As per the design pattern reference book Design Patterns - Elements of Reusable
Object-Oriented Software , there are 23 design patterns which can be classified in
three categories

– Creational Pattern

– Structural Pattern

– Behavioral Pattern

■ In this presentation, we will be discussing only about Creational Patterns.


Creational Patterns
■ Creational Design Patterns are concerned with the way in which objects are created.

■ They reduce complexities and instability by creating objects in a controlled manner.


■ Some of the important types of Creational Patterns are:
– Singleton – Ensures that at most only one instance of an object exists throughout
application

– Factory Method – Creates objects of several related classes without specifying the exact
object to be created

– Abstract Factory – Creates families of related dependent objects

– Builder – Constructs complex objects using step-by-step approach


Singleton Design Pattern

■ The Singleton Design Pattern aims to keep a check on initialization of objects of a


particular class by ensuring that only one instance of the object exists throughout
the code.

■ A Singleton class also provides one unique global access point to the object so that
each subsequent call to the access point returns only that particular object.

■ Let us see an example of how the singleton design pattern is implemented in Java.
Singleton Design Pattern Contd..

Image showing the Java code to ensure Single Object for a method.
Singleton Design Pattern Contd..

This is the original DoF implementation which is more complex and causes problems while
Multi-threading.
Factory Method Design Pattern

■ According to GoF, this pattern “defines an interface for creating an object, but let
subclasses decide which class to instantiate.

■ This pattern delegates the responsibility of initializing a class from the client to a
particular factory class by creating a type of virtual constructor.

■ To achieve this, we rely on a factory which provides us with the objects, hiding the
actual implementation details. The created objects are accessed using a common
interface.
Factory Method Design Pattern Contd..

■ Let us create a Polygon Interface

■ Then, we are going to create a PolygonFactory class that uses the above interface to
create object of the class that the user needs.
Factory Method Design Pattern Contd..

The above code depicts the PolygonFactory class deciding to return an object
that the user needs depending on the interface that was declared earlier.
Abstract Factory Method Design Pattern

■ In the previous section, we saw how the Factory Method design pattern could be
used to create objects related to a single family.

■ By contrast, the Abstract Factory Design Pattern is used to create families of related
or dependent objects. It’s also sometimes called a factory of factories.

■ The GoF definition states that an Abstract Factory “provides an interface for creating
families of related or dependent objects without specifying their concrete classes”.
Abstract Factory Method Design Pattern Contd..
■ To deal with an example, here is a FactoryProvider which gives us either object of
Animal or Color class depending on the user choice
Abstract Factory Method Design Pattern Contd..
Builder Design Pattern
■ The Builder Design Pattern is another creational pattern designed to deal with the
construction of comparatively complex objects.

■ When the complexity of creating object increases, the Builder pattern can separate
out the instantiation process by using another object (a builder) to construct the
object..

■ This builder can then be used to create many other similar representations

using a simple step-by-step approach.


Builder Design Pattern Contd..
■ The original Builder Design Pattern introduced by GoF focuses on abstraction and is
very good when dealing with complex objects, however, the design is a little
complicated.

■ Joshua Bloch, in his book Effective Java, introduced an improved version of the
builder pattern which is very easy to understand and hence we will be using that
version of Builder Design Pattern implementation.
Builder Design Pattern Contd..
■ In the current implementation, we will discuss an object of BankAccount class can
be created using a Builder Class.

The above code shows the class BankAccount having some private data fields and a static class
BankAccountBuiler that is embedded inside it for assisting in creating object.
Builder Design Pattern Contd..

The above code shows how the BankAccountBuilder class is used to create objects.
Builder Design Pattern Contd..
■ In order to create object for the class BankAccount using the BankAccountBuilder class, the Java
syntax to do so is:

BankAccount newAccount = new BankAccount

. BankAccountBuilder (“Shridhar”, “100269”)

. withEmail (“[email protected]”)

. wantNewsLetter (false)

. build( );
Summary
■ Design patterns are solutions to general problems that software developers face
during software development.

■ These design patterns suggest the best practices to be followed by a software


developer for extensive scalability and functionality.

■ Design Patterns can be mainly classified into 3 types as:

– Creational Pattern

– Structural Pattern

– Behavioral Pattern
Summary Contd..
■ The 4 main types of Creation Design Patterns are:
– Singleton – Ensures that at most only one instance of an object exists
throughout application

– Factory Method – Creates objects of several related classes without specifying


the exact object to be created

– Abstract Factory – Creates families of related dependent objects

– Builder – Constructs complex objects using step-by-step approach

You might also like