Design Patterns: - Shridhar Hegde
Design Patterns: - Shridhar Hegde
- Shridhar Hegde
■ 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.
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
– Factory Method – Creates objects of several related classes without specifying the exact
object to be created
■ 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..
■ 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
■ 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:
. withEmail (“[email protected]”)
. wantNewsLetter (false)
. build( );
Summary
■ Design patterns are solutions to general problems that software developers face
during software development.
– 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