# Improving Observer Design Implementation for itk::Object.

**URL:** https://discourse.itk.org/t/improving-observer-design-implementation-for-itk-object/6645
**Category:** Engineering
**Created:** [May 20, 2024, 9:26am UTC](https://discourse.itk.org/t/improving-observer-design-implementation-for-itk-object/6645 "2024-05-20T09:26:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Piyush\_Aggarwal](https://discourse.itk.org/user_avatar/discourse.itk.org/piyush_aggarwal/32/4067_2.png) [@Piyush\_Aggarwal](https://discourse.itk.org/u/Piyush_Aggarwal)
#### Post date: [May 20, 2024, 9:26am UTC](https://discourse.itk.org/t/improving-observer-design-implementation-for-itk-object/6645/1 "2024-05-20T09:26:25Z")

</div>

Hello, everyone. I work with ITK at my day job. Upon inspection into the header of itkObject, I noticed there is a const API available for AddObserver but not for RemoveObserver. Since both APIs internally just call the mutable m\_SubjectImplementation’s RemoveObserver(), would it make sense to provide a const version of the RemoveObserver API just like AddObserver()?  
Here’s a patch, if you’d like to accept the proposal:-

```auto
index 3a63ffaa45..f2ba3d7dd5 100644
--- a/Modules/Core/Common/src/itkObject.cxx
+++ b/Modules/Core/Common/src/itkObject.cxx
@@ -494,6 +494,13 @@ Object::GetCommand(unsigned long tag)
void
Object::RemoveObserver(unsigned long tag)
+{
+ const auto & thisAsConst = *this;
+ return thisAsConst.RemoveObserver(tag);
+}
+
+void
+Object::RemoveObserver(unsigned long tag) const
{
if (this->m_SubjectImplementation)
{
    this->m_SubjectImplementation->RemoveObserver(tag);
  }
}

```

---

<div class="post-metadata">

### Author: ![Piyush\_Aggarwal](https://discourse.itk.org/user_avatar/discourse.itk.org/piyush_aggarwal/32/4067_2.png) [@Piyush\_Aggarwal](https://discourse.itk.org/u/Piyush_Aggarwal)
#### Post date: [May 20, 2024, 9:32am UTC](https://discourse.itk.org/t/improving-observer-design-implementation-for-itk-object/6645/2 "2024-05-20T09:32:35Z")

</div>

Here’s the link to RemoveObserver() API on GitHub:  
[ITK/Modules/Core/Common/src/itkObject.cxx at master · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/src/itkObject.cxx#L496)

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [May 20, 2024, 4:02pm UTC](https://discourse.itk.org/t/improving-observer-design-implementation-for-itk-object/6645/3 "2024-05-20T16:02:34Z")

</div>

PR xref:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/4685>
>
> Exposes a const version of \`RemoveObserver() API\` just like the already existing… \`AddObserver()\` API \[here\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/src/itkObject.cxx#L467) . 
> 
> Helps with application architecture when building observer design pattern with itkObject as subject.
> 
> @mentions: @thewtex - I apologize for mentioning you without any sort of prior discussion. I'm a new contributor, and would be very grateful for some feedback on this PR!
> 
> \## PR Checklist
> \- \[\] No \[API changes\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md#breaking-changes) were made (or the changes have been approved)
> \- \[X\] No \[major design changes\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md#design-changes) were made (or the changes have been approved)
> \- \[\] Added test (or behavior not changed)
> \- \[\] Updated API documentation (or API not changed)
> \- \[\] Added Python wrapping to new files (if any) as described in \[ITK Software Guide\](https://itk.org/ItkSoftwareGuide.pdf) Section 9.5
> \- \[\] Added \[ITK examples\](https://github.com/InsightSoftwareConsortium/ITKSphinxExamples) for all new major features (if any)
> 
> Thanks for reviewing this change!
