python - Best design pattern for collection of objects -


i have following architecture:

         +-----------------+          |   collection    |          |                 |          |                 |          |                 |          |                 |          +-----------------+            |        ^            |        |   create() |        |            |        |            v        | getitem(b) +------------+      | |   item   |      | |            |      | |            +------+ |            | +------------+ 

a class managing collection of items, creates items. , these items may need other items collection.

the actual code in python, , collection passes parameter created item. see it, bad practice. improvement can see pass few functions of collection needed item, instead of whole instance.

for example:

class collection:     getitem(self, id):         ...     createitem(self, id):         item = item(id, self) # <-- pass self         ...  class item:     __init__(self, id, col):         ...         col.getitem(...) 

how can avoid passing self parameter? or common usage in python?

more detailed example

 ┌──────┐            ┌──────────┐           ┌─────┐                                │client│            │collection│           │itema│                                └──┬───┘            └────┬─────┘           └──┬──┘                                   │ updateitem(a, param)│                    │                                      │ ────────────────────>                    │                                      │                     │                    │                                      │                     │ update(param, self)│  ╔═════════════════════════════╗     │                     │ ───────────────────>  ║need update linked itemb ░║     │                     │                    │  ╚═════════════════════════════╝     │                     │     getitem(b)     │                                      │                     │ <───────────────────                                      │                     │                    │                                      │                     │        itemb       │                                      │                     │  ─ ─ ─ ─ ─ ─ ─ ─ ─ >                                      │                     │                    │                                      │                     │                    │────┐                                 │                     │                    │    │ updateitemb()                   │                     │                    │<───┘                              ┌──┴───┐            ┌────┴─────┐           ┌──┴──┐                                │client│            │collection│           │itema│                                └──────┘            └──────────┘           └─────┘                               

a non-answer: given current level of detail, recommending specific pattern wrong approach! coming ddd perspective, wondering if should rather step , should @ model @ whole.

it looks mixing @ least two responsibilities within "collection" class:

  • knowledge create objects
  • and update them

ddd suggests have factories responsible creating objects.

and instead of thinking of collection provides access all kinds of entries, might rather define aggregates, , identify root objects. because want make sure there 1 defined "path" information.

in other words: should strive design model provides helpful abstractions real world elements dealing with. , current implementation feels evolved out of technical ideas how organize things. recommendation: step , @ big picture.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -