The best design pattern for collecting objects

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) # <-- pass 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β”‚                              
 β””β”€β”€β”€β”€β”€β”€β”˜            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”˜                              
+4
source share
2 answers

: , - ! DDD, , .

, "":

DDD , .

, , , . , "", .

: , , . , , , . , : .

+1

Source: https://habr.com/ru/post/1682215/


All Articles