Patterns Créationnels
Singleton
lazy_static! {
static ref INSTANCE: Mutex<Singleton> = Mutex::new(Singleton);
}
struct Singleton;
impl Singleton {
pub fn get_instance() -> &'static Mutex<Singleton> {
INSTANCE
}
}
Utilisez-le lorsque vous avez un seul point d'accès global à une ressource.
Factory Method
trait Shape {
fn draw(&self);
}
struct Circle;
impl Shape for Circle {
fn draw(&self) {
println!("Drawing a circle");
}
}
struct Square;
impl Shape for Square {
fn draw(&self) {
println!("Drawing a square");
}
}
trait ShapeFactory {
fn create_shape(&self) -> Box<dyn Shape>;
}
struct CircleFactory;
impl ShapeFactory for CircleFactory {
fn create_shape(&self) -> Box<dyn Shape> {
Box::new(Circle)
}
}
struct SquareFactory;
impl ShapeFactory for SquareFactory {
fn create_shape(&self) -> Box<dyn Shape> {
Box::new(Square)
}
}
Utilisez-le pour créer des objets sans spécifier la classe exacte.
Patterns Structurels
Adapter
trait Target {
fn request(&self);
}
struct Adaptee;
impl Adaptee {
fn specific_request(&self) {
println!("Specific Request");
}
}
struct Adapter {
adaptee: Adaptee,
}
impl Target for Adapter {
fn request(&self) {
self.adaptee.specific_request();
}
}
Utilisez-le pour rendre des interfaces incompatibles opérationnelles entre elles.
Decorator
trait Coffee {
fn cost(&self) -> f64;
}
struct SimpleCoffee;
impl Coffee for SimpleCoffee {
fn cost(&self) -> f64 {
1.0
}
}
struct MilkDecorator {
coffee: Box<dyn Coffee>,
}
impl Coffee for MilkDecorator {
fn cost(&self) -> f64 {
self.coffee.cost() + 0.5
}
}
Utilisez-le pour ajouter des responsabilités à un objet individuellement sans modifier sa structure.
Patterns Comportementaux
Observer
trait Observer {
fn update(&self, message: &str);
}
struct ConcreteObserver;
impl Observer for ConcreteObserver {
fn update(&self, message: &str) {
println!("Received message: {}", message);
}
}
struct Subject {
observers: Vec<Box<dyn Observer>>,
state: String,
}
impl Subject {
fn new() -> Self {
Subject {
observers: vec![],
state: "".to_string(),
}
}
fn attach(&mut self, observer: Box<dyn Observer>) {
self.observers.push(observer);
}
fn notify(&self) {
for observer in &self.observers {
observer.update(&self.state);
}
}
fn set_state(&mut self, state: String) {
self.state = state;
self.notify();
}
}
Utilisez-le pour maintenir un ensemble de vues à jour automatiquement lorsqu'un modèle change.
Strategy
trait PaymentStrategy {
fn pay(&self, amount: f64);
}
struct CreditCardPayment;
impl PaymentStrategy for CreditCardPayment {
fn pay(&self, amount: f64) {
println!("Paid with credit card: {}", amount);
}
}
struct PayPalPayment;
impl PaymentStrategy for PayPalPayment {
fn pay(&self, amount: f64) {
println!("Paid with PayPal: {}", amount);
}
}
struct ShoppingCart {
strategy: Box<dyn PaymentStrategy>,
}
impl ShoppingCart {
fn new(strategy: Box<dyn PaymentStrategy>) -> Self {
ShoppingCart { strategy }
}
fn checkout(&self, amount: f64) {
self.strategy.pay(amount);
}
}
Utilisez-le pour permettre aux clients de choisir la façon dont ils veulent effectuer un paiement.