@@ -8,10 +8,30 @@ def __init__(self, name: str):
8
8
self .name = name
9
9
10
10
11
- class Container :
12
- """A container class that uses comprehension to create components."""
11
+ class AssociationContainer :
12
+ """Type hints only - no ownership."""
13
+ # Association: just type hints, no actual assignment
14
+ components : list [Component ]
15
+ component_dict : dict [int , Component ]
16
+ components_set : set [Component ]
17
+ lazy_components : Generator [Component ]
18
+
19
+
20
+ class AggregationContainer :
21
+ """Comprehensions using existing objects - aggregation."""
22
+ def __init__ (self , existing_components : list [Component ]):
23
+ # Aggregation: comprehensions using existing objects (not creating)
24
+ self .components : list [Component ] = [comp for comp in existing_components ]
25
+ self .component_dict : dict [str , Component ] = {f"key_{ i } " : comp for i , comp in enumerate (existing_components )}
26
+ self .components_set : set [Component ] = {comp for comp in existing_components }
27
+ self .lazy_components : Generator [Component ] = (comp for comp in existing_components )
28
+
29
+
30
+ class CompositionContainer :
31
+ """Comprehensions creating new objects - composition."""
13
32
def __init__ (self ):
14
- self .components : list [Component ] = [Component (f"component_{ i } " ) for i in range (3 )] # list
15
- self .component_dict : dict [int , Component ] = {i : Component (f"dict_component_{ i } " ) for i in range (2 )} # dict
16
- self .components_set : set [Component ] = {Component (f"set_component_{ i } " ) for i in range (2 )} # set
17
- self .lazy_components : Generator [Component ] = (Component (f"lazy_{ i } " ) for i in range (2 )) # generator
33
+ # Composition: comprehensions creating new objects
34
+ self .components : list [Component ] = [Component (f"component_{ i } " ) for i in range (3 )]
35
+ self .component_dict : dict [int , Component ] = {i : Component (f"dict_component_{ i } " ) for i in range (2 )}
36
+ self .components_set : set [Component ] = {Component (f"set_component_{ i } " ) for i in range (2 )}
37
+ self .lazy_components : Generator [Component ] = (Component (f"lazy_{ i } " ) for i in range (2 ))
0 commit comments