Relationship Manger - Before and After - Modelling Composite Design Pattern in Python

You might also like

Download as pdf
Download as pdf
You are on page 1of 1

woolcD

C:\Documents Documents\aa
ard Settings\AdminisbatorMy bthon...

,
usE-RM = 1 (9-"^ il^^3Il^ .ft fl
5,^,,.'hJr
if USE_RM:
4 from RM ilnpolt RML
5 RM = RMlo
I'dixectionalrr)
o RDl.RR("xy",'onetoone",

I
9
10
11
!2
clats _CorE)onent (obj ect) :
def _setThing(sel'f,
def -getThing(self):
def -getBack(sel^f):
thing) :

ttring = property (_getThing,


"dW,,P.i
baeL = property (_getBack)
L1l else:
1q class \
_corqlonent{object):
de?_init_(seir): \ ^w
ID

t7 =
sere.tf,jlng Nonel L ,h{e*""k
18 serr.back = NoneJ o[f i.Adal o4^l^'^1
19 ll
20 crass component(tonulonent): F^'9?l- tWO /Oin\ry)'
2L def Do(self): # virtual |
22 raise 'Do not irtPlemented'
ZJ
4A class Thj.ng (Component ) :
25 def Do(seLf):
z4 return 'hi'
2'7
2B clasg Decorator (Conponent ) :
.zt pa93
?n
5f class Decoratorl (Decorato.r) :
32 def Do{5e1f}:
return t * * I * self.thing.Dc() +' ""'
34
class Proxy (co[E]onent ) :
JO def -init-(self ):
5 t cgtrE)snerrt . _i"Iit_ ( s e 1f )
ae self.thing = ThinSo
39
llo def Do{gel-f } :
4I assert self.thing, 'oops, have lost the real thing obiect'
42 return self ' thing.Do ( )
i?
44 def AddDecorator(self' d) :
45 sel.f .hsertDecorator(d) # TODA rnake this append to the -ljst of decorators.
{b

def InsertDecorator(self, d) :
418
. 4 9 Inserts the decorator first in the list of decorators.
rrirt
50
cl d.thing = sel-f.thing # make decorater paint to the thing you were pointinEr to
5Z setf.thing = 4 # point' to the decotatox
CJ

54

58
X "vLt
59
OU
6t
Fj-nd the
point it
thing_A poj-ntj-ng
at the lhing the
to the dyi,ng decorator
dying
and
deco.rator was pointing to. ' /r*'t{zJ1
o4
OJ assert d.back
= d.thing
64
65
d.back.thing
fa,cut f
66
67
b5
if not usE_RM:
d.thing.back = d.back # update back pointer! !
fr"-. Ai""^7
CA
70
JL

'J
irnport unitte-st
hilhQdin " u
74 class TestcaseoL (u]altte.st.fe.stcase) :

def setup(se1f):
77 self .Proxything = f'3o*t,,
td seIf.d1 = Decoratorlo
19
80 def checkNoDecoration(self ):
81 self .ProxYthing = P3o*U1,
82 res = self .proxythinE.Do ()
83 #Tsrint res
84 asgert te.s == 'hi'
85
Ub def checkoneDecoration ( self) I
8? self .proxythi rrg.AddDecorator ( 3elf' dl)
88 res = self .prox!.thitg.Doo
dv &rir]t res
r ^ft PM
24f04f2604II:00

You might also like