diff --git a/src/patterns/.DS_Store b/src/patterns/.DS_Store index b0f965b..cd0763d 100644 Binary files a/src/patterns/.DS_Store and b/src/patterns/.DS_Store differ diff --git a/src/patterns/behavioral/observer.md b/src/patterns/behavioral/observer.md index a45910e..b4ebb2c 100644 --- a/src/patterns/behavioral/observer.md +++ b/src/patterns/behavioral/observer.md @@ -4,28 +4,12 @@ The Observer pattern is a behavioral design pattern where an object (the subject ### Step-by-Step Plan -1- **Define the Observer trait**: Create a trait that observers must implement. +1- **Define the Observer trait**: Create a trait that observers must implement.
+2- **Define the Subject struct**: Create a struct that maintains a list of observers and notifies them of changes.
+3- **Implement the Observer trait for concrete observers**: Create concrete observer structs that implement the Observer trait.
+4- **Demonstrate the Observer pattern in the main function**: Create a subject, add observers, and notify them of changes.
-```rust,noplaypen -{{#include observer/src/observer.rs}} -``` - -2- **Define the Subject struct**: Create a struct that maintains a list of observers and notifies them of changes. - -```rust,noplaypen -{{#include observer/src/subject.rs}} -``` - -3- **Implement the Observer trait for concrete observers**: Create concrete observer structs that implement the Observer trait. - -```rust,noplaypen -{{#include observer/src/concrete_observer.rs}} -``` - -4- **Demonstrate the Observer pattern in the main function**: Create a subject, add observers, and notify them of changes. - -```rust,noplaypen +```rust {{#include observer/src/main.rs}} ``` -You may find the sample project [here](https://github.com/saltukalakus/idiomatic-rust-snippets/tree/main/src/patterns/behavioral/observer). \ No newline at end of file diff --git a/src/patterns/behavioral/observer/src/concrete_observer.rs b/src/patterns/behavioral/observer/src/concrete_observer.rs deleted file mode 100644 index 493bc0c..0000000 --- a/src/patterns/behavioral/observer/src/concrete_observer.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::observer::Observer; - -pub struct ConcreteObserver { - name: String, -} - -impl ConcreteObserver { - pub fn new(name: &str) -> Self { - ConcreteObserver { - name: name.to_string(), - } - } -} - -impl Observer for ConcreteObserver { - fn update(&self, message: &str) { - println!("{} received message: {}", self.name, message); - } -} \ No newline at end of file diff --git a/src/patterns/behavioral/observer/src/main.rs b/src/patterns/behavioral/observer/src/main.rs index 9f6987b..126ceca 100644 --- a/src/patterns/behavioral/observer/src/main.rs +++ b/src/patterns/behavioral/observer/src/main.rs @@ -1,9 +1,49 @@ -mod observer; -mod subject; -mod concrete_observer; +// Observer type +pub trait Observer { + fn update(&self, message: &str); +} -use subject::Subject; -use concrete_observer::ConcreteObserver; +// Concrete observer +pub struct ConcreteObserver { + name: String, +} + +impl ConcreteObserver { + pub fn new(name: &str) -> Self { + ConcreteObserver { + name: name.to_string(), + } + } +} + +impl Observer for ConcreteObserver { + fn update(&self, message: &str) { + println!("{} received message: {}", self.name, message); + } +} + +// Subject +pub struct Subject { + observers: Vec>, +} + +impl Subject { + pub fn new() -> Self { + Subject { + observers: Vec::new(), + } + } + + pub fn add_observer(&mut self, observer: Box) { + self.observers.push(observer); + } + + pub fn notify_observers(&self, message: &str) { + for observer in &self.observers { + observer.update(message); + } + } +} fn main() { let mut subject = Subject::new(); diff --git a/src/patterns/behavioral/observer/src/observer.rs b/src/patterns/behavioral/observer/src/observer.rs deleted file mode 100644 index 02c1122..0000000 --- a/src/patterns/behavioral/observer/src/observer.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub trait Observer { - fn update(&self, message: &str); -} \ No newline at end of file diff --git a/src/patterns/behavioral/observer/src/subject.rs b/src/patterns/behavioral/observer/src/subject.rs deleted file mode 100644 index 09c3217..0000000 --- a/src/patterns/behavioral/observer/src/subject.rs +++ /dev/null @@ -1,23 +0,0 @@ -use crate::observer::Observer; - -pub struct Subject { - observers: Vec>, -} - -impl Subject { - pub fn new() -> Self { - Subject { - observers: Vec::new(), - } - } - - pub fn add_observer(&mut self, observer: Box) { - self.observers.push(observer); - } - - pub fn notify_observers(&self, message: &str) { - for observer in &self.observers { - observer.update(message); - } - } -} \ No newline at end of file