Observer pattern in Rust – driven by intrinsic motivation…

Work is worship…

The observer design pattern is a very popular design pattern in the Object Oriented world. I must admit, I first saw the usefulness of this design pattern while studying the document view architecture of the MFC source…


This content originally appeared on DEV Community and was authored by Somenath Mukhopadhyay

Work is worship...

The observer design pattern is a very popular design pattern in the Object Oriented world. I must admit, I first saw the usefulness of this design pattern while studying the document view architecture of the MFC source code.

Later on, I used this pattern in many places.

There is a lot of similarity between the Observer Pattern, the Callback mechanism, and the Event Handler pattern in Java. Usually, the callback method is used when there is only one observer who awaits the signal from the subject.

So, let me put it in this fashion.

Suppose, there is a central document that is viewed by a few applications - someone is viewing it in a spreadsheet, someone as a Pie chart, and so on.

Now if the data in the document is updated, all the viewers must be updated and they should synchronise their views with the latest data set. So basically all the viewers were observing the central data. The moment it changes, all the observers get their respective views updated.

The class diagram and the sequence diagram of the observer pattern will be as follows.

Class Diagram

Class Diagram

Sequence Diagram

Sequence Diagram

Here goes an example of Observer Pattern written in Rust.

trait Observer {

fn update(&self,data:&str);

}

struct Subject<'a> {

observers: Vec<&'a dyn Observer>,

state: String,

}

impl<'a> Subject<'a> {

fn new(state: String) -> Self {

Self {

observers: Vec::new(),

state: state,

}

}

fn attach(&mut self, observer: &'a dyn Observer) {

self.observers.push(observer);

}

fn detach(&mut self, observer: &dyn Observer) {

self.observers.retain(|o| !std::ptr::eq(*o, observer));

}

fn notify(&self) {

for o in &self.observers {

o.update(&self.state);

}

}

fn set_state(&mut self, state: String) {

self.state = state;

self.notify();

}

}

struct ConcreteObserver {

name: String,

}

impl Observer for ConcreteObserver {

fn update(&self,data:&str) {

println!("{} received data: {}",self.name,data);

}

}

fn main() {{}

let mut subject = Subject::new("initial data".to_string());

let observer1=ConcreteObserver {

name: "Observer 1".to_string(),

};

let observer2=ConcreteObserver {

name: "Observer 2".to_string(),

};

subject.attach(&observer1);

subject.attach(&observer2);

subject.set_state("updated_data".to_string());

subject.detach(&observer2);

subject.set_state("Again updated data".to_string()); 

subject.detach(&observer1);

}


This content originally appeared on DEV Community and was authored by Somenath Mukhopadhyay


Print Share Comment Cite Upload Translate Updates
APA

Somenath Mukhopadhyay | Sciencx (2025-09-05T14:21:32+00:00) Observer pattern in Rust – driven by intrinsic motivation…. Retrieved from https://www.scien.cx/2025/09/05/observer-pattern-in-rust-driven-by-intrinsic-motivation/

MLA
" » Observer pattern in Rust – driven by intrinsic motivation…." Somenath Mukhopadhyay | Sciencx - Friday September 5, 2025, https://www.scien.cx/2025/09/05/observer-pattern-in-rust-driven-by-intrinsic-motivation/
HARVARD
Somenath Mukhopadhyay | Sciencx Friday September 5, 2025 » Observer pattern in Rust – driven by intrinsic motivation…., viewed ,<https://www.scien.cx/2025/09/05/observer-pattern-in-rust-driven-by-intrinsic-motivation/>
VANCOUVER
Somenath Mukhopadhyay | Sciencx - » Observer pattern in Rust – driven by intrinsic motivation…. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/05/observer-pattern-in-rust-driven-by-intrinsic-motivation/
CHICAGO
" » Observer pattern in Rust – driven by intrinsic motivation…." Somenath Mukhopadhyay | Sciencx - Accessed . https://www.scien.cx/2025/09/05/observer-pattern-in-rust-driven-by-intrinsic-motivation/
IEEE
" » Observer pattern in Rust – driven by intrinsic motivation…." Somenath Mukhopadhyay | Sciencx [Online]. Available: https://www.scien.cx/2025/09/05/observer-pattern-in-rust-driven-by-intrinsic-motivation/. [Accessed: ]
rf:citation
» Observer pattern in Rust – driven by intrinsic motivation… | Somenath Mukhopadhyay | Sciencx | https://www.scien.cx/2025/09/05/observer-pattern-in-rust-driven-by-intrinsic-motivation/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.