1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
//! Contains the ObserverDecimal, ObserverHexadecimal, and ObserverBinary
//! structs representing the various observers that can be used in this
//! Observer design pattern example.
//-----------------------------------------------------------------------------
use std::{rc::Rc, cell::RefCell};
use super::observer_inumberchanged_trait::IObserverNumberChanged;
//-----------------------------------------------------------------------------
/// Represents an observer that prints out the current number from the
/// Subject in decimal.
pub struct ObserverDecimal { }
impl ObserverDecimal {
/// Constructor
///
/// # Returns
/// Returns a new instance of the ObserverDecimal class as represented by
/// the IObserverNumberChanged trait.
pub fn new() -> Rc<RefCell<dyn IObserverNumberChanged>> {
Rc::new(RefCell::new(ObserverDecimal {}))
}
/// Helper method to display the number in decimal.
///
/// # Parameters
/// - number
///
/// The number to display.
fn show(&self, number: u32) {
println!(" Decimal : {0}", number);
}
}
impl IObserverNumberChanged for ObserverDecimal {
fn notify(&mut self, updated_number: u32) {
self.show(updated_number);
}
}
//#############################################################################
//#############################################################################
/// Represents an observer that prints out the current number from the
/// Subject in hexadecimal.
pub struct ObserverHexadecimal { }
impl ObserverHexadecimal {
/// Constructor
///
/// # Returns
/// Returns a new instance of the ObserverHexadecimal class as represented
/// by the IObserverNumberChanged trait.
pub fn new() -> Rc<RefCell<dyn IObserverNumberChanged>> {
Rc::new(RefCell::new(ObserverHexadecimal {}))
}
/// Helper method to display the number in hexadecimal.
///
/// # Parameters
/// - number
///
/// The number to display.
fn show(&self, number: u32) {
println!(" Hexadecimal: 0X{0:08X}", number);
}
}
impl IObserverNumberChanged for ObserverHexadecimal {
fn notify(&mut self, updated_number: u32) {
self.show(updated_number);
}
}
//#############################################################################
//#############################################################################
/// Represents an observer that prints out the current number from the
/// Subject in binary.
pub struct ObserverBinary { }
impl ObserverBinary {
/// Constructor
///
/// # Returns
/// Returns a new instance of the ObserverBinary class as represented by
/// the IObserverNumberChanged trait.
pub fn new() -> Rc<RefCell<dyn IObserverNumberChanged>> {
Rc::new(RefCell::new(ObserverBinary {}))
}
/// Helper method to display the number in binary.
///
/// # Parameters
/// - number
///
/// The number to display.
fn show(&self, number: u32) {
println!(" Binary : 0b{0:032b}", number);
}
}
impl IObserverNumberChanged for ObserverBinary {
fn notify(&mut self, updated_number: u32) {
self.show(updated_number);
}
}