The Dependency Mechanism - part 2

From Cuis CookBook
Jump to navigation Jump to search
  • In Cuis-6 there isn't anymore a Model, there instead an ActiveModel class, but the name 'model' a very frequent in the code. You can still find Model in Squeak-5.3 and VisualWorks-8.3 if you wish to check out some implementation.
  • We have seen (here) how to manage Dependency with the Parent / Dependent strategy. But where do all the relations involving objects are stored ?
  • There relations are stored by default in the class variable DependentsFields of the class Object which is a specialized dictionary, where at every key corresponds an object who is Parent and its values are a list of Dependents objects.
  • You can well understand that if there are a lot of dependencies the dictionary in DependentsFields becomes very large and also, there is another problem. If a dependent lost all references in the VM except for a forgotten relationship link in DependentsFields it will stay there and will not be garbage collected.
  • To avoid these two problems, and for performance reasons, it was introduced the class Model which is a subclass of Object and a has an instance variable field called dependents where all the dependents are stored.
  • In this way, if objA is a intended to be a Parent and you make it as a Model subclass instance,  all its depency relationships will be localized into objA itself avoiding a large centralized permanent dictionary and also, once you delete objA, you are sure all its dependents will be free to be garbage collected, when necessary.