Factory Method Pattern

The Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

In Rust, the Factory Method pattern can be implemented using traits and structs. The trait defines the method for creating objects, and the structs implement this trait to create specific types of objects.

Let's consider an example where we have a Shape trait and two structs Circle and Square that implement this trait. We will create a ShapeFactory trait with a method create_shape and two factories CircleFactory and SquareFactory that implement this trait.

// Define the Shape trait
trait Shape {
    fn draw(&self);
}

// Implement the Shape trait for Circle
struct Circle;

impl Shape for Circle {
    fn draw(&self) {
        println!("Drawing a Circle");
    }
}

// Implement the Shape trait for Square
struct Square;

impl Shape for Square {
    fn draw(&self) {
        println!("Drawing a Square");
    }
}

// Define the ShapeFactory trait
trait ShapeFactory {
    fn create_shape(&self) -> Box<dyn Shape>;
}

// Implement the ShapeFactory trait for CircleFactory
struct CircleFactory;

impl ShapeFactory for CircleFactory {
    fn create_shape(&self) -> Box<dyn Shape> {
        Box::new(Circle)
    }
}

// Implement the ShapeFactory trait for SquareFactory
struct SquareFactory;

impl ShapeFactory for SquareFactory {
    fn create_shape(&self) -> Box<dyn Shape> {
        Box::new(Square)
    }
}

fn main() {
    // Create a Circle using CircleFactory
    let circle_factory = CircleFactory;
    let circle = circle_factory.create_shape();
    circle.draw();

    // Create a Square using SquareFactory
    let square_factory = SquareFactory;
    let square = square_factory.create_shape();
    square.draw();
}

Output

Drawing a Circle
Drawing a Square

The ShapeFactory trait defines the create_shape method, and the CircleFactory and SquareFactory structs implement this method to create specific shapes. This allows for flexibility in creating different types of shapes without changing the client code.