Skip to content

Commit

Permalink
refactor observer pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Dec 11, 2024
1 parent 749a519 commit 2c8bc4a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 71 deletions.
Binary file modified src/patterns/.DS_Store
Binary file not shown.
26 changes: 5 additions & 21 deletions src/patterns/behavioral/observer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/>
2- **Define the Subject struct**: Create a struct that maintains a list of observers and notifies them of changes.<br/>
3- **Implement the Observer trait for concrete observers**: Create concrete observer structs that implement the Observer trait.<br/>
4- **Demonstrate the Observer pattern in the main function**: Create a subject, add observers, and notify them of changes.<br/>

```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).
19 changes: 0 additions & 19 deletions src/patterns/behavioral/observer/src/concrete_observer.rs

This file was deleted.

50 changes: 45 additions & 5 deletions src/patterns/behavioral/observer/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn Observer>>,
}

impl Subject {
pub fn new() -> Self {
Subject {
observers: Vec::new(),
}
}

pub fn add_observer(&mut self, observer: Box<dyn Observer>) {
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();
Expand Down
3 changes: 0 additions & 3 deletions src/patterns/behavioral/observer/src/observer.rs

This file was deleted.

23 changes: 0 additions & 23 deletions src/patterns/behavioral/observer/src/subject.rs

This file was deleted.

0 comments on commit 2c8bc4a

Please sign in to comment.