[Checkins] SVN: zope2docs/trunk/zdgbook/source/ComponentsAndInterfaces.rst Use zope.interface

Baiju M baiju.m.mail at gmail.com
Tue Oct 6 03:54:59 EDT 2009


Log message for revision 104832:
  Use zope.interface
  

Changed:
  U   zope2docs/trunk/zdgbook/source/ComponentsAndInterfaces.rst

-=-
Modified: zope2docs/trunk/zdgbook/source/ComponentsAndInterfaces.rst
===================================================================
--- zope2docs/trunk/zdgbook/source/ComponentsAndInterfaces.rst	2009-10-06 06:21:25 UTC (rev 104831)
+++ zope2docs/trunk/zdgbook/source/ComponentsAndInterfaces.rst	2009-10-06 07:54:59 UTC (rev 104832)
@@ -2,11 +2,10 @@
 Components and Interfaces
 #########################
 
-Zope is becoming a component system.  Zope components will be Python
-objects with interfaces that describe them.  Right now only some of
-the Zope code base uses interfaces.  In coming releases more and more
-of Zope will include interfaces.  As a Zope developer you can use
-interfaces right now to build your Zope components.
+Zope uses a component architecture internally in many places.  Zope
+components is nothing but Python objects with interfaces that
+describe them.  As a Zope developer you can use interfaces right now
+to build your Zope components.
 
 Zope Components
 ===============
@@ -21,38 +20,39 @@
 components, this one generally consists of two pieces, an interface,
 and an implementation::
 
-  from Interface import Base
+  from zope.interface import Interface
+  from zope.interface import implements
 
-  class Hello(Base):
+  class IHello(Interface):
       """The Hello interface provides greetings."""
 
-      def hello(self, name):
-          """ Say hello to the name """
+      def hello(name):
+          """Say hello to the name"""
 
-  class HelloComponent:
+  class HelloComponent(object):
 
-      __implements__ = Hello
+      implements(IHello)
 
       def hello(self, name):
           return "hello %s!" % name
 
 Let's take a look at this step by step.  Here, you see two Python
-class statements.  The first statement creates the *interface*, and
-the second statement creates the *implementation*.
+class statements.  The first class statement creates the *interface*,
+and the second class statement creates the *implementation*.
 
-The first class statement creates the 'Hello' interface.  This
+The first class statement creates the 'IHello' interface.  This
 interface describes one method, called 'hello'.  Notice that there is
 no implementation for this method, interfaces do not define behavior,
 they just describe a specification.
 
 The second 'class' statement creates the 'HelloComponent' class.
-This class is the actual component that *does* what 'Hello'
+This class is the actual component that *does* what 'IHello'
 *describes*.  This is usually referred to as the *implementation* of
-'Hello'.  In order for you to know what interfaces 'HelloComponent'
+'IHello'.  In order for you to know what interfaces 'HelloComponent'
 implements, it must somehow associate itself with an interface.  The
-'__implements__' class attribute does just that.  It says, "I
-implement these interfaces".  In this case, 'HelloComponent' asserts
-that it implements one interface, 'Hello'.
+'implements' function call inside the class does just that.  It says,
+"I implement these interfaces".  In this case, 'HelloComponent'
+asserts that it implements one interface, 'IHello'.
 
 The interface describes how you would work with the object, but it
 doesn't dictate how that description is implemented.  For example,
@@ -61,7 +61,7 @@
       import xmlrpclib
       class XMLRPCHello:
 
-          __implements__ = Hello
+          implementats(IHello)
 
           def hello(self, name):
               """
@@ -150,30 +150,30 @@
 and that the resulting object is an *interface*, not a class.
 
 To create an interface object using Python's class syntax, create a
-Python class that subclasses from 'Interface.Base'::
+Python class that subclasses from 'zope.interface.Interface'::
 
-  from Interface import Base
+  from zope.interface import Interface
 
-  class Hello(Base):
+  class IHello(Interface):
 
-      def hello(self, name):
+      def hello(name):
           """Say hello to the world"""
 
 This interface does not implement behavior for its methods, it just
 describes an interface that a typical "Hello" object would realize.
-By subclassing the 'Interface.Base' interface, the resulting object
-'Hello' is an interface object. The Python interpreter confirms
-this::
+By subclassing the 'zope.interface.Interface' interface, the
+resulting object 'Hello' is an interface object. The Python
+interpreter confirms this::
 
-  >>> Hello
-  <Interface Hello at 812cbd4>
+  >>> IHello
+  <InterfaceClass __main__.IHello>
 
 Now, you can associate the 'Hello' Interface with your new, concrete
 class in which you define your user behavior. For example::
 
   class HelloComponent:
 
-      __implements__ = Hello
+      implements(IHello)
 
       def hello(self, name):
           return "Hello %s!" % name
@@ -188,67 +188,44 @@
 
   class HelloComponent:
 
-      __implements__ = Hello, Item
+      implements(IHello, IItem)
 
-This '__implements__' attribute is called an *interface assertion*.
-An interface assertion can be either an interface, or a sequence of
-interface assertions. Here's a more complex example::
 
-  class Sandwich:
-
-      __implements__ = (Food, (Nourishing, Delicious), (GetsStaleQuickly, 
-                       (EdibleWithHands, GoodForLunch)))
-
-
-Interface assertions allow complex nesting of interfaces. This is
-mostly useful when you wish to assert that your class implements some
-specific interfaces, along with whatever interfaces your base class
-implements::
-
-   class Sandwich(Food):
-
-       __implements__ = (EdibleWithHands, GoodForLunch, Food.__implements__)
-
-Take care before you assert that your class implements the interfaces
-of your base classes.
-
-
 The Interface Model
 ===================
 
 Interfaces can extend other interfaces.  For example, let's extend
-the 'Hello' interface by adding an additional method::
+the 'IHello' interface by adding an additional method::
 
-  class SmartHello(Hello):
+  class ISmartHello(IHello):
       """A Hello object that remembers who it's greeted"""
 
       def lastGreeted(self):
           """Returns the name of the last person greeted."""
 
 
-'SmartHello' extends the 'Hello' interface.  It does this by using
+'ISmartHello' extends the 'IHello' interface.  It does this by using
 the same syntax a class would use to subclass another class.
 
-
-Now, you can ask the 'SmartHello' for a list of the interfaces it
+Now, you can ask the 'ISmartHello' for a list of the interfaces it
 extends with 'getBases'::
 
-  >>> SmartHello.getBases()
-  [<interface Hello at 80c72c8>]
+  >>> ISmartHello.getBases()
+  (<InterfaceClass __main__.IHello>,)
 
-
 An interface can extend any number of other interfaces, and
 'getBases' will return that list of interfaces for you.  If you want
-to know if 'SmartHello' extends any other interface, you could call
+to know if 'ISmartHello' extends any other interface, you could call
 'getBases' and search through the list, but a convenience method
 called 'extends' is provided that returns true or false for this
 purpose::
 
-  >>> SmartHello.extends(Hello)
-  1
-  >>> SmartHello.extends(Sandwich)
-  0
-  >>>
+  >>> ISmartHello.extends(IHello)
+  True
+  >>> ISandwich(Interface):
+  ...     pass
+  >>> ISmartHello.extends(ISandwich)
+  False
 
 Here you can see 'extends' can be used to determine if one interface
 extends another.
@@ -289,9 +266,9 @@
 For example::
 
   >>> User.namesAndDescriptions()
-  [('getUserName', <Interface.Method.Method instance at 80f38f0>),
-  ('getFavoriteColor', <Interface.Method.Method instance at 80b24f0>),
-  ('getPassword', <Interface.Method.Method instance at 80fded8>)]
+  [('getUserName', <Interface.Method.Method object at 80f38f0>),
+  ('getFavoriteColor', <Interface.Method.Method object at 80b24f0>),
+  ('getPassword', <Interface.Method.Method object at 80fded8>)]
 
 As you can see, the "description" of the Interface's three items in
 these cases are all 'Method' objects.  Description objects can be
@@ -315,7 +292,7 @@
 
   >>> m=User.namesAndDescriptions()[0][1]
   >>> m
-  <Interface.Method.Method instance at 80f38f0>
+  <Interface.Method.Method object at 80f38f0>
   >>> m.getSignatureString()
   '(fullName=1)'
   >>> m.getSignatureInfo()   
@@ -333,13 +310,13 @@
 it implements that interface.  For example, say you want to know if
 instances of the 'HelloComponent' class implement 'Hello'::
 
-  Hello.implementedByInstancesOf(HelloComponent)
+  IHello.implementedBy(HelloComponent)
 
 This is a true expression.  If you had an instance of
 'HelloComponent', you can also ask the interface if that instance
 implements the interface::
 
-  Hello.implementedBy(my_hello_instance)
+  IHello.implementedBy(my_hello_instance)
 
 This would also return true if *my_hello_instance* was an instance of
 *HelloComponent*, or any other class that implemented the *Hello*



More information about the checkins mailing list