Karmyog - Work is worship...
In Rust, i keep my Trust...
The strategy design pattern is a behavioral design pattern that lets you dynamically switch the behavior of an object at runtime. It achieves this by separating the core functionality of the object from the specific algorithms it uses.
Here's a breakdown of the core concepts:
Strategy Interface: This interface defines the common operation that all the different algorithms will implement. This ensures that all the interchangeable strategies can be used by the context object.
Concrete Strategies: These are the classes that implement the specific algorithms. Each concrete strategy class implements the strategy interface and provides its own unique behavior for the operation.
Context Object: This object holds a reference to a strategy object and delegates the specific operation to it. It can change its behavior at runtime by switching the reference to a different concrete strategy.
In my code, the
trait TransportationToAirport{
fn going_to_the_airport(&self);
}
plays the role of the Strategy interface.
Three concrete Strategy classes have been derived from this interface - namely, By_Ola, By_Bus and By_Rapido.
These concrete strategy classes help to pick up a specific way for going to the airport dynamically, i.e., in runtime.
Here's the source code for Strategy Pattern implemented in Rust.
use std::io;
trait TransportationToAirport {
fn going_to_the_airport(&self);
}
struct ByBus {}
impl TransportationToAirport for ByBus {
fn going_to_the_airport(&self) {
println!("Going to airport by Bus...");
}
}
struct ByOla {}
impl TransportationToAirport for ByOla {
fn going_to_the_airport(&self) {
println!("Going to airport by Ola...");
}
}
struct ByRapido {}
impl TransportationToAirport for ByRapido {
fn going_to_the_airport(&self) {
println!("Going to airport by Rapido...");
}
}
struct Traveller {
strategy: Box<dyn TransportationToAirport>,
}
impl Traveller {
fn new(strategy: Box<dyn TransportationToAirport>) -> Self {
Traveller { strategy }
}
fn travel(&self) {
self.strategy.going_to_the_airport();
}
pub fn set_strategy(&mut self, strategy: Box<dyn TransportationToAirport>) {
self.strategy = strategy;
}
}
fn main() {
println!("Enter your choice...");
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed to read line");
if choice.trim().eq_ignore_ascii_case("BUS") {
let traveller = Traveller::new(Box::new(ByBus {}));
traveller.travel();
} else if choice.trim().eq_ignore_ascii_case("OLA") {
let traveller = Traveller::new(Box::new(ByOla {}));
traveller.travel();
} else if choice.trim().eq_ignore_ascii_case("RAPIDO") {
let traveller = Traveller::new(Box::new(ByRapido {}));
traveller.travel();
} else {
println!("Invalid choice. Please choose BUS, OLA, or RAPIDO.");
}
}
Here's the output of the above code:
Enter your choice...
OLA
Going to airport by Ola...
Top comments (0)