Facade KU

asynchat.fifo facades for list

& dbhash facades for bsddb

0 ...also given as example of Adapter...!-) d old sets.Set used to facade for diet a Queue facades for deque + lock 0 well...:-)

0 os.path: basename, dirname facade for split + indexing; isdir &c facade for os.stat + stat.S ISDIR &c.

some RL facades may have substantial code

0 simplifying the _protocol_ is the key

0 interface simplifications are often accompanied by minor functional additions

0 Facade occurs at all levels of complexity, but it's most important for _complicated subsystems_ (and reasonably-simple views)

0 inheritance is never useful for Facade, because it can only "widen", never "restrict" (so, wrapping is the norm)

callables (functions &c) play a large role in Python programming -- so you often may need to adapt or facade them

0 functools.partial to preset some args (AKA "currying") + bound-methods special case d decorator syntax to adapt functions or methods by wrapping in HOFs

0 closures for simp e HOF needs

0 classes w/_call_for complex ones

have N1 realizations p of abstraction A,

0 each using any one of N2 implementations i of functionality F, 0 without coding N1*N2 cases ... _>

have abstract superclass A of all p hold a reference R to the interface F of all i, 0 ensure each p uses any functionality of F (thus, from a i) only by delegating to R

class AbstractParser(object):

self.scanner = scanner def __getattr(self, name):

return getattr(self.scanner, name) class ExpressionParser(AbstractParser):

def expr(self):

... token = self.next_token() ... ... self.push_back(token) ...

def expr(self):

... token = self.next_token() ... ... self.push_back(token) ...

0 BaseServer is the "abstraction" A

0 BaseRequestHandler is F (the abstract superclass for functionality implementation)

0 (also uses mixins for threading, forking...)

0 note: A holds the very class F, and instantiates it per-request (shades of a Factory DP...) -- typical in Bridge DP in Python (also e.g. email: Parser -> Message)

0 -> Bridge is typical of somewhat "rich", complicated situations client code y requires a protocol C supplier code " does provide protocol C but we want to insert some semantic tweak often dynamically plug-in/plug-out-able 0 decorator code % "sneaks in the middle": y uses 5 just as it would use " 5 wraps ", and it may intercept, modify, add (a little), delegate, ...

class WriteFullLinesOnlyFile(object):

self._f = open(*a, **k) self._b = " def write(self, data):

lns = (self._b+data).splitlines(True) if lns[-1][-1]=='\n': self._b = " else: self._b = lns.pop(-1) self._f.writelines(lns)

return getattr(self._f, name)

Was this article helpful?

0 0

Post a comment