Chapter 4 - Design Patterns

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

MedTech – Mediterranean Institute of Technology

Software Engineering

Chapter 4 – Design Patterns


Known Patterns and Design and Implementation Examples

MedTech

Dr. Lilia SFAXI


Slide 1
www.liliasfaxi.wix.com /liliasfaxi
Design Patterns

DESIGN PATTERNS: DEFINITION AND UTILITY

MedTech

Dr. Lilia SFAXI


Slide 2
www.liliasfaxi.wix.com /liliasfaxi
Design Patterns
Design Patterns: Definition and Utility

• In software engineering, a design pattern is a general


repeatable solution to a commonly occuring problem in
software design

• It isn’t a finished design that can be transformed directly into


code, but a description or template for how to solve a
problem that can be used in many different situations

MedTech

Dr. Lilia SFAXI


Slide 3
www.liliasfaxi.wix.com /liliasfaxi
Design Patterns: Usage
Design Patterns: Definition and Utility

• Design patterns:
• Provide general solutions, documented in a format that doesn’t require
specifics tied to a particular problem
• Can speed up the development process by providing tested, proven
development paradigms
• Help you benefit from the experience of fellow developers
• Prevent subtle issues that can cause major problems
• Improve code readability for coders and architects familiar with them

MedTech

Dr. Lilia SFAXI


Slide 4
www.liliasfaxi.wix.com /liliasfaxi
Design Patterns: Essential Elements
Design Patterns: Definition and Utility

• A pattern has four essential elements:


• The pattern name that we use to describe a design problem
• The problem that describes when to apply the pattern
• The solution that describes the elements that make up the
design
• The consequences that are the results and trade-offs of
applying the pattern

MedTech

Dr. Lilia SFAXI


Slide 5
www.liliasfaxi.wix.com /liliasfaxi
GoF Design Patterns
Design Patterns: Definition and Utility
• The Gang of Four are the four authors of the book « Design Patterns:
Elements of Reusable Object-Oriented Software »
• Defined 23 design patterns for recurrent design issues, called GoF
design patterns
• Classified by purpose :
• Structural : Concerns the composition of classes and objects
• Behavioral : Characterizes the interaction and responsibility of objects and
classes
• Creational : Concerns the creation process of objects and classes
• … and by scope:
• Class scope: relationship between classes and subclasses, defined
statically
• Object scope: object relationships, dynamic MedTech

Dr. Lilia SFAXI


Slide 6
www.liliasfaxi.wix.com /liliasfaxi
GoF Design Patterns
Design Patterns: Definition and Utility

MedTech

Dr. Lilia SFAXI


Slide 7
www.liliasfaxi.wix.com /liliasfaxi
Design Patterns

CREATIONAL PATTERNS

MedTech

Dr. Lilia SFAXI


Slide 8
www.liliasfaxi.wix.com /liliasfaxi
Creational Patterns: Definition
Creational Patterns
• Creational patterns abstract the instantiation process.
• They help to make a system independent of how its objects are
created, composed, and represented
• Creational patterns for classes use inheritance to vary the class that is
instantiated.
• Creational patterns for objects delegate instantiation to another object.

• Examples:
• Factory
• Singleton
• Builder
• Prototype

MedTech

Dr. Lilia SFAXI


Slide 9
www.liliasfaxi.wix.com /liliasfaxi
Singleton
Creational Patterns
• Ensure that only one instance of a class is created and provide a global
access point to the object.

MedTech

Dr. Lilia SFAXI


Slide 10
www.liliasfaxi.wix.com /liliasfaxi
Singleton: Usage
Creational Patterns
• When to Use
• When we must ensure that only one instance of a class is created
• When the instance must be available through all the code
• In multi-threading environments when multiple threads must access the
same resources through the same singleton object.
• Common Usage
• Logger Classes
• Configuration Classes
• Accessing resources in shared mode
• Other design patterns implemented as Singletons:
• Factories and Abstract Factories, Builder, Prototype

MedTech

Dr. Lilia SFAXI


Slide 11
www.liliasfaxi.wix.com /liliasfaxi
Factory
Creational Patterns
• Creates objects without exposing the instantiation logic to the client
• Refers to the newly created object through a common interface.

MedTech

Dr. Lilia SFAXI


Slide 12
www.liliasfaxi.wix.com /liliasfaxi
Factory: Usage
Creational Patterns
• When to use
• When a framework delegates the creation of objects derived from a common
superclass to the factory
• When we need flexibility in adding new types of objects that must be
created by the class

• Common Usage
• factories providing an xml parser:
• javax.xml.parsers.DocumentBuilderFactory
• javax.xml.parsers.SAXParserFactory
• java.net.URLConnection

MedTech

Dr. Lilia SFAXI


Slide 13
www.liliasfaxi.wix.com /liliasfaxi
Builder
Creational Patterns
• Defines an instance for creating an object but letting subclasses
decide which class to instanciate
• Allows finer control over the construction process

MedTech

Dr. Lilia SFAXI


Slide 14
www.liliasfaxi.wix.com /liliasfaxi
Builder
Creational Patterns

MedTech

Dr. Lilia SFAXI


Slide 15
www.liliasfaxi.wix.com /liliasfaxi
Builder
Creational Patterns

MedTech

Dr. Lilia SFAXI


Slide 16
www.liliasfaxi.wix.com /liliasfaxi
Builder
Creational Patterns

MedTech

Dr. Lilia SFAXI


Slide 17
www.liliasfaxi.wix.com /liliasfaxi
Builder
Creational Patterns

MedTech

Dr. Lilia SFAXI


Slide 18
www.liliasfaxi.wix.com /liliasfaxi
Builder
Creational Patterns

MedTech

Dr. Lilia SFAXI


Slide 19
www.liliasfaxi.wix.com /liliasfaxi
Builder: Usage
Creational Patterns
• When to use
• When the creation algorithm of a complex object is independent from the
parts that actually compose the object
• When the system needs to allow different representations for the objects
that are being built

• Builder and Factory


• Very similar to the Factory pattern
• Factory : the client uses the factory’s methods to create its own objects
• Builder : the Builder class is instructed on how to create the object and then
it is asked for it, but the way that the class is put together is up to the
Builder class

MedTech

Dr. Lilia SFAXI


Slide 20
www.liliasfaxi.wix.com /liliasfaxi
Design Patterns

STRUCTURAL PATTERNS

MedTech

Dr. Lilia SFAXI


Slide 21
www.liliasfaxi.wix.com /liliasfaxi
Definition
Structural Patterns

• Structural patterns are concerned with how classes and objects are
composed to form larger structures.
• Structural class patterns use inheritance to compose interfaces or
implementations.
• Structural object patterns describe ways to compose objects to realize new
functionality. The added flexibility of object composition comes from the ability
to change the composition at runtime, which is impossible with static class
composition.

• Examples:
• Adapter
• Proxy
• Bridge
• Composite
MedTech

Dr. Lilia SFAXI


Slide 22
www.liliasfaxi.wix.com /liliasfaxi
Adapter
Structural Patterns
• Converts the interface of a class into another interface the clients expect
• Lets classes work together, that normally wouldn’t, due to incompatible
interfaces

MedTech

Dr. Lilia SFAXI


Slide 23
www.liliasfaxi.wix.com /liliasfaxi
Adapter
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 24
www.liliasfaxi.wix.com /liliasfaxi
Adapter
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 25
www.liliasfaxi.wix.com /liliasfaxi
Adapter
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 26
www.liliasfaxi.wix.com /liliasfaxi
Adapter
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 27
www.liliasfaxi.wix.com /liliasfaxi
Adapter
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 28
www.liliasfaxi.wix.com /liliasfaxi
Proxy
Structural Patterns
• Provide a « Placeholder » for an object to control references to it

MedTech

Dr. Lilia SFAXI


Slide 29
www.liliasfaxi.wix.com /liliasfaxi
Proxy
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 30
www.liliasfaxi.wix.com /liliasfaxi
Proxy
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 31
www.liliasfaxi.wix.com /liliasfaxi
Proxy
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 32
www.liliasfaxi.wix.com /liliasfaxi
Proxy
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 33
www.liliasfaxi.wix.com /liliasfaxi
Composite
Structural Patterns
• Compose objects into tree structures to represent part-whole hierarchies.
• Composite lets clients treat individual objects and compositions of objects
uniformly.

MedTech

Dr. Lilia SFAXI


Slide 34
www.liliasfaxi.wix.com /liliasfaxi
Composite
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 35
www.liliasfaxi.wix.com /liliasfaxi
Composite
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 36
www.liliasfaxi.wix.com /liliasfaxi
Composite
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 37
www.liliasfaxi.wix.com /liliasfaxi
Composite
Structural Patterns

MedTech

Dr. Lilia SFAXI


Slide 38
www.liliasfaxi.wix.com /liliasfaxi
Design Patterns

BEHAVIORAL PATTERNS

MedTech

Dr. Lilia SFAXI


Slide 39
www.liliasfaxi.wix.com /liliasfaxi
Definition
Behavioral Patterns

• Behavioral patterns are concerned with algorithms and the assignment of


responsibilities between objects
• Behavioral class patterns use inheritance to distribute behavior between
classes.
• Behavioral object patterns use composition rather than inheritance. Some
describe how a group of peer objects cooperate to perform a task that no
single object can carry out by itself.

• Examples:
• Command
• Iterator
• Observer
• Strategy

MedTech

Dr. Lilia SFAXI


Slide 40
www.liliasfaxi.wix.com /liliasfaxi
Command
Behavioral Patterns
• Encapsulates a request in an object
• Allows the parameterization of clients with different requests
• Allows saving the request in a queue

MedTech

Dr. Lilia SFAXI


Slide 41
www.liliasfaxi.wix.com /liliasfaxi
Command
Behavioral Patterns

MedTech

Dr. Lilia SFAXI


Slide 42
www.liliasfaxi.wix.com /liliasfaxi
Command
Behavioral Patterns

MedTech

Dr. Lilia SFAXI


Slide 43
www.liliasfaxi.wix.com /liliasfaxi
Iterator
Behavioral Patterns
• The iterator pattern allows
us to:
• Access contents of a
collection without
exposing its internal
structure.
• Support multiple
simultaneous traversals of
a collection.
• Provide a uniform interface
for traversing different
collections.

MedTech

Dr. Lilia SFAXI


Slide 44
www.liliasfaxi.wix.com /liliasfaxi
Iterator
Behavioral Patterns

MedTech

Dr. Lilia SFAXI


Slide 45
www.liliasfaxi.wix.com /liliasfaxi
Observer
Behavioral Patterns
• Defines a one-to-many dependency between objects so that when one
object changes state, all its dependents are notified and updated
automatically.

MedTech

Dr. Lilia SFAXI


Slide 46
www.liliasfaxi.wix.com /liliasfaxi
Observer
Behavioral Patterns

MedTech

Dr. Lilia SFAXI


Slide 47
www.liliasfaxi.wix.com /liliasfaxi
References
• Object Oriented Design, http://www.oodesign.com/, consulted
november 2016
• Gang of Four (GoF)OO Design Patterns , ( Course ) WATERLOO CHERITON
SCHOOL OF COMPUTER SCIENCE, 2011
• Design Patterns , ( Course ) Faculty of Science, Engineering and
Technology, 2007

• Textbooks
• E. Gamma & al. Design Patterns: Elements of Reusable Object-Oriented
Software , Addison-Wesley, 1994
• B. Christiansson & al. GoF Design Patterns -with examples using Java and
UML2 , 2008
MedTech

Dr. Lilia SFAXI


Slide 48
www.liliasfaxi.wix.com /liliasfaxi

You might also like