# Observer: How is better?

**URL:** <https://forums.sketchup.com/t/observer-how-is-better/346032>\
**Category:** Ruby API\
**Tags:** observer\
**Created:** [April 9, 2026, 10:28am UTC](https://forums.sketchup.com/t/observer-how-is-better/346032 "2026-04-09T10:28:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [April 9, 2026, 10:28am UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/1 "2026-04-09T10:28:13Z")

</div>

There are several instances of class (items) that are crated and managed by a Tool class instance (manager). Every item must react to the view being changed.

Which is better?  
A) Add an instance of the observer to each item?  
B) Add an instance of the observer to the Tool instance and “inform” items about the change?

_In the examples, for simplicity, I create 100 items during initialization, and the reaction of the items is a simple count._

Case A:

```ruby
module Dezmo
  class MyItemA
    def initialize
      @my_variable = 0
      start_observing
    end
    
    def onViewChanged(view)
      @my_variable += 1
    end
    
    def start_observing
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
      model.active_view.add_observer(self)
    end
    
    def stop_observing 
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
    end
  end
  
  class MyItemManagerToolA
    def initialize
      @my_items = {}
      100.times{|i|
         @my_items[i] = Dezmo::MyItemA.new
      }
    end
    
    def deactivate(view)
      @my_items.each{ |item| item.stop_observing }
    end
    
    def manage_items_method_1
       # ...and so on
    end
  end
end

```

Case B

```ruby
module Dezmo
  class MyItemB  
    def initialize
      @my_variable = 0
    end
    
    def view_changed(view)
       @my_variable += 1
    end
  end
  
  class MyItemManagerToolB
    def initialize
      @my_items = {}
      100.times{|i|
         @my_items[i] = Dezmo::MyItemB.new
      }
      start_observing
    end
    
    def deactivate(view)
      stop_observing
    end
    
    def onViewChanged(view)
      @my_items.each{ |item| item.view_changed(view) }
    end
    
    def start_observing
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
      model.active_view.add_observer(self)
    end
    
    def stop_observing 
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
    end
    
    def manage_items_method_1
       # ...and so on
    end
  end
end

```

Other quiestions:  
If I replace e.g. `@my_items[50] = Dezmo::MyItemA.new`  
What happens to the item that was previously assigned to `@my_items[50]`?  
What happens to its observer if it was assigned to it and I did not remove it?  
Does the garbage collection mechanism remove them?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 9, 2026, 5:33pm UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/2 "2026-04-09T17:33:51Z")

</div>

> [@dezmo](#):
>
> If I replace e.g. `@my_items[50] = Dezmo::MyItemA.new`  
> What happens to the item that was previously assigned to `@my_items[50]`?

It becomes **unreferenced** and will be garbage collected the next time [`GC`](https://docs.ruby-lang.org/en/3.2/GC.html) runs.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 9, 2026, 6:06pm UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/3 "2026-04-09T18:06:01Z")

</div>

> [@dezmo](#):
>
> What happens to its observer if it was assigned to it and I did not remove it?  
> Does the garbage collection mechanism remove them?

I think that the SketchUp observer queue disposes of observers attached to invalid objects. The observer queue will be holding a reference to `MyItemA` which might prevent immediate garbage collection when your code stops referencing it, so the `stop_observing()` method has benefit.

* * *

Of the two scenarios, I’d prefer **B**. But I also would likely key the hash with object IDs or PIDs if they are `Drawingelement` subclass objects. You can also key the hash with object references as well which are also unique.

If the things to be watched are model objects, you could just insert `@var`s into the entity instances using the [`instance_variable_set()`](https://docs.ruby-lang.org/en/3.2/Object.html#method-i-instance_variable_set) method.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [April 9, 2026, 6:38pm UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/4 "2026-04-09T18:38:07Z")

</div>

I also like better B (less observers).

> [@DanRathbun](#):
>
> If the things to be watched are model objects, you could just insert `@var`s into the entity instances using the [`instance_variable_set()`](https://docs.ruby-lang.org/en/3.2/Object.html#method-i-instance_variable_set) method.

No. There are a lot more to do inside in one item. I just put an example that need a calculation if view changes.  
These are not a `Drawingelement`, but It is a holding a calculated points of let say, shapes, however I also can call it as my own virtual Drawingelement, because it will drawn by a Tool (or Overlay).  
Some have the property that they always seem to be the same size (like the protractor), so I have to watch when the view changes and calculate a new transformation.

---

<div class="post-metadata">

**Author:** ![CAUL](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@CAUL](https://forums.sketchup.com/u/CAUL)\
**Post date:** [April 15, 2026, 11:49am UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/5 "2026-04-15T11:49:54Z")

</div>

> [@dezmo](#):
>
> There are several instances of class (items) that are crated and managed by a Tool class instance (manager). Every item must react to the view being changed.

If you are inside a tool I would use draw(view) to track changes. That would get rid of the observer altogether.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [April 15, 2026, 12:20pm UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/6 "2026-04-15T12:20:21Z")

</div>

The thing is, there’s quite a lot of calculation (can be) involved, so I’d rather calculate it in advance and have # draw(…) just draw the \*points (cached in instance variable).

I have the feeling (but I have no idea) that `view.draw(...)` is called more often than the view observer changes (?)

---

<div class="post-metadata">

**Author:** ![Fredo6](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/fredo6/32/77024_2.png) [@Fredo6](https://forums.sketchup.com/u/Fredo6)\
**Post date:** [April 15, 2026, 4:41pm UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/7 "2026-04-15T16:41:35Z")

</div>

I woul use B, that is, an observer attached to the tool, created at ``activate`` and removed at ``deactivate``.

Another option is to store the view parameters (eye, target, etc…) in `draw` and compare them each time. If they are different, then you perform the view-dependent calculation. Otherwise, you used the cached value. `draw` is called anyway when the view changes.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [April 15, 2026, 6:13pm UTC](https://forums.sketchup.com/t/observer-how-is-better/346032/8 "2026-04-15T18:13:27Z")

</div>

I wrote a test code to compare the number of calls in draw vs. observer, Tool vs. Overlay.

1. animation: I change the view with the mouse wheel or the middle mouse button (Orbit Tool).  
In Overlay, the draw method is called more times than the observer. If in Tool - obviously when using the Orbit Tool, MyTool is suspended - it does not call draw.

2. animation: I change the view with 3Dconnection mouse.  
There is no really significant difference in the number of draw or observer calls.

It seems that in Tool it is better to calculate in the draw method than in the Observer.  
With Overlay it is almost the same.

Is there something I did not take into account?

```ruby
module Dezmo
  class MyOv < Sketchup::Overlay
    def initialize
      super('example_inc.my_ov', 'TestOv')
    end
    def onViewChanged(view)
      @viewobs_count += 1
    end
    def start
      @draw_count = 0
      @viewobs_count = 0
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
      model.active_view.add_observer(self)
    end
    def stop
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
    end
    def draw(view)
       @draw_count += 1
       view.draw_text([10,60], 
                      "Odraw_count:#{@draw_count}\nOviewobs_count:#{@viewobs_count}",
                      size:16,
                      bold:true,
                      color:"red")
    end
  end
  ov = MyOv.new
  Sketchup.active_model.overlays.add(ov)

  class MyTool
    def initialize
      @draw_count = 0
      @viewobs_count = 0
      start_observing
    end
    def deactivate(view)
      stop_observing
    end
    def resume(view)
      view.invalidate
    end
    def onViewChanged(view)
      @viewobs_count += 1
    end
    def start_observing
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
      model.active_view.add_observer(self)
    end

    def stop_observing 
      model = Sketchup.active_model
      model.active_view.remove_observer(self)
    end

    def draw(view)
       @draw_count += 1
       view.draw_text([10,10], 
                      "draw_count:#{@draw_count}\nviewobs_count:#{@viewobs_count}",
                      size:16,
                      bold:true)
    end
  end
  Sketchup.active_model.select_tool(MyTool.new)
end

```

![ovvsdraw](https://global.discourse-cdn.com/sketchup/original/4X/4/5/0/4502dee5fdd0d5360901c78697d16d045f56f1fb.gif)

![ovvsdraw3Dcon](https://global.discourse-cdn.com/sketchup/original/4X/4/0/5/405db1624316ef6281158f501bf5d00a8d8e6ba0.gif)
