vendor/Five/1.2b-r20590

view doc/presentations/five_interface_tutorial.mgp @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %deffont "standard" xfont "helvetica-medium-r"
3 %deffont "thick" xfont "helvetica-bold-r"
4 %deffont "typewriter" xfont "courier-medium-r"
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 %%
7 %% Default settings per each line numbers.
8 %%
9 %default 1 area 90 90, leftfill, size 2, fore "gray20", back "white", font "standard", hgap 0
10 %default 2 size 7, vgap 10, prefix " ", ccolor "blue"
11 %default 3 size 2, bar "gray70", vgap 10
12 %default 4 size 5, fore "gray20", vgap 30, prefix " ", font "standard"
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 %%
15 %% Default settings that are applied to TAB-indented lines.
16 %%
17 %tab 1 size 5, vgap 40, prefix " ", icon box "red" 50
18 %tab 2 size 4, vgap 40, prefix " ", icon arc "yellow" 50
19 %tab 3 size 3, vgap 40, prefix " ", icon delta3 "white" 40
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 %page
23 Five - Zope 3 in Zope 2
28 %center
29 Martijn Faassen, Infrae
30 faassen@infrae.com
31 Five developer
33 %page
35 Interfaces, adapters
38 What are interfaces?
40 What are adapters?
42 Why?
44 A very quick introduction
46 %page
48 Actually
51 This tutorial applies to Zope 3 as much as to Five
53 Indication Five reached its goal in this area
55 %page
57 Interface example
60 %size 4, fore "blue"
61 from zope.interface import Interface
63 class IElephant(Interface):
64 """An elephant is a big grey animal.
65 """
66 def getAngerLevel():
67 "Return anger level on scale 0 (placid) to 10 (raging)"
69 def trample(target):
70 "Trample the target."
72 def trumpet():
73 "Make loud noise with trunk."
75 %page
77 Interface example, continued
80 %size 4, fore "blue"
81 from zope.interface import implements
83 class AfricanElephant:
84 implements(IElephant)
86 def getAngerLevel(self):
87 return 5 # always pretty stroppy
89 def trample(self, target):
90 target.flatten()
92 def trumpet(self):
93 return "A terrible racket"
95 %page
97 Interfaces
100 Interfaces are about the what, not the how
102 Interfaces don't do anything, they just describe
104 Code can state what interfaces objects provide
106 Code can introspect whether objects provide interfaces
108 %page
110 Why interfaces?
113 They are documentation
115 Make multiple implementations of same interface easier
117 Allows you to program against published APIs
119 Allow glueing by interface
121 %page
123 Component architecture
126 zope.component part of Zope 3
128 allows glueing together of components in various ways
130 a component is an object which provides an interface
132 a Zope 2 object with a Zope 3 interface is a component
134 %page
136 Adapters, example
139 %size 4, fore "blue"
140 class INoiseMaker(Interface):
141 """Something that makes noise.
142 """
143 def makeNoise():
144 "Returns the noise that's made."
146 %page
148 Adapters, example continued
151 %size 4, fore "blue"
152 class ElephantNoiseMaker:
153 """Adapts elephant to noise maker.
154 """
155 implements(INoiseMaker)
157 def __init__(self, context):
158 self.context = context
160 def makeNoise(self):
161 return self.context.trumpet()
163 %page
165 Adapters, example continued 2
168 %size 4, fore "blue"
169 >>> elephant = AfricanElephant()
170 >>> noise_maker = ElephantNoiseMaker(elephant)
171 >>> print noise_maker.makeNoise()
172 'A terrible racket'
174 %page
176 Adapters
179 Add behavior to object without changing its class
181 More manageable than mixins
183 Define new behavior in terms of other behavior
185 %page
187 Adapters, continued
190 Less framework burden on adapted objects
192 They only need to be a component
194 Adapted doesn't know about the adapter
196 Adapter is a component itself
198 %page
200 Adapter lookup
203 We just manually glued the adapter to the adapted
205 What if we had INoiseMaker adapters for other objects?
207 We want a universal way to say: give me a INoiseMaker for this object
209 This allows use to write more generic code
211 %page
213 Adapter lookup, example
216 %size 4, fore "blue"
217 for animal in animal_farm:
218 noise_maker = INoiseMaker(animal)
219 print noise_maker.makeNoise()
221 %page
223 Adapter glueing
226 System need to be informed what can adapt what
228 Zope Configuration Markup Language (ZCML) is used for that
230 %page
232 ZCML example
235 %size 4, fore "blue"
236 <configure xmlns="http://namespaces.zope.org/zope">
237 <adapter
238 for=".module.IElephant"
239 provides=".module.INoiseMaker"
240 factory=".module.ElephantNoiseMaker" />
241 <adapter
242 for=".other.IChicken"
243 provides=".module.INoiseMaker"
244 factory=".other.ChickenNoiseMaker" />
245 </configure>
247 %page
249 ZCML, what we just said
252 The adapter ElephantNoiseMaker adapts any object that provides IElephant to a INoiseMaker
254 The adapter ChickenNoiseMaker adapts any object that provides IChicken to a INoiseMaker
256 %page
258 This works in Zope 2 with Five
261 This works in Zope 2 with Five
263 Your objects just need to be components (provide Zope 3 interfaces)
265 Your ZCML goes into configure.zcml in your product
267 That's it