Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Design Pattern

Goals of this Lecture


● Introduce the concept of design patterns
● Explain how it arose from the field of architecture
● Discuss why design patterns are important and what advantages they provide
● OO basics and Principles
Design Patterns
● Originated from the architecture domain by an architect Christopher
Alexander in 1970s

“Each pattern describes a problem that 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”
History
● Christopher Alexander proposes patterns as a way of capturing design
knowledge in architecture domain (1977 - 1979)
○ Each pattern represent a tried, efficient solution to a design problem
○ The Timeless Way of Building book, published in 1979
● Kent Beck & Ward Cunningham used Alexander’s design patterns ideas for
smalltalk GUI design
○ OOPSLA workshop (Object-Oriented Programming, Systems, Languages, and Applications),
1987
● Erich Gamma, Richard Helm, Ralf Johnson, and John Vlissides
○ Design Patterns: Elements of Reusable Object-Oriented Software book, 1994
○ Known as Gang of Four - GoF
Patterns in Software
● “…a pattern is a named problem/solution pair that can be applied in new
contexts, with advice on how to apply it and discussion of its trade-offs.” *

*Larman, page 218


Software Design Patterns
● A general solution of a recurring problem in a particular context
● Description of a communicating classes and objects that interacts with each
other to solve a software problem in a particular context
● It is a description/template on how to solve the problem
● It is not a finished design
● An efficient way of learning from experience of others
○ A format of collecting wisdom and experience of expert designers, and communicating it to
novices [Beck]
How to use them
● “The best way to use patterns is to load your brain with them and then
recognize places in your design and existing applications where you can
apply them. Instead of code reuse, with patterns you get experience reuse.”*

*Head First Design Patterns, p. xi


Are Design Patterns Important?
● Learn from others experience
● Apply effective solutions to known problem with no need to rediscover
solution
○ This will increase productivity
● Hiring newcomers is a serious problem in software engineering
○ It takes long time to prepare them until they become productive
○ In hardware engineering, this problem was solved by having what is called “schematic
diagram”
○ Software industry has been trying to come up with approaches to improve this
■ Via standardization of programming languages
■ Libraries standardization
■ One of the most important ways is design patterns
Benefits of Using Design Patterns
● Simplify communication between developers
○ If we both know the same thing, then it is easy to explain things
○ For example, simply saying “Observer” pattern vs fully describing it which is time consuming
and error-prone
○ Observer is an object, called the subject, maintains a list of its dependents, called
observers, and notifies them automatically of any changes
● Off-the-shelf solutions for common problems
○ If you know the patterns, when you encounter problems, you can easily find the solution
Someone has already solved your problem
Patterns Properties
● Common vocabulary
○ Being familiar with the patterns helps developers to apply them on the problems with no need
to rediscover the solution
● Describe complex parts of the design in compact form
● A good pattern should
○ be very well documented
○ be as general as possible
○ contain a solution that has been proven to effectively solve the problem in the indicated
context
● Patterns do not:
○ Provide an exact solution, code implementation
○ Solve all design problems
Elements of a Pattern
● Name
○ Meaningful identifier of the pattern
○ Important because
■ becomes part of the design vocabulary
■ improves level of communication
● Problem
○ Shows when the pattern is applicable
● Solution
○ Describes elements that made the design pattern, their relationships, and responsibilities
● Consequences
○ Understanding the impact of applying the pattern
○ Each pattern has costs as well as benefits
Pattern Description
● To reuse the design pattern
○ We need to record the decisions, alternatives, trade-offs that led to it
○ Concrete examples are also important as they help to see the design in action
● Describe the patterns in consistent format
● Following the same template for describing the patterns make it easy
○ to learn, compare, and use
Pattern Description Template
● Name & Classification
○ Name is important as it will be part of design vocabulary
● Intent
○ Short description of the purpose of the pattern
○ what the pattern does
○ which issue/ design problem does it solve
● Also known as
○ Specify other names if it has
● Motivation
○ A scenario describing a problem and how the pattern solves that problem
Cont… Pattern Description Template

● Applicability
○ Situations/ circumstances in which the pattern can be applied
● Structure
○ A graphical representation of the classes involved in the pattern
● Participants
○ Classes and objects participating in the pattern and their responsibilities
● Collaboration
○ Shows how participants cooperate to achieve their responsibilities
Cont… Pattern Description Template

● Consequences
○ Results of the application, benefits, costs
● Implementation
○ Specifies which techniques to be aware of when implementing the pattern, language issue,
dependencies
● Sample code
○ Code snippet/fragment which shows how to implement the pattern
● Known uses
○ Real examples in which the pattern were used
● Related patterns
○ Specify other patterns that relate to this pattern, and what are the differences between them
Catalog of Design Patterns
● There are multiple design patterns
○ So we need a way to classify them into families
● Classification patterns will help
○ Learning patterns faster
○ Finding new patterns
● Two criterias for classifying patterns
○ Based on the purpose
■ Reflects what the pattern does
○ Based on scope
■ Specifies whether the pattern applies to classes or objects
Types of Design Patterns (purpose)
● Based on the purpose, patterns are classified into three groups
○ Creational patterns
■ To create objects of the right class for the problem, generally when instances of several
different classes are available
○ Structural patterns
■ To form a larger structure from individual parts
○ Behavioral patterns
■ Describe interaction between objects
■ Focus on how parts communicate with other
Types of Design Patterns (scope)

● Class patterns
○ deals with relationships between classes and their subclasses
○ These relationships are established through inheritance
● Objects patterns
○ Deals with relationships between objects
Why Design Patterns
● Reuse tried, proven solutions
● Establish common vocabulary
○ Higher level of communication
○ Increase productivity
○ Save time to discuss details
○ Avoids misunderstandings

Provides experience reuse, not code reuse


Fundamental OO Concepts & Principles
● Design patterns rely on OO basics and principles
● OO basics
○ Abstraction
○ Encapsulation
○ Inheritance
○ Polymorphism
● Design patterns emerged from fundamental principles
○ Encapsulate what varies
○ Favor composition over inheritance
○ Program to interfaces, not to implementations

You might also like