From 032db8ddb51cd130624fca1ed1fb29d023ce41fe Mon Sep 17 00:00:00 2001 From: saltukalakus Date: Sun, 29 Dec 2024 20:33:54 +0000 Subject: [PATCH] abstract method project --- src/patterns/creational/abstract-method.md | 100 +----------------- .../creational/abstract-method/Cargo.lock | 7 ++ .../creational/abstract-method/Cargo.toml | 6 ++ .../creational/abstract-method/src/main.rs | 99 +++++++++++++++++ 4 files changed, 113 insertions(+), 99 deletions(-) create mode 100644 src/patterns/creational/abstract-method/Cargo.lock create mode 100644 src/patterns/creational/abstract-method/Cargo.toml create mode 100644 src/patterns/creational/abstract-method/src/main.rs diff --git a/src/patterns/creational/abstract-method.md b/src/patterns/creational/abstract-method.md index 77cea25..0fb5755 100644 --- a/src/patterns/creational/abstract-method.md +++ b/src/patterns/creational/abstract-method.md @@ -5,105 +5,7 @@ The Abstract Factory design pattern provides an interface for creating families Here is an example of the Abstract Factory Pattern: ```rust -// Define traits for products -trait Chair { - fn has_legs(&self) -> bool; - fn sit_on(&self); -} - -trait Sofa { - fn has_legs(&self) -> bool; - fn lie_on(&self); -} - -// Concrete products for Modern style -struct ModernChair; -struct ModernSofa; - -impl Chair for ModernChair { - fn has_legs(&self) -> bool { - true - } - - fn sit_on(&self) { - println!("Sitting on a modern chair."); - } -} - -impl Sofa for ModernSofa { - fn has_legs(&self) -> bool { - true - } - - fn lie_on(&self) { - println!("Lying on a modern sofa."); - } -} - -// Concrete products for Victorian style -struct VictorianChair; -struct VictorianSofa; - -impl Chair for VictorianChair { - fn has_legs(&self) -> bool { - true - } - - fn sit_on(&self) { - println!("Sitting on a Victorian chair."); - } -} - -impl Sofa for VictorianSofa { - fn has_legs(&self) -> bool { - true - } - - fn lie_on(&self) { - println!("Lying on a Victorian sofa."); - } -} - -// Abstract factory trait -trait FurnitureFactory { - fn create_chair(&self) -> Box; - fn create_sofa(&self) -> Box; -} - -// Concrete factories -struct ModernFurnitureFactory; - -impl FurnitureFactory for ModernFurnitureFactory { - fn create_chair(&self) -> Box { - Box::new(ModernChair) - } - - fn create_sofa(&self) -> Box { - Box::new(ModernSofa) - } -} - -struct VictorianFurnitureFactory; - -impl FurnitureFactory for VictorianFurnitureFactory { - fn create_chair(&self) -> Box { - Box::new(VictorianChair) - } - - fn create_sofa(&self) -> Box { - Box::new(VictorianSofa) - } -} - -// Client code -fn main() { - let factory: Box = Box::new(ModernFurnitureFactory); - let chair = factory.create_chair(); - let sofa = factory.create_sofa(); - - chair.sit_on(); - sofa.lie_on(); -} +{{#include abstract-method/src/main.rs}} ``` In this example, the `FurnitureFactory` trait defines methods for creating abstract products (`Chair` and `Sofa`). The `ModernFurnitureFactory` and `VictorianFurnitureFactory` structs implement this trait to create concrete products. The client code uses the factory to create and interact with the products without knowing their concrete types. \ No newline at end of file diff --git a/src/patterns/creational/abstract-method/Cargo.lock b/src/patterns/creational/abstract-method/Cargo.lock new file mode 100644 index 0000000..67abe31 --- /dev/null +++ b/src/patterns/creational/abstract-method/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "abstract_method_pattern" +version = "0.1.0" diff --git a/src/patterns/creational/abstract-method/Cargo.toml b/src/patterns/creational/abstract-method/Cargo.toml new file mode 100644 index 0000000..38a411a --- /dev/null +++ b/src/patterns/creational/abstract-method/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "abstract_method_pattern" +version = "0.1.0" +edition = "2018" + +[dependencies] \ No newline at end of file diff --git a/src/patterns/creational/abstract-method/src/main.rs b/src/patterns/creational/abstract-method/src/main.rs new file mode 100644 index 0000000..d2371da --- /dev/null +++ b/src/patterns/creational/abstract-method/src/main.rs @@ -0,0 +1,99 @@ +// Define traits for products +trait Chair { + fn has_legs(&self) -> bool; + fn sit_on(&self); +} + +trait Sofa { + fn has_legs(&self) -> bool; + fn lie_on(&self); +} + +// Concrete products for Modern style +struct ModernChair; +struct ModernSofa; + +impl Chair for ModernChair { + fn has_legs(&self) -> bool { + true + } + + fn sit_on(&self) { + println!("Sitting on a modern chair."); + } +} + +impl Sofa for ModernSofa { + fn has_legs(&self) -> bool { + true + } + + fn lie_on(&self) { + println!("Lying on a modern sofa."); + } +} + +// Concrete products for Victorian style +struct VictorianChair; +struct VictorianSofa; + +impl Chair for VictorianChair { + fn has_legs(&self) -> bool { + true + } + + fn sit_on(&self) { + println!("Sitting on a Victorian chair."); + } +} + +impl Sofa for VictorianSofa { + fn has_legs(&self) -> bool { + true + } + + fn lie_on(&self) { + println!("Lying on a Victorian sofa."); + } +} + +// Abstract factory trait +trait FurnitureFactory { + fn create_chair(&self) -> Box; + fn create_sofa(&self) -> Box; +} + +// Concrete factories +struct ModernFurnitureFactory; + +impl FurnitureFactory for ModernFurnitureFactory { + fn create_chair(&self) -> Box { + Box::new(ModernChair) + } + + fn create_sofa(&self) -> Box { + Box::new(ModernSofa) + } +} + +struct VictorianFurnitureFactory; + +impl FurnitureFactory for VictorianFurnitureFactory { + fn create_chair(&self) -> Box { + Box::new(VictorianChair) + } + + fn create_sofa(&self) -> Box { + Box::new(VictorianSofa) + } +} + +// Client code +fn main() { + let factory: Box = Box::new(ModernFurnitureFactory); + let chair = factory.create_chair(); + let sofa = factory.create_sofa(); + + chair.sit_on(); + sofa.lie_on(); +} \ No newline at end of file