Skip to content

Commit

Permalink
update command pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Dec 1, 2024
1 parent 1ef5753 commit b7c89cf
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/patterns/behavioral/command/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::cell::RefCell;
use std::rc::Rc;

// Command
pub trait Command {
fn execute(&self);
}

// ConcreteCommand for turning on the light
pub struct TurnOnCommand {
pub light: Rc<RefCell<Light>>,
}
Expand All @@ -26,6 +28,7 @@ impl Command for TurnOffCommand {
}
}

// Invoker
pub struct RemoteControl{
command: Option<Box<dyn Command>>,
}
Expand All @@ -46,6 +49,7 @@ impl RemoteControl {
}
}

// Receiver
pub struct Light {
is_on: bool,
}
Expand All @@ -70,6 +74,7 @@ impl Light {
}
}

// Client
fn main() {
let light = Rc::new(RefCell::new(Light::new()));
let turn_on_command = TurnOnCommand { light: Rc::clone(&light) };
Expand Down

0 comments on commit b7c89cf

Please sign in to comment.