1 .. _adapters: |
1 .. _adapters: |
2 |
2 |
3 Interfaces and Adapters |
3 Interfaces and Adapters |
4 ----------------------- |
4 ----------------------- |
5 |
5 |
6 Interfaces are the same thing as object-oriented programming |
6 Interfaces are the same thing as object-oriented programming `interfaces`_. |
7 `interfaces`_. Adapter refers to a well-known `adapter`_ design |
7 Adapter refers to a well-known `adapter`_ design pattern that helps separating |
8 pattern that helps separating concerns in object oriented |
8 concerns in object oriented applications. |
9 applications. |
|
10 |
9 |
11 .. _`interfaces`: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html |
10 .. _`interfaces`: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html |
12 .. _`adapter`: http://en.wikipedia.org/wiki/Adapter_pattern |
11 .. _`adapter`: http://en.wikipedia.org/wiki/Adapter_pattern |
13 |
12 |
14 In |cubicweb| adapters provide logical functionalities |
13 In |cubicweb| adapters provide logical functionalities to entity types. They |
15 to entity types. They are introduced in version `3.9`. Before that one |
14 are introduced in version `3.9`. Before that one had to implement Interfaces in |
16 had to implement Interfaces in entity classes to achieve a similar goal. However, |
15 entity classes to achieve a similar goal. However, the problem with this |
17 hte problem with this approch is that is clutters the entity class's namespace, exposing |
16 approach is that is clutters the entity class's namespace, exposing name |
18 name collision risks with schema attributes/relations or even methods names |
17 collision risks with schema attributes/relations or even methods names |
19 (different interfaces may define the same method with not necessarily the same |
18 (different interfaces may define the same method with not necessarily the same |
20 behaviour expected). |
19 behaviour expected). |
21 |
20 |
22 Definition of an adapter is quite trivial. An excerpt from cubicweb |
21 Definition of an adapter is quite trivial. An excerpt from cubicweb |
23 itself (found in :mod:`cubicweb.entities.adapters`): |
22 itself (found in :mod:`cubicweb.entities.adapters`): |
126 def bar(self, *args): |
125 def bar(self, *args): |
127 raise NotImplementedError |
126 raise NotImplementedError |
128 |
127 |
129 class MyEntity(AnyEntity): |
128 class MyEntity(AnyEntity): |
130 __regid__ = 'MyEntity' |
129 __regid__ = 'MyEntity' |
131 __implements__ = AnyEntity.__implements__ + (IFoo,) |
130 __implements__ = AnyEntity.__implements__ + (IFoo,) |
132 |
131 |
133 def bar(self, *args): |
132 def bar(self, *args): |
134 return sum(captain.age for captain in self.captains) |
133 return sum(captain.age for captain in self.captains) |
135 |
134 |
136 class FooView(EntityView): |
135 class FooView(EntityView): |