M2S2 Design Patterns

You might also like

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

1

WWW.PENTALOG.COM
Design Patterns

WWW.PENTALOG.COM
Iterator

WWW.PENTALOG.COM
Python features: Iterator

C++

Java

Python

WWW.PENTALOG.COM
Python features: Iterator

WWW.PENTALOG.COM
Design Patterns: Iterator
● iter()
● __next__()
● __iter__()
● StopIteration

WWW.PENTALOG.COM
Generator

WWW.PENTALOG.COM
Python features: Generator
● Generators are a simple and powerful tool for creating
iterators.

WWW.PENTALOG.COM
Decorator

WWW.PENTALOG.COM
Python features: Decorator

Callable that takes another function as


argument (the decorated function)
my_func = decorator(my_func)

10

WWW.PENTALOG.COM
Python features: Decorator

11

WWW.PENTALOG.COM
Python features: Decorator

12

WWW.PENTALOG.COM
Python features: Decorator

13

WWW.PENTALOG.COM
Python features: Decorator

Memoization
● optimization technique used primarily to speed up computer
programs
● store the results of expensive function calls
● return the cached result when the same inputs occur again

14

WWW.PENTALOG.COM
Python features: Decorator

15

WWW.PENTALOG.COM
Python features: Decorator

16

WWW.PENTALOG.COM
Python features: Decorator

fib(20): 3.00 s
memoized fib(20): 0.004 s

17

WWW.PENTALOG.COM
Façade

18

WWW.PENTALOG.COM
Design Patterns: Façade

19

WWW.PENTALOG.COM
Design Patterns: Façade

20

WWW.PENTALOG.COM
Adapter

21

WWW.PENTALOG.COM
Design Patterns: Adapter

22

WWW.PENTALOG.COM
Template method

23

WWW.PENTALOG.COM
Design Patterns: Template Method

24

WWW.PENTALOG.COM
S.O.L.I.D principles

25

WWW.PENTALOG.COM
S.O.L.I.D
● Single responsibility principle
● Open/closed principle
● Liskov substitution principle
● Interface segregation principle
● Dependency inversion principle

26

WWW.PENTALOG.COM
S.O.L.I.D
● Single responsibility principle
○ A class should have only one reason to change.

● Open/closed principle
○ software entities (classes, modules, functions, etc.)
should be open for extension, but closed for modification
○ an entity can allow its behaviour to be extended without
modifying its source code

27

WWW.PENTALOG.COM
S.O.L.I.D
● Liskov substitution principle
○ Barbara Liskov
○ if S is a subtype of T, then objects of type T may be
replaced with objects of type S

● Interface segregation principle


○ no client should be forced to depend on methods it does
not use

28

WWW.PENTALOG.COM
S.O.L.I.D
● Dependency inversion principle
○ decoupling software modules
○ High-level modules should not depend on low-level
modules. Both should depend on abstractions.
○ Abstractions should not depend on details. Details should
depend on abstractions

29

WWW.PENTALOG.COM
K.I.S.S
● Keep it simple, stupid
● Simplicity should be a key goal in design and unnecessary
complexity should be avoided

D.R.Y
● Don’t repeat yourself
● vs Write Everything Twice

30

WWW.PENTALOG.COM
Q&A

31

WWW.PENTALOG.COM
32

WWW.PENTALOG.COM

You might also like