Bridge Design Pattern
The Bridge pattern is used to separate the abstraction from its implementation, allowing them to vary independently. It is useful when both the class and what it does vary often.
// Abstraction trait Shape { fn draw(&self); } // Implementor trait Color { fn fill(&self); } // Concrete Implementor 1 struct Red; impl Color for Red { fn fill(&self) { println!("Filling with red color"); } } // Concrete Implementor 2 struct Blue; impl Color for Blue { fn fill(&self) { println!("Filling with blue color"); } } // Refined Abstraction struct Circle { color: Box<dyn Color>, } impl Shape for Circle { fn draw(&self) { print!("Drawing Circle - "); self.color.fill(); } } // Refined Abstraction struct Square { color: Box<dyn Color>, } impl Shape for Square { fn draw(&self) { print!("Drawing Square - "); self.color.fill(); } } fn main() { let red = Box::new(Red); let blue = Box::new(Blue); let red_circle = Circle { color: red }; let blue_square = Square { color: blue }; red_circle.draw(); blue_square.draw(); }
- Shape Trait: Defines the abstraction with a
drawmethod. - Color Trait: Defines the implementor with a
fillmethod. - Red and Blue Structs: Concrete implementors of the
Colortrait. - Circle and Square Structs: Refined abstractions that implement the
Shapetrait and use aColorto fill the shape.