Design Patterns: Devendra Dave Ravi Babu Vepa
Design Patterns: Devendra Dave Ravi Babu Vepa
Devendra Dave
Ravi Babu Vepa
Agenda
● Introduction to Design Patterns
● Types of Design Patterns
● GoF Software Design Patterns
● Types of GoF Design Patterns
● Frequently used Design Patterns
● References and Further Reading
2
Introduction to Design Patterns
●
Pattern describes a problem which occurs over and over again in our
environment, and then describes the core of the solution to the
problem, in such a way that you can use this solution million times
over, without ever doing it the same way twice.
● Desription of each pattern can consist of the following
➔ Name handle to describe a design problem
➔ Problem – when to apply pattern
➔ Solution – solution template describing elements and their
relationships
➔ Consequences results and trade offs of applying pattern
3
● Types of Design Patterns
● Gang of Four Patterns
● Enterprise Patterns
● SOA and Messaging Patterns
● Model – View Patterns
4
● GoF Software Design Patterns
● 23 classic software design patterns providing recurring
solutions to common problems in software design
● Developed by Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides, often referred to as the Gang
of Four
● Design Patterns: Elements of Reusable ObjectOriented
Software Book
5
Types of GoF Design Patterns
● Creational Design Patterns
– Singleton Pattern
– Factory Pattern
– Abstract Factory Pattern
– Builder Pattern
– Prototype Pattern
● Structural Design Patterns
– Adapter Pattern
– Composite Pattern
– Proxy Pattern
– Flyweight Pattern
– Facade Pattern
– Bridge Pattern
– Decorator Pattern
●
Behavioral Design Patterns
– Template Method Pattern
– Mediator Pattern
– Chain of Responsibility Pattern
– Observer Pattern
– Strategy Pattern
– State Pattern
– Visitor Pattern
– Iterator Pattern
– Memento Pattern
6
Creational Design Patterns
● Abstracts the instantiation process
● Make system independent of how its objects are created,
composed and represented
● Creational Patterns highly used in software design
– Singleton Pattern
– Factory Method Pattern
– Abstract Factory Pattern
7
Singleton Pattern
● Ensure a class only has one instance, and provide a global point of access
to it.
● Client access the singleton instance soley though its instance
operation/method
● Sample Usage : Logger, configuration classes, connection pool manager etc
class Singleton
{
private static Singleton instance;
private Singleton()
{
...
}
return instance;
}
...
public void doSomething()
{
...
}
}
8
Factory Method Pattern
● Factory is a Java class that is used to encapsulate object
creation code
● Factory class instantiates and returns a particular type of
object based on data passed to the factory
● Objects returned from factory are subclasses of common
parent class
● e.g. BorderFactory
● java.sql.Connection
● java.sql.Statement
9
Factory Method Pattern (..contd.)
package com.cakes;
package com.cakes;
public class AnimalFactory {
public class Cat extends Animal {
public Animal getAnimal(String type) {
@Override
if ("canine".equals(type)) {
public String makeSound() {
return new Dog();
return "Meow";
} else {
}
return new Cat();
}
}
}
}
package com.cakes;
public class Demo {
public static void main(String[] args) {
AnimalFactory animalFactory = new AnimalFactory();
Animal a1 = animalFactory.getAnimal("feline");
System.out.println("a1 sound: " + a1.makeSound());
Animal a2 = animalFactory.getAnimal("canine");
System.out.println("a2 sound: " + a2.makeSound());
}
}
10
● Abstract Factory Pattern
● Abstract factory is a factory that returns factories
● An abstract factory is used to return factories that can be
used to create sets of related objects
● e.g. Swing UI LookFeel UIManager,
javax.swing.plaf.metal.MetalLookAndFeel, GTKLookAndFeel,
WindowsLookAndFeel
11
● Abstract Factory Pattern (Contd.)
12
Structural Design Patterns
● Concerened with how classes and objects are composed
to form a larger structures
● Used in establishing the relationship between different
entities
● Structural Patterns highly used in software design
– Facade Pattern
– Proxy Pattern
– Decorator Pattern
13
Facade Pattern
● A facade classes is used to provide a single interface to
set of classes
● Facade simplifies a clients interaction with a complex
system by localizing the interactions into a single interface
●
14
Proxy Pattern
● A proxy class is used to control access to another class
● A proxy is to control access rights to an object
● Usage: RMI Proxies in form of stub and skeleton
15
Behavioral Patterns
● Identify common communication patterns between objects and
realize these patterns
● Increase flexibility in carrying out this communication
● Concerened with algorithms and the assignment of
responsibiities between objects
● Behavioral Patterns highly used in software design
– Template Method Pattern
– Strategy Pattern
– Chain of Responsibility Pattern
16
Template Method Pattern
● Define the sekeleton of an algorithm in form of
method(template method), deferring some steps to
subclasses
● Lets subclasses redefine certain steps without changing
the algorithm structure
● Usage: Meal example, Parser examples(db, csv etc)
17
Strategy Patterns
● Defines a family of algorithms,encapsulates each
algorithm, and makes the algorithms interchangeable
within that family
● Algorithms can be selected on the fly
● Alternative to subclassing and allows different behaviors to
be placed in concrete strategy classes
● Usage: Multiple validation alogrithms to be used, printer
driver example
18
References & Further Readings
● Books
● Links
● http://www.oodesign.com/
● http://en.wikipedia.org/wiki/Software_design_pattern
● http://www.corej2eepatterns.com/
19
Thank You
20