<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://95.179.246.60/mediawiki/index.php?action=history&amp;feed=atom&amp;title=The_Dependency_Mechanism_-_part_1</id>
	<title>The Dependency Mechanism - part 1 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://95.179.246.60/mediawiki/index.php?action=history&amp;feed=atom&amp;title=The_Dependency_Mechanism_-_part_1"/>
	<link rel="alternate" type="text/html" href="http://95.179.246.60/mediawiki/index.php?title=The_Dependency_Mechanism_-_part_1&amp;action=history"/>
	<updated>2026-05-08T12:45:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>http://95.179.246.60/mediawiki/index.php?title=The_Dependency_Mechanism_-_part_1&amp;diff=182&amp;oldid=prev</id>
		<title>Nmingott: imported material</title>
		<link rel="alternate" type="text/html" href="http://95.179.246.60/mediawiki/index.php?title=The_Dependency_Mechanism_-_part_1&amp;diff=182&amp;oldid=prev"/>
		<updated>2025-05-12T20:41:42Z</updated>

		<summary type="html">&lt;p&gt;imported material&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;* Smalltalk has a way to keep a relation between objects in the sense that, if &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; changes then &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039; gets immediately informed about it.&lt;br /&gt;
* The classic way for an object to keep updated about another object state is &amp;#039;&amp;#039;&amp;#039;polling&amp;#039;&amp;#039;&amp;#039;, that is &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039; checks every X milliseconds the state of &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039;. But this is very much resource consuming and, if possible, it should be avoided. Smalltalk has more sophisticated ways to solve this problem.&lt;br /&gt;
* The first thing you need to know is that there is a &amp;#039;&amp;#039;&amp;#039;classic mechanism&amp;#039;&amp;#039;&amp;#039; to do it but in &amp;#039;&amp;#039;&amp;#039;Cuis&amp;#039;&amp;#039;&amp;#039; it has been &amp;#039;&amp;#039;&amp;#039;obsoleted&amp;#039;&amp;#039;&amp;#039;. We will look at it in any case, because you can find it in books.&lt;br /&gt;
&lt;br /&gt;
=== The Classic Mechanism of Parent and Dependent ===&lt;br /&gt;
&lt;br /&gt;
* I red about this mechanism in the two books; the content there has to be adapted to run in Cuis but there is still some good interesting contents.&lt;br /&gt;
** &amp;quot;Smalltalk by Example&amp;quot; by Alec Sharp, see Chapter 19. pg.149.&lt;br /&gt;
** &amp;quot;Smalltalk an introduction to application development with VisualWorks&amp;quot; by Hopkins &amp;amp; Horan, see Chapter 27, pg.287.&lt;br /&gt;
* We will explain this mechanism with the simplest example possible&lt;br /&gt;
* There are two objects, &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039; which actually contains only just a number in the instance variable &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt;.&lt;br /&gt;
* We want that when &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; changes its &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt; to, say, 10, then &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039; value must become automatically 20, that is, it must double &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; value.&lt;br /&gt;
* So let&amp;#039;s define 2 classes and a few methods, very simple stuff:&lt;br /&gt;
&lt;br /&gt;
 Object subclass: MyParentNumber&lt;br /&gt;
     instanceVariableNames: &amp;#039;value&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
 MyParentNumber &amp;gt;&amp;gt; initialize&lt;br /&gt;
     value := 0 &lt;br /&gt;
     ^ self &lt;br /&gt;
 &lt;br /&gt;
 Object subclass: MyDependentNumber&lt;br /&gt;
     instanceVariableNames: &amp;#039;value&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
 MyDependentNumber &amp;gt;&amp;gt; initialize&lt;br /&gt;
     value := 0 &lt;br /&gt;
     ^ self&lt;br /&gt;
 &lt;br /&gt;
 MyDependentNumber &amp;gt;&amp;gt; update: aParameter&lt;br /&gt;
     |outStr|&lt;br /&gt;
     outStr := &amp;#039;One of my parent changed and toldme &amp;quot;{1}&amp;quot; ! I may do something !&amp;#039; format: {aParam. }. &lt;br /&gt;
     Transcript show: outStr .          &lt;br /&gt;
&lt;br /&gt;
* It is all classic except for the &amp;lt;code&amp;gt;update: aParameter&amp;lt;/code&amp;gt;. That is the method that will be called automatically when &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; will get updated.&lt;br /&gt;
* So let&amp;#039;s now create &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; ,  &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039; and make &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039; dependent on &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 objA _ MyParentNumber new.&lt;br /&gt;
 objB _ MyDependentNumber new.  &lt;br /&gt;
 &lt;br /&gt;
 &amp;quot;. this makes the connection, the dependency relation &amp;quot;&lt;br /&gt;
 objA addDependent: objB. &lt;br /&gt;
&lt;br /&gt;
* Now, open your Transcript and run this&lt;br /&gt;
&lt;br /&gt;
 objA changed: #foobar.&lt;br /&gt;
&lt;br /&gt;
* You will notice that in the transcript there is written &amp;quot;One of my parent changed and told me &amp;quot;foobar&amp;quot; ! I may do something !&amp;quot;&lt;br /&gt;
* Aha ! It works. You sent a message &amp;lt;code&amp;gt;changed:#aParameter&amp;lt;/code&amp;gt; to &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; and the message &amp;lt;code&amp;gt;update:aParameter&amp;lt;/code&amp;gt; was sent to &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
* Now we have all the pieces to complete our objective, we just need to glue them together.&lt;br /&gt;
* Before that, let&amp;#039;s &amp;#039;&amp;#039;&amp;#039;break the connection&amp;#039;&amp;#039;&amp;#039; between &amp;#039;&amp;#039;&amp;#039;objA&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;objB&amp;#039;&amp;#039;&amp;#039; and show that nothing happens anymore.&lt;br /&gt;
&lt;br /&gt;
 objA removeDependent: objB.&lt;br /&gt;
 objA changed: #foobar. &lt;br /&gt;
 objA changed: #foobar. &lt;br /&gt;
 objA changed: #foobar. &lt;br /&gt;
 &amp;quot; ... nothing intereseting appears into the Transcript now&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* So, here is the new version of the code, the code for the new classes MyParentNumber.st and MyDependentNumber.st is available for download and file-in. &lt;br /&gt;
&lt;br /&gt;
 Object subclass: MyParentNumber&lt;br /&gt;
     instanceVariableNames: &amp;#039;value&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
 MyParentNumber &amp;gt;&amp;gt; initialize&lt;br /&gt;
     value := 0 &lt;br /&gt;
     ^ self &lt;br /&gt;
 &lt;br /&gt;
 MyParentNumber &amp;gt;&amp;gt; value&lt;br /&gt;
     ^ value &lt;br /&gt;
 &lt;br /&gt;
 MyParentNumber &amp;gt;&amp;gt; value: aNumber&lt;br /&gt;
      value := aNumber.&lt;br /&gt;
      &amp;quot;when i change, i inform my dependents that i did, they will check what changed.&lt;br /&gt;
       the value of the parameter #iChanged is insignificant. &amp;quot;&lt;br /&gt;
      self changed: #iChanged. &lt;br /&gt;
 &lt;br /&gt;
 Object subclass: MyDependentNumber&lt;br /&gt;
     instanceVariableNames: &amp;#039;value parent&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
 MyDependentNumber Class &amp;gt;&amp;gt; newWithParent: anObject&lt;br /&gt;
     ^ super new initializeWithParent: anObject&lt;br /&gt;
 &lt;br /&gt;
 MyDependentNumber &amp;gt;&amp;gt; initializeWithParent: anObject&lt;br /&gt;
     value := 0.&lt;br /&gt;
     parent := anObject . &lt;br /&gt;
     parent addDependent: self.&lt;br /&gt;
     ^ self &lt;br /&gt;
 &lt;br /&gt;
 MyDependentNumber &amp;gt;&amp;gt; value&lt;br /&gt;
    ^ value &lt;br /&gt;
 &lt;br /&gt;
 MyDependentNumber &amp;gt;&amp;gt; update: aParameter&lt;br /&gt;
     |vPar vMe |&lt;br /&gt;
     &amp;quot;. i don&amp;#039;t check the parameter, since I know that i am bound to only one Parent and i know &lt;br /&gt;
        i can read its state when he tells me that he changed. &amp;quot;&lt;br /&gt;
     vPar := parent value.  &lt;br /&gt;
     vMe := vPar * 2.&lt;br /&gt;
     value := vMe.&lt;br /&gt;
     Transcript show: (&amp;#039;my parent value is:{1} and mine is value:{2}&amp;#039; format: {vPar. vMe} ).  &lt;br /&gt;
&lt;br /&gt;
* Let&amp;#039;s try it out&lt;br /&gt;
&lt;br /&gt;
 objA _ MyParentNumber new.&lt;br /&gt;
 objB _ MyDependentNumber newWithParent: objA. &lt;br /&gt;
 &lt;br /&gt;
 objA value.   &amp;quot;=&amp;gt; 0&amp;quot;&lt;br /&gt;
 objB value .  &amp;quot;=&amp;gt; 0&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 objA value: 10.  &lt;br /&gt;
 objB value.        &amp;quot; =&amp;gt; 20&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* We see it works and we are very happy with that.&lt;br /&gt;
* Now the sour part, this mechanism is &amp;#039;&amp;#039;&amp;#039;deprecated&amp;#039;&amp;#039;&amp;#039; in &amp;#039;&amp;#039;&amp;#039;Cuis&amp;#039;&amp;#039;&amp;#039;. The methods &amp;lt;code&amp;gt;addDependent:&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;changed&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;update:&amp;lt;/code&amp;gt; ... are defined in &amp;#039;&amp;#039;&amp;#039;Object&amp;#039;&amp;#039;&amp;#039;. And if you look at their implementation you will see the mechanism with is used now, called &amp;#039;&amp;#039;&amp;#039;Observer pattern&amp;#039;&amp;#039;&amp;#039;, which we will see in another page.&lt;/div&gt;</summary>
		<author><name>Nmingott</name></author>
	</entry>
</feed>