I have the following architecture:
+-----------------+
| Collection |
| |
| |
| |
| |
+-----------------+
| ^
| |
Create() | |
| |
v | GetItem(B)
+------------+ |
| Item A | |
| | |
| +------+
| |
+------------+
A class that manages a set of elements creates elements. And these items may need other items from the collection.
The actual code is in python, and so far Collectionpasses itself as a parameter to the generated one Item. As I see it, this is bad practice. The only improvement that I see is to pass in a few functions Collectionneeded for Item, instead of the whole instance.
For instance:
class Collection:
GetItem(self, id):
...
CreateItem(self, id):
item = Item(id, self)
...
class Item:
__init__(self, id, col):
...
col.GetItem(...)
How can I avoid passing self as a parameter? Or is this common use in Python?
More detailed example
ββββββββ ββββββββββββ βββββββ
βClientβ βCollectionβ βItemAβ
ββββ¬ββββ ββββββ¬ββββββ ββββ¬βββ
β UpdateItem(A, param)β β
β ββββββββββββββββββββ> β
β β β
β β Update(param, self)β βββββββββββββββββββββββββββββββ
β β βββββββββββββββββββ> βNeed to update linked ItemB ββ
β β β βββββββββββββββββββββββββββββββ
β β GetItem(B) β
β β <βββββββββββββββββββ
β β β
β β ItemB β
β β β β β β β β β β β >
β β β
β β ββββββ
β β β β UpdateItemB()
β β β<ββββ
ββββ΄ββββ ββββββ΄ββββββ ββββ΄βββ
βClientβ βCollectionβ βItemAβ
ββββββββ ββββββββββββ βββββββ
source
share