0% found this document useful (0 votes)
16 views41 pages

Design Patterns

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views41 pages

Design Patterns

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Introduction to Design Patterns

A Brief History of Design Patterns

 1979 - Christopher Alexander


publishes: “The Timeless Way of
Buildings”
- Introduces the notion of pattern
and a pattern language
- It is a architecture book and not a
software book
- Alexander sought to define step-
by-step rules for solving common
engineering problems relevant to
the creation of buildings and
communities
A Brief History of Design Patterns

 1987 - OOPSLA - Kent Beck and


Ward Cunningham at the
OOPSLA-87 workshop on the
Specification and Design for
Object-Oriented Programming
publish the paper: Using Pattern
Languages for Object-Oriented
Programs
- Discovered Alexander's work for
software engineers by applying 5
patterns in Smalltalk
A Brief History of Design Patterns

 1993 - GOF submitted a catalog


of object oriented software
design patterns to the European
Conference of Object-Oriented
Programming (ECOOP) in 1993
E. Gamma, R. Helm, R.
Johnson, J. Vlissides. Design
Patterns: Abstraction and Reuse
of Object-Oriented Design.
ECOOP 97 LNCS 707, Springer,
1993
A Brief History of Design Patterns

 1993 - Kent Beck and Grady


Booch sponsor the first meeting
of what is now known as the
Hillside Group

 1994 - First Pattern Languages


of Programs (PLoP) conference
A Brief History of Design Patterns

 1995 - GOF publishes :


Design Patterns: Elements
of Reusable Object-
Oriented Software

 The most popular computer


book ever published

 1 million copies sold


What are Patterns?

 A pattern involves a general


description of a recurring
solution to a recurring
problem with various goals
and constraints. It identify
more than a solution, it also
explains why the solution is
needed.
[James Coplien]
What are Patterns?

 ..describes a problem which


occurs over and over again
in our environment, and then
describes the core of the
solution to that problem, in
such a way that you can use
this solution a million times
over, without ever doing it the
same way twice
[Christopher Alexander]
What are Design Patterns?

 “Design patterns are recurring solutions


to software design problems you find
again and again in real-world application
development”
What are Design Patterns?

 Design Patterns not only allow you to


reuse a code solution, but help provide
extensibility, maintainability, and a way
for fellow programmers to understand the
solution
Design Patterns Space

 Creational Patterns
- Deal with initializing and configuring of classes
and objects
 Structural Patterns
- Deal with decoupling interface and
implementation of classes and objects
 Behavioral Patterns
- Deal with dynamic interactions among societies
of classes and objects
Design Patterns Space
Purpose
Creational Structural Behavioral
Class  Factory Adapter Interpreter
Method
 Abstract  Adapter  Chain of
Factory  Bridge Responsibility
 Builder  Composite  Command
 Prototype  Decorator  Iterator
Scope  Singleton  Facade  Mediator
Object  Flyweight  Memento
 Proxy  Observer
 State
 Strategy
 Visitor
How Patterns are Described

 The GoF book describes a pattern using the


following four attributes:
- The name to describes the pattern, its solutions
and consequences in a word or two
- The problem describes when to apply the
pattern
- The solution describes the elements that make
up the design, their relationships,
responsibilities, and collaborations
- The consequences are the results and
tradeoffs in applying the pattern
Factory
 Used to avoid using new operator
repeatedly for creating different objects
 Problem:
 Multiple classes implementing an interface
 How to create these objects? – main() or what?
 Solution:
 Let factory class create and return objects to
main()
 main() – create object of factory and get required
object with the help of parameter
 factory class implements the same interface
Factory - Example
Factory - Example
Factory - Code
Factory - Code
Factory - Code
Singleton
 Used when we need to create only one
object of any specific class
 Problem:
 A class can have multiple objects
 How to restrict a class to make only a single
object?
 Solution:
 Make a class having one static private property
and private constructor a.k.a. Singleton pattern
Singleton - Example

We can create N objects of class A,,,,,no restirction


Singleton - Example

private constructor
private constructor doesn’t allow us to
create object in main()
static Keyword
 static properties are shared among
objects

 static methods/function can be called


without creating objects of class

 That’s why main() is static in Java


 Its inside a class but we run it without creating
its objects
Singleton - Code

private constructor doesn’t allow us


to create object in main()
Singleton - Code
Adapter
 Used to adapt
 E.g. language translation using translator
(adapter)
 Problem:
 How to convert old type to a new type?
 Solution:
 Create Adapter class implementing new type
and uses old type as property. Override new type
method to do the conversion.
Adapter - Example
Adapter - Example
Adapter - Code
Adapter - Code
Facade
 Encapsulates invocation of methods in
different classes and their sequences
 Problem:
 How to call multiple methods from different
classes in some sequence without remembering
the sequence?
 Solution:
 Create Facade class that contains objects of
various classes and use method to define the
sequence of invocation.
Facade - Example

6 methods in 3 classes, difficult to replicate the sequence of method calls


Facade - Example

Use Façade to define the sequence of method calls and use Façade object to
do all the stuff
Facade - Code
Facade - Code
Facade - Code
Builder
 Problem:
 To initialize properties we have to remember the
sequence given in constructor. How to do
initialization without remembering sequence?
 Number of parameters in constructor call must be
equal to number of parameters in constructor
definition. How to avoid it?
 Solution:
 Use Builder to build object and return it to the
main()
Builder - Code
Builder – Code (The Problem)

Sequence is the problem


Builder – Code (The Solution)
Builder - Code

You might also like