vendor/Five/1.2b-r20590

annotate 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
rev   line source
fguillaume@0 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fguillaume@0 2 %deffont "standard" xfont "helvetica-medium-r"
fguillaume@0 3 %deffont "thick" xfont "helvetica-bold-r"
fguillaume@0 4 %deffont "typewriter" xfont "courier-medium-r"
fguillaume@0 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fguillaume@0 6 %%
fguillaume@0 7 %% Default settings per each line numbers.
fguillaume@0 8 %%
fguillaume@0 9 %default 1 area 90 90, leftfill, size 2, fore "gray20", back "white", font "standard", hgap 0
fguillaume@0 10 %default 2 size 7, vgap 10, prefix " ", ccolor "blue"
fguillaume@0 11 %default 3 size 2, bar "gray70", vgap 10
fguillaume@0 12 %default 4 size 5, fore "gray20", vgap 30, prefix " ", font "standard"
fguillaume@0 13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fguillaume@0 14 %%
fguillaume@0 15 %% Default settings that are applied to TAB-indented lines.
fguillaume@0 16 %%
fguillaume@0 17 %tab 1 size 5, vgap 40, prefix " ", icon box "red" 50
fguillaume@0 18 %tab 2 size 4, vgap 40, prefix " ", icon arc "yellow" 50
fguillaume@0 19 %tab 3 size 3, vgap 40, prefix " ", icon delta3 "white" 40
fguillaume@0 20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fguillaume@0 21 %page
fguillaume@0 22
fguillaume@0 23 Five - Zope 3 in Zope 2
fguillaume@0 24
fguillaume@0 25
fguillaume@0 26
fguillaume@0 27
fguillaume@0 28 %center
fguillaume@0 29 Martijn Faassen, Infrae
fguillaume@0 30 faassen@infrae.com
fguillaume@0 31 Five developer
fguillaume@0 32
fguillaume@0 33 %page
fguillaume@0 34
fguillaume@0 35 Interfaces, adapters
fguillaume@0 36
fguillaume@0 37
fguillaume@0 38 What are interfaces?
fguillaume@0 39
fguillaume@0 40 What are adapters?
fguillaume@0 41
fguillaume@0 42 Why?
fguillaume@0 43
fguillaume@0 44 A very quick introduction
fguillaume@0 45
fguillaume@0 46 %page
fguillaume@0 47
fguillaume@0 48 Actually
fguillaume@0 49
fguillaume@0 50
fguillaume@0 51 This tutorial applies to Zope 3 as much as to Five
fguillaume@0 52
fguillaume@0 53 Indication Five reached its goal in this area
fguillaume@0 54
fguillaume@0 55 %page
fguillaume@0 56
fguillaume@0 57 Interface example
fguillaume@0 58
fguillaume@0 59
fguillaume@0 60 %size 4, fore "blue"
fguillaume@0 61 from zope.interface import Interface
fguillaume@0 62
fguillaume@0 63 class IElephant(Interface):
fguillaume@0 64 """An elephant is a big grey animal.
fguillaume@0 65 """
fguillaume@0 66 def getAngerLevel():
fguillaume@0 67 "Return anger level on scale 0 (placid) to 10 (raging)"
fguillaume@0 68
fguillaume@0 69 def trample(target):
fguillaume@0 70 "Trample the target."
fguillaume@0 71
fguillaume@0 72 def trumpet():
fguillaume@0 73 "Make loud noise with trunk."
fguillaume@0 74
fguillaume@0 75 %page
fguillaume@0 76
fguillaume@0 77 Interface example, continued
fguillaume@0 78
fguillaume@0 79
fguillaume@0 80 %size 4, fore "blue"
fguillaume@0 81 from zope.interface import implements
fguillaume@0 82
fguillaume@0 83 class AfricanElephant:
fguillaume@0 84 implements(IElephant)
fguillaume@0 85
fguillaume@0 86 def getAngerLevel(self):
fguillaume@0 87 return 5 # always pretty stroppy
fguillaume@0 88
fguillaume@0 89 def trample(self, target):
fguillaume@0 90 target.flatten()
fguillaume@0 91
fguillaume@0 92 def trumpet(self):
fguillaume@0 93 return "A terrible racket"
fguillaume@0 94
fguillaume@0 95 %page
fguillaume@0 96
fguillaume@0 97 Interfaces
fguillaume@0 98
fguillaume@0 99
fguillaume@0 100 Interfaces are about the what, not the how
fguillaume@0 101
fguillaume@0 102 Interfaces don't do anything, they just describe
fguillaume@0 103
fguillaume@0 104 Code can state what interfaces objects provide
fguillaume@0 105
fguillaume@0 106 Code can introspect whether objects provide interfaces
fguillaume@0 107
fguillaume@0 108 %page
fguillaume@0 109
fguillaume@0 110 Why interfaces?
fguillaume@0 111
fguillaume@0 112
fguillaume@0 113 They are documentation
fguillaume@0 114
fguillaume@0 115 Make multiple implementations of same interface easier
fguillaume@0 116
fguillaume@0 117 Allows you to program against published APIs
fguillaume@0 118
fguillaume@0 119 Allow glueing by interface
fguillaume@0 120
fguillaume@0 121 %page
fguillaume@0 122
fguillaume@0 123 Component architecture
fguillaume@0 124
fguillaume@0 125
fguillaume@0 126 zope.component part of Zope 3
fguillaume@0 127
fguillaume@0 128 allows glueing together of components in various ways
fguillaume@0 129
fguillaume@0 130 a component is an object which provides an interface
fguillaume@0 131
fguillaume@0 132 a Zope 2 object with a Zope 3 interface is a component
fguillaume@0 133
fguillaume@0 134 %page
fguillaume@0 135
fguillaume@0 136 Adapters, example
fguillaume@0 137
fguillaume@0 138
fguillaume@0 139 %size 4, fore "blue"
fguillaume@0 140 class INoiseMaker(Interface):
fguillaume@0 141 """Something that makes noise.
fguillaume@0 142 """
fguillaume@0 143 def makeNoise():
fguillaume@0 144 "Returns the noise that's made."
fguillaume@0 145
fguillaume@0 146 %page
fguillaume@0 147
fguillaume@0 148 Adapters, example continued
fguillaume@0 149
fguillaume@0 150
fguillaume@0 151 %size 4, fore "blue"
fguillaume@0 152 class ElephantNoiseMaker:
fguillaume@0 153 """Adapts elephant to noise maker.
fguillaume@0 154 """
fguillaume@0 155 implements(INoiseMaker)
fguillaume@0 156
fguillaume@0 157 def __init__(self, context):
fguillaume@0 158 self.context = context
fguillaume@0 159
fguillaume@0 160 def makeNoise(self):
fguillaume@0 161 return self.context.trumpet()
fguillaume@0 162
fguillaume@0 163 %page
fguillaume@0 164
fguillaume@0 165 Adapters, example continued 2
fguillaume@0 166
fguillaume@0 167
fguillaume@0 168 %size 4, fore "blue"
fguillaume@0 169 >>> elephant = AfricanElephant()
fguillaume@0 170 >>> noise_maker = ElephantNoiseMaker(elephant)
fguillaume@0 171 >>> print noise_maker.makeNoise()
fguillaume@0 172 'A terrible racket'
fguillaume@0 173
fguillaume@0 174 %page
fguillaume@0 175
fguillaume@0 176 Adapters
fguillaume@0 177
fguillaume@0 178
fguillaume@0 179 Add behavior to object without changing its class
fguillaume@0 180
fguillaume@0 181 More manageable than mixins
fguillaume@0 182
fguillaume@0 183 Define new behavior in terms of other behavior
fguillaume@0 184
fguillaume@0 185 %page
fguillaume@0 186
fguillaume@0 187 Adapters, continued
fguillaume@0 188
fguillaume@0 189
fguillaume@0 190 Less framework burden on adapted objects
fguillaume@0 191
fguillaume@0 192 They only need to be a component
fguillaume@0 193
fguillaume@0 194 Adapted doesn't know about the adapter
fguillaume@0 195
fguillaume@0 196 Adapter is a component itself
fguillaume@0 197
fguillaume@0 198 %page
fguillaume@0 199
fguillaume@0 200 Adapter lookup
fguillaume@0 201
fguillaume@0 202
fguillaume@0 203 We just manually glued the adapter to the adapted
fguillaume@0 204
fguillaume@0 205 What if we had INoiseMaker adapters for other objects?
fguillaume@0 206
fguillaume@0 207 We want a universal way to say: give me a INoiseMaker for this object
fguillaume@0 208
fguillaume@0 209 This allows use to write more generic code
fguillaume@0 210
fguillaume@0 211 %page
fguillaume@0 212
fguillaume@0 213 Adapter lookup, example
fguillaume@0 214
fguillaume@0 215
fguillaume@0 216 %size 4, fore "blue"
fguillaume@0 217 for animal in animal_farm:
fguillaume@0 218 noise_maker = INoiseMaker(animal)
fguillaume@0 219 print noise_maker.makeNoise()
fguillaume@0 220
fguillaume@0 221 %page
fguillaume@0 222
fguillaume@0 223 Adapter glueing
fguillaume@0 224
fguillaume@0 225
fguillaume@0 226 System need to be informed what can adapt what
fguillaume@0 227
fguillaume@0 228 Zope Configuration Markup Language (ZCML) is used for that
fguillaume@0 229
fguillaume@0 230 %page
fguillaume@0 231
fguillaume@0 232 ZCML example
fguillaume@0 233
fguillaume@0 234
fguillaume@0 235 %size 4, fore "blue"
fguillaume@0 236 <configure xmlns="http://namespaces.zope.org/zope">
fguillaume@0 237 <adapter
fguillaume@0 238 for=".module.IElephant"
fguillaume@0 239 provides=".module.INoiseMaker"
fguillaume@0 240 factory=".module.ElephantNoiseMaker" />
fguillaume@0 241 <adapter
fguillaume@0 242 for=".other.IChicken"
fguillaume@0 243 provides=".module.INoiseMaker"
fguillaume@0 244 factory=".other.ChickenNoiseMaker" />
fguillaume@0 245 </configure>
fguillaume@0 246
fguillaume@0 247 %page
fguillaume@0 248
fguillaume@0 249 ZCML, what we just said
fguillaume@0 250
fguillaume@0 251
fguillaume@0 252 The adapter ElephantNoiseMaker adapts any object that provides IElephant to a INoiseMaker
fguillaume@0 253
fguillaume@0 254 The adapter ChickenNoiseMaker adapts any object that provides IChicken to a INoiseMaker
fguillaume@0 255
fguillaume@0 256 %page
fguillaume@0 257
fguillaume@0 258 This works in Zope 2 with Five
fguillaume@0 259
fguillaume@0 260
fguillaume@0 261 This works in Zope 2 with Five
fguillaume@0 262
fguillaume@0 263 Your objects just need to be components (provide Zope 3 interfaces)
fguillaume@0 264
fguillaume@0 265 Your ZCML goes into configure.zcml in your product
fguillaume@0 266
fguillaume@0 267 That's it