Download presentation
Presentation is loading. Please wait.
Published byQuentin Pope Modified over 6 years ago
1
COMP280:Introduction to Software Development Week 10, Lecture 28
Bernhard Pfahringer
2
Design Patterns 1994 book by GoF “Gang of Four” Gamma, Helm, Johnson, Vlissides Template solution for common problem Make up for a deficiency/shortcoming of the language lack of library Also help documenting/understanding systems Generally: Patterns use classes and instances to explicitly represent actions
3
Groups of patterns Creational, e.g. Structural, e.g. Behavioural, e.g.
Prototype Abstract Factory Builder Structural, e.g. Adapter Decorator Proxy Behavioural, e.g. Iterator Command [Concurrency] [Architecture]
4
Prototype [ plus shallow and deep copying ]
Creating an instance using another as a template In Java or C/C++: copy-constructor Python: only one __init__ (more or less) but copy(), copy.deepcopy(), __copy__, __deepcopy__ a = [ [ 0 ] ] b = a.copy() a[0][0] = 1 b ? b[0] = 2 a ? a = [ [ 0 ], 1] a[1] = a[0] a ? a[0][0] = 1 b = copy.deepcopy(a) b[0][0] = 2 b ?
5
Adapter In static languages: wrap some class inside another, using composition, to provide for a different interface (think electric plugs) Python: can add methods at runtime, plus duck-typing E.g. you have some classes all implementing make_noise(), but class Dog only implements bark(), then we can do: def make_noise(self): self.bark() Dog.make_noise = make_noise More concise, using an anonymous function: Dog.make_noise = lambda self: self.bark()
6
Iterator So useful, it is now built into most languages Libraries
Python3: iter( … ), next( … ), define your one class: __init__, __iter__, __next__ Syntactic sugar for i in list: … Python: prefer generators, simpler to code, no class needed
7
Iterator example (cheating?)
class Flat: def __init__(self,list): self.flat_iter = iter(flatten(list)) def __iter__(self): return self.flat_iter def __next__(self): return next(self.flat_iter) >>> iter = Flat( [[[1],[[2],3]]]) >>> next(iter) >>> for i in iter: print(i) def flatten(list): flat_list = [] def flatten_helper(list): for x in list: if isinstance(x,list): flatten_helper(x) else: flat_list.append(x) flatten_helper(list) return flat_list
8
Generator-based flatten_list
def flatten_list(l): if isinstance(l, list): for entry in l: for terminal in flatten_list(entry): yield terminal else: yield l
9
Simplify using “yield from”
def flatten_list(l): if isinstance(l, list): for entry in l: yield from flatten_list(entry) else: yield l >>> tuple( flatten_list( [1,[2,[3],4],5]) ) (1,2,3,4,5)
10
Command pattern (for whatever reason) represent commands explicitly
Might be more flexible Can be done dynamically
11
Example: class Debit is a Command
class Log: def __init__(self): self._transactions = [] def do(self, command): self._transactions.append(command) command.do() def undo(self): self._transactions.pop().undo() class Debit: def __init__(self, account, amount): self._account = account self._amount = amount def do(self): self._account.subtract(self._amount) def undo(self): self._account.add(self._amount)
12
Command example cont. class Account: def __init__(self, amount): self._balance = amount self.showBalance() def add(self, amount): self._balance += amount def subtract(self, amount): self._balance -= amount def showBalance(self): print( self, "balance", self._balance) >>> acc = Account(0) >>> log = Log() >>> log.do( Debit( acc, 10)) >>> log.do( Debit( acc, 20)) >>> log.undo()
13
Concurrency / Architecture
Too complex for this course, use Wikipedia as a starting point for your own research Architecture: e.g. MVC model-view-controller Interesting reading: /2007/08/25/interactive- application-architecture/ and-gonzo/flux-vs-mvc-design- patterns-57b28c0f71b7
14
Design Patterns summary
Patterns can be useful for multiple reasons Frequently used ones “migrate” into languages Some patterns are more important for static languages Mocking, as used for testing, can be seen as a pattern
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.