0

It's quite difficult to have a good title of my question. From what I understand, the Adapter is to add more services to the components without changing it. The adapter can extends services from multiple components.

But what about the dependency between component? How can I set the dependency between the component A (Person) and the component B (Task) like this normal Python code

class Person:
 pass

class Task:
 def __init__(self, person):
  self.owner = person

If I implements the 2 classes

from zope.interface import Interface
from zope.interface import implements

class IPerson(Interface):
 pass

class Person(object):
 implements(IPerson)

class ITask(Interface):
 pass

class Task(object):
 implements(ITask)
 def __init__(self, person):
  self.owner = person

Is it a good implementation?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
nam
  • 3,542
  • 9
  • 46
  • 68

1 Answers1

1

The point is that with the ZCA, you don't set a dependency to a concrete object. You use utilities instead.

Object A would implement an interface, and you look up the interface in B to find a concrete implementation, which could be A, but that depends on the register.

Note that the ZCA is meant to allow you to plug different implementations for a given interface, and is not always needed.

If your Task object expects a specific type, then document that. Using the ZCA is not a requirement here. At most, you can try to adapt the passed in value to IPerson; if the passed in object already implements that interface, that action is a no-op:

class Task(object):
    implements(ITask)

    def __init__(self, owner):
        self.owner = IPerson(owner)

That would allow you the flexibility of passing in something else later, something that is not a Person itself, but could be adapted to that interface.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I've updated my question above. My question is, the instance A needs an instance of B. If I use the utility, how can I know that this instance of B proceeded by the register service corresponds to A – nam Oct 12 '12 at 15:04
  • 1
    @HOAINAMNGUYEN: You are talking about API contracts, really, not the component architecture. The ZCA doesn't *enforce* interfaces, it just uses them as (powerful) registration and lookup keys. The fact that your Task object can only work with IPerson objects is something you'd document, the ZCA won't enforce that dependency for you. – Martijn Pieters Oct 12 '12 at 15:10