Does this code follow the Observer pattern?

I tried to apply the observer pattern to Rust. As in other GC languages ​​such as JS or Java, I wanted to send data links to the Observable in an event on the Observer . But the compiler continued to give me a headache due to check verification. Therefore, because of this, I learned about the use of Rc , but this did not allow me to change the value in Observable , then I used RefCell for internal variability, which worked the way I wanted. Hoorah, I said. But then I realized that Rc leads to the fact that one place can be sent from different places, which made the Observer event system obsolete. Therefore, after removing the event method from Observer I got:

 struct Observable<T: Clone> { value: Rc<RefCell<T>> } impl<T: Clone> Observable<T> { fn new(value: T) -> Observable<T> { Observable { value: Rc::new(RefCell::new(value)) } } fn set_value(&mut self, value: T) { *self.value.borrow_mut() = value; } fn register(&mut self) -> Observer<T> { Observer::new(self.value.clone()) } } struct Observer<T: Clone> { value: Rc<RefCell<T>> } impl<T: Clone> Observer<T> { fn new(value: Rc<RefCell<T>>) -> Observer<T> { Observer { value } } fn value(&self) -> T { (*self.value.borrow()).clone() } } 

Link to Rust Playground

So the above code is an observer pattern from a technical point of view? Because otherwise it works for me. But just wanted to know what an observer pattern is?

+5
source share
2 answers

So the above code is an observer pattern from a technical point of view?

NO

But just wanted to know what an observer pattern is?

I updated your code to simulate an observer pattern.

 #[allow(unused_variables)] pub trait Events { fn on_value(&self, value: &str) {} } struct Observable { value: String, observers: Vec<Box<Events>>, } impl Observable { fn new(value: &str) -> Observable { Observable { value: value.to_owned(), observers: Vec::new(), } } fn set_value(&mut self, value: &str) { self.value = value.to_owned(); // send event to observers for observer in &self.observers { observer.on_value(value); } } fn register<E: Events + 'static>(&mut self, observer: E) { self.observers.push(Box::new(observer)); } } struct Observer; impl Events for Observer { fn on_value(&self, value: &str) { println!("received value: {:?}", value); } } fn main() { let mut observable = Observable::new("initial value"); observable.register(Observer); observable.set_value("updated value"); } 

The object is Observable , and it maintains a list of observers . When a new value is set, Observable notifies observers.

Playground Link

Blog post about the observer pattern in rust

+3
source

No, he does not represent the observer pattern.

From wikipedia

An observer pattern is a software development pattern in which an object called a subject maintains a list of its dependents called observers and automatically notifies them of any state changes, usually by calling one of its methods.

What is missing in your implementation is that observers are not notified of observed changes.

+1
source

Source: https://habr.com/ru/post/1275839/


All Articles