48
MedTech Chapter 4 – Design Patterns Known Patterns and Design and Implementation Examples Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 1 MedTech – Mediterranean Institute of Technology Software Engineering MedTech

Software Engineering - chp4- design patterns

Embed Size (px)

Citation preview

Page 1: Software Engineering - chp4- design patterns

MedTech

Chapter 4 – Design Patterns

Known Patterns and Design and Implementation Examples

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 1

MedTech – Mediterranean Institute of TechnologySoftware Engineering

MedTech

Page 2: Software Engineering - chp4- design patterns

MedTech

DESIGN PATTERNS: DEFINITION AND UTILITYDesign Patterns

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 2

Page 3: Software Engineering - chp4- design patterns

MedTech

Design Patterns

• In software engineering, a design pattern is a generalrepeatable solution to a commonly occuring problem in software design

• It isn’t a finished design that can be transformed directly intocode, but a description or template for how to solve a problem that can be used in many different situations

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 3

Design Patterns: Definition and Utility

Page 4: Software Engineering - chp4- design patterns

MedTech

Design Patterns: Usage

• 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

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 4

Design Patterns: Definition and Utility

Page 5: Software Engineering - chp4- design patterns

MedTech

Design Patterns: Essential Elements

• 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

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 5

Design Patterns: Definition and Utility

Page 6: Software Engineering - chp4- design patterns

MedTech

GoF Design Patterns

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 6

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 GoFdesign 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

Page 7: Software Engineering - chp4- design patterns

MedTech

GoF Design Patterns

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 7

Design Patterns: Definition and Utility

Page 8: Software Engineering - chp4- design patterns

MedTech

CREATIONAL PATTERNSDesign Patterns

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 8

Page 9: Software Engineering - chp4- design patterns

MedTech

Creational Patterns: Definition

• 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

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 9

Creational Patterns

Page 10: Software Engineering - chp4- design patterns

MedTech

Singleton

• Ensure that only one instance of a class is created and provide a global access point to the object.

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 10

Creational Patterns

Page 11: Software Engineering - chp4- design patterns

MedTech

Singleton: Usage

• 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

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 11

Creational Patterns

Page 12: Software Engineering - chp4- design patterns

MedTech

Factory

• Creates objects without exposing the instantiation logic to the client• Refers to the newly created object through a common interface.

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 12

Creational Patterns

Page 13: Software Engineering - chp4- design patterns

MedTech

Factory: Usage

• 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 becreated by the class

• Common Usage• factories providing an xml parser:

• javax.xml.parsers.DocumentBuilderFactory

• javax.xml.parsers.SAXParserFactory

• java.net.URLConnection

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 13

Creational Patterns

Page 14: Software Engineering - chp4- design patterns

MedTech

Builder

• Defines an instance for creating an object but letting subclassesdecide which class to instanciate

• Allows finer control over the construction process

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 14

Creational Patterns

Page 15: Software Engineering - chp4- design patterns

MedTech

Builder

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 15

Creational Patterns

Page 16: Software Engineering - chp4- design patterns

MedTech

Builder

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 16

Creational Patterns

Page 17: Software Engineering - chp4- design patterns

MedTech

Builder

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 17

Creational Patterns

Page 18: Software Engineering - chp4- design patterns

MedTech

Builder

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 18

Creational Patterns

Page 19: Software Engineering - chp4- design patterns

MedTech

Builder

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 19

Creational Patterns

Page 20: Software Engineering - chp4- design patterns

MedTech

Builder: Usage

• 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 objectsthat 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 thenit is asked for it, but the way that the class is put together is up to the Builder class

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 20

Creational Patterns

Page 21: Software Engineering - chp4- design patterns

MedTech

STRUCTURAL PATTERNSDesign Patterns

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 21

Page 22: Software Engineering - chp4- design patterns

MedTech

Definition

• 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 abilityto change the composition at runtime, which is impossible with static class composition.

• Examples:• Adapter• Proxy• Bridge• Composite

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 22

Structural Patterns

Page 23: Software Engineering - chp4- design patterns

MedTech

Adapter

• Converts the interface of a class into another interface the clients expect• Lets classes work together, that normally wouldn’t, due to incompatible

interfaces

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 23

Structural Patterns

Page 24: Software Engineering - chp4- design patterns

MedTech

Adapter

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 24

Structural Patterns

Page 25: Software Engineering - chp4- design patterns

MedTech

Adapter

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 25

Structural Patterns

Page 26: Software Engineering - chp4- design patterns

MedTech

Adapter

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 26

Structural Patterns

Page 27: Software Engineering - chp4- design patterns

MedTech

Adapter

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 27

Structural Patterns

Page 28: Software Engineering - chp4- design patterns

MedTech

Adapter

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 28

Structural Patterns

Page 29: Software Engineering - chp4- design patterns

MedTech

Proxy

• Provide a « Placeholder » for an object to control references to it

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 29

Structural Patterns

Page 30: Software Engineering - chp4- design patterns

MedTech

Proxy

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 30

Structural Patterns

Page 31: Software Engineering - chp4- design patterns

MedTech

Proxy

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 31

Structural Patterns

Page 32: Software Engineering - chp4- design patterns

MedTech

Proxy

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 32

Structural Patterns

Page 33: Software Engineering - chp4- design patterns

MedTech

Proxy

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 33

Structural Patterns

Page 34: Software Engineering - chp4- design patterns

MedTech

Composite

• Compose objects into tree structures to represent part-whole hierarchies. • Composite lets clients treat individual objects and compositions of objects

uniformly.

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 34

Structural Patterns

Page 35: Software Engineering - chp4- design patterns

MedTech

Composite

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 35

Structural Patterns

Page 36: Software Engineering - chp4- design patterns

MedTech

Composite

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 36

Structural Patterns

Page 37: Software Engineering - chp4- design patterns

MedTech

Composite

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 37

Structural Patterns

Page 38: Software Engineering - chp4- design patterns

MedTech

Composite

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 38

Structural Patterns

Page 39: Software Engineering - chp4- design patterns

MedTech

BEHAVIORAL PATTERNSDesign Patterns

Dr. L i l ia SFAXIwww. l i l iasfax i .wix .com /l i l iasfax i

S l id e 39

Page 40: Software Engineering - chp4- design patterns

MedTech

Definition

• 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

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 40

Behavioral Patterns

Page 41: Software Engineering - chp4- design patterns

MedTech

Command

• Encapsulates a request in an object• Allows the parameterization of clients with different requests

• Allows saving the request in a queue

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 41

Behavioral Patterns

Page 42: Software Engineering - chp4- design patterns

MedTech

Command

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 42

Behavioral Patterns

Page 43: Software Engineering - chp4- design patterns

MedTech

Command

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 43

Behavioral Patterns

Page 44: Software Engineering - chp4- design patterns

MedTech

Iterator

• The iterator pattern allowsus to:• Access contents of a

collection withoutexposing its internalstructure.

• Support multiple simultaneous traversals of a collection.

• Provide a uniform interface for traversing differentcollections.

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 44

Behavioral Patterns

Page 45: Software Engineering - chp4- design patterns

MedTech

Iterator

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 45

Behavioral Patterns

Page 46: Software Engineering - chp4- design patterns

MedTech

Observer

• Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updatedautomatically.

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 46

Behavioral Patterns

Page 47: Software Engineering - chp4- design patterns

MedTech

Observer

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 47

Behavioral Patterns

Page 48: Software Engineering - chp4- design patterns

MedTech

References

Dr. L i l ia SFAXI www. l i l iasfax i .wix .com /l i l iasfax i

S l id e 48

• Object Oriented Design, http://www.oodesign.com/, consultednovember 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