Skip to content

Commit

Permalink
abstract method project
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Dec 29, 2024
1 parent 014f56c commit 032db8d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 99 deletions.
100 changes: 1 addition & 99 deletions src/patterns/creational/abstract-method.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Chair>;
fn create_sofa(&self) -> Box<dyn Sofa>;
}

// Concrete factories
struct ModernFurnitureFactory;

impl FurnitureFactory for ModernFurnitureFactory {
fn create_chair(&self) -> Box<dyn Chair> {
Box::new(ModernChair)
}

fn create_sofa(&self) -> Box<dyn Sofa> {
Box::new(ModernSofa)
}
}

struct VictorianFurnitureFactory;

impl FurnitureFactory for VictorianFurnitureFactory {
fn create_chair(&self) -> Box<dyn Chair> {
Box::new(VictorianChair)
}

fn create_sofa(&self) -> Box<dyn Sofa> {
Box::new(VictorianSofa)
}
}

// Client code
fn main() {
let factory: Box<dyn FurnitureFactory> = 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.
7 changes: 7 additions & 0 deletions src/patterns/creational/abstract-method/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/patterns/creational/abstract-method/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "abstract_method_pattern"
version = "0.1.0"
edition = "2018"

[dependencies]
99 changes: 99 additions & 0 deletions src/patterns/creational/abstract-method/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Chair>;
fn create_sofa(&self) -> Box<dyn Sofa>;
}

// Concrete factories
struct ModernFurnitureFactory;

impl FurnitureFactory for ModernFurnitureFactory {
fn create_chair(&self) -> Box<dyn Chair> {
Box::new(ModernChair)
}

fn create_sofa(&self) -> Box<dyn Sofa> {
Box::new(ModernSofa)
}
}

struct VictorianFurnitureFactory;

impl FurnitureFactory for VictorianFurnitureFactory {
fn create_chair(&self) -> Box<dyn Chair> {
Box::new(VictorianChair)
}

fn create_sofa(&self) -> Box<dyn Sofa> {
Box::new(VictorianSofa)
}
}

// Client code
fn main() {
let factory: Box<dyn FurnitureFactory> = Box::new(ModernFurnitureFactory);
let chair = factory.create_chair();
let sofa = factory.create_sofa();

chair.sit_on();
sofa.lie_on();
}

0 comments on commit 032db8d

Please sign in to comment.