Struct design_pattern_examples_rust::observer::observer_numberproducer::ObserverNumberProducer
source · pub struct ObserverNumberProducer {
number: u32,
observers: Vec<Rc<RefCell<dyn IObserverNumberChanged>>>,
}
Expand description
Represents the Observer Subject in this example, in this case, a struct that contains a single number that is updated with a call to the update() method. Whenever update() is called, the number is incremented and all observers are notified. The observers are passed (“pushed”) the changed number through the IObserverNumberChanged trait.
Fields§
§number: u32
The number being maintained.
observers: Vec<Rc<RefCell<dyn IObserverNumberChanged>>>
The list of observers subscribed to this class instance.
Implementations§
source§impl ObserverNumberProducer
impl ObserverNumberProducer
sourcepub fn new() -> ObserverNumberProducer
pub fn new() -> ObserverNumberProducer
Default constructor
sourcepub fn add_observer(
&mut self,
observer: &Rc<RefCell<dyn IObserverNumberChanged>>
)
pub fn add_observer( &mut self, observer: &Rc<RefCell<dyn IObserverNumberChanged>> )
Call this method to subscribe an observer to this struct for notifications about changing numbers. Does nothing if the given observer is already subscribed.
Parameters
-
observer
The observer as represented by the IObserverNumberChanged trait.
sourcepub fn remove_observer(
&mut self,
observer: &Rc<RefCell<dyn IObserverNumberChanged>>
)
pub fn remove_observer( &mut self, observer: &Rc<RefCell<dyn IObserverNumberChanged>> )
Call this method to unsubscribe an observer from this struct so notifications are no longer received. Does nothing if the given observer was not subscribed.
Parameters
-
observer
The observer as represented by the IObserverNumberChanged trait.
sourcefn find_index_of_observer(
&self,
observer: &Rc<RefCell<dyn IObserverNumberChanged>>
) -> Option<usize>
fn find_index_of_observer( &self, observer: &Rc<RefCell<dyn IObserverNumberChanged>> ) -> Option<usize>
Helper method to retrieve the index to the specified observer if the observer is in the list.
Parameters
-
observer
The observer as represented by the IObserverNumberChanged trait.
Returns
Returns Some(index) if the observer was found; otherwise, returns None.
sourcefn notify_observers(&self)
fn notify_observers(&self)
Helper method to notify all observers that the number has changed.