vendor/Five/1.2b-r20590

diff 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 diff
     1.1 new file mode 100644
     1.2 --- /dev/null
     1.3 +++ b/doc/presentations/five_interface_tutorial.mgp
     1.4 @@ -0,0 +1,267 @@
     1.5 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1.6 +%deffont "standard" xfont "helvetica-medium-r"
     1.7 +%deffont "thick" xfont "helvetica-bold-r"
     1.8 +%deffont "typewriter" xfont "courier-medium-r"
     1.9 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1.10 +%%
    1.11 +%% Default settings per each line numbers.
    1.12 +%%
    1.13 +%default 1 area 90 90, leftfill, size 2, fore "gray20", back "white", font "standard", hgap 0
    1.14 +%default 2 size 7, vgap 10, prefix " ", ccolor "blue"
    1.15 +%default 3 size 2, bar "gray70", vgap 10
    1.16 +%default 4 size 5, fore "gray20", vgap 30, prefix " ", font "standard"
    1.17 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1.18 +%%
    1.19 +%% Default settings that are applied to TAB-indented lines.
    1.20 +%%
    1.21 +%tab 1 size 5, vgap 40, prefix "  ", icon box "red" 50
    1.22 +%tab 2 size 4, vgap 40, prefix "      ", icon arc "yellow" 50
    1.23 +%tab 3 size 3, vgap 40, prefix "            ", icon delta3 "white" 40
    1.24 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1.25 +%page
    1.26 +
    1.27 +Five - Zope 3 in Zope 2
    1.28 +
    1.29 +
    1.30 +
    1.31 +
    1.32 +%center
    1.33 +Martijn Faassen, Infrae
    1.34 +faassen@infrae.com
    1.35 +Five developer
    1.36 +
    1.37 +%page
    1.38 +
    1.39 +Interfaces, adapters
    1.40 +
    1.41 +
    1.42 +	What are interfaces?
    1.43 +
    1.44 +	What are adapters?
    1.45 +
    1.46 +	Why?
    1.47 +
    1.48 +	A very quick introduction
    1.49 +
    1.50 +%page
    1.51 +
    1.52 +Actually
    1.53 +
    1.54 +
    1.55 +	This tutorial applies to Zope 3 as much as to Five
    1.56 +
    1.57 +	Indication Five reached its goal in this area
    1.58 +
    1.59 +%page
    1.60 +
    1.61 +Interface example
    1.62 +
    1.63 +
    1.64 +%size 4, fore "blue"
    1.65 +from zope.interface import Interface
    1.66 +
    1.67 +class IElephant(Interface):
    1.68 +    """An elephant is a big grey animal.
    1.69 +    """
    1.70 +    def getAngerLevel():
    1.71 +        "Return anger level on scale 0 (placid) to 10 (raging)"
    1.72 +
    1.73 +    def trample(target):
    1.74 +        "Trample the target."
    1.75 +
    1.76 +    def trumpet():
    1.77 +        "Make loud noise with trunk."
    1.78 +
    1.79 +%page
    1.80 +
    1.81 +Interface example, continued
    1.82 +
    1.83 +
    1.84 +%size 4, fore "blue"
    1.85 +from zope.interface import implements
    1.86 +
    1.87 +class AfricanElephant:
    1.88 +    implements(IElephant)
    1.89 +
    1.90 +    def getAngerLevel(self):
    1.91 +        return 5 # always pretty stroppy
    1.92 +
    1.93 +    def trample(self, target):
    1.94 +        target.flatten()
    1.95 +
    1.96 +    def trumpet(self):
    1.97 +        return "A terrible racket"
    1.98 +
    1.99 +%page
   1.100 +
   1.101 +Interfaces
   1.102 +
   1.103 +
   1.104 +	Interfaces are about the what, not the how 
   1.105 +
   1.106 +	Interfaces don't do anything, they just describe
   1.107 +
   1.108 +	Code can state what interfaces objects provide
   1.109 +
   1.110 +	Code can introspect whether objects provide interfaces
   1.111 +
   1.112 +%page
   1.113 +
   1.114 +Why interfaces?
   1.115 +        
   1.116 +
   1.117 +	They are documentation
   1.118 +
   1.119 +	Make multiple implementations of same interface easier
   1.120 +
   1.121 +	Allows you to program against published APIs
   1.122 +
   1.123 +	Allow glueing by interface
   1.124 +
   1.125 +%page
   1.126 +
   1.127 +Component architecture
   1.128 +
   1.129 +
   1.130 +	zope.component part of Zope 3
   1.131 +
   1.132 +	allows glueing together of components in various ways
   1.133 +
   1.134 +	a component is an object which provides an interface
   1.135 +
   1.136 +	a Zope 2 object with a Zope 3 interface is a component
   1.137 +
   1.138 +%page
   1.139 +
   1.140 +Adapters, example
   1.141 +
   1.142 +
   1.143 +%size 4, fore "blue"
   1.144 +class INoiseMaker(Interface):
   1.145 +    """Something that makes noise.
   1.146 +    """
   1.147 +    def makeNoise():
   1.148 +         "Returns the noise that's made."
   1.149 +
   1.150 +%page
   1.151 +
   1.152 +Adapters, example continued
   1.153 +
   1.154 +
   1.155 +%size 4, fore "blue"
   1.156 +class ElephantNoiseMaker:
   1.157 +     """Adapts elephant to noise maker.
   1.158 +     """
   1.159 +     implements(INoiseMaker)
   1.160 +
   1.161 +     def __init__(self, context):
   1.162 +         self.context = context
   1.163 +
   1.164 +     def makeNoise(self):
   1.165 +         return self.context.trumpet()
   1.166 +
   1.167 +%page
   1.168 +
   1.169 +Adapters, example continued 2
   1.170 +
   1.171 +
   1.172 +%size 4, fore "blue"
   1.173 +>>> elephant = AfricanElephant()
   1.174 +>>> noise_maker = ElephantNoiseMaker(elephant)
   1.175 +>>> print noise_maker.makeNoise()
   1.176 +'A terrible racket'
   1.177 +
   1.178 +%page
   1.179 +
   1.180 +Adapters
   1.181 +
   1.182 +
   1.183 +	Add behavior to object without changing its class
   1.184 +
   1.185 +	More manageable than mixins
   1.186 +
   1.187 +	Define new behavior in terms of other behavior
   1.188 +
   1.189 +%page
   1.190 +
   1.191 +Adapters, continued
   1.192 +
   1.193 +
   1.194 +	Less framework burden on adapted objects
   1.195 +
   1.196 +	They only need to be a component
   1.197 +
   1.198 +	Adapted doesn't know about the adapter
   1.199 +
   1.200 +	Adapter is a component itself
   1.201 +
   1.202 +%page
   1.203 +
   1.204 +Adapter lookup
   1.205 +
   1.206 +
   1.207 +	We just manually glued the adapter to the adapted
   1.208 +
   1.209 +	What if we had INoiseMaker adapters for other objects?
   1.210 +
   1.211 +	We want a universal way to say: give me a INoiseMaker for this object
   1.212 +
   1.213 +	This allows use to write more generic code
   1.214 +
   1.215 +%page
   1.216 +
   1.217 +Adapter lookup, example
   1.218 +
   1.219 +
   1.220 +%size 4, fore "blue"
   1.221 +for animal in animal_farm:
   1.222 +    noise_maker = INoiseMaker(animal)
   1.223 +    print noise_maker.makeNoise()
   1.224 +
   1.225 +%page
   1.226 +
   1.227 +Adapter glueing
   1.228 +
   1.229 +
   1.230 +	System need to be informed what can adapt what
   1.231 +
   1.232 +	Zope Configuration Markup Language (ZCML) is used for that
   1.233 +
   1.234 +%page
   1.235 +
   1.236 +ZCML example
   1.237 +
   1.238 +
   1.239 +%size 4, fore "blue"
   1.240 +<configure xmlns="http://namespaces.zope.org/zope">
   1.241 +  <adapter
   1.242 +      for=".module.IElephant"
   1.243 +      provides=".module.INoiseMaker"
   1.244 +      factory=".module.ElephantNoiseMaker" />
   1.245 +  <adapter
   1.246 +      for=".other.IChicken"
   1.247 +      provides=".module.INoiseMaker"
   1.248 +      factory=".other.ChickenNoiseMaker" />
   1.249 +</configure>
   1.250 +
   1.251 +%page
   1.252 +
   1.253 +ZCML, what we just said
   1.254 +
   1.255 +
   1.256 +	The adapter ElephantNoiseMaker adapts any object that provides IElephant to a INoiseMaker
   1.257 +
   1.258 +	The adapter ChickenNoiseMaker adapts any object that provides IChicken to a INoiseMaker
   1.259 +
   1.260 +%page
   1.261 +
   1.262 +This works in Zope 2 with Five
   1.263 +
   1.264 +
   1.265 +	This works in Zope 2 with Five
   1.266 +
   1.267 +	Your objects just need to be components (provide Zope 3 interfaces)
   1.268 +
   1.269 +	Your ZCML goes into configure.zcml in your product
   1.270 +
   1.271 +	That's it