This page looks best with JavaScript enabled

Factory pattern

 ·  ☕ 3 min read  ·  ✍️ t1

The factory pattern is a design pattern that is used to create objects without specifying the exact class of object that will be created. This allows for flexibility in the creation of objects, as the factory can be configured to create different types of objects based on the needs of the application.

In Rust, the factory pattern can be implemented using traits and generics. This allows for the creation of a factory trait that can be implemented by different types of factories, each of which can create different types of objects.

Here is an example of a factory trait and a simple factory implementation that creates objects of type Person:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
trait Factory<T> {
    fn create(&self) -> T;
}

struct PersonFactory {
    name: String,
    age: u32,
}

impl Factory<Person> for PersonFactory {
    fn create(&self) -> Person {
        Person {
            name: self.name.clone(),
            age: self.age,
        }
    }
}

struct Person {
    name: String,
    age: u32,
}

In this example, the Factory trait defines a method create that takes a self reference and returns an object of type T. This trait is implemented by the PersonFactory struct, which takes a name and age as parameters and uses them to create a Person object.

To use the factory, we can create an instance of PersonFactory and call the create method to create a new Person object:

1
2
3
4
5
6
let factory = PersonFactory {
    name: "John Doe".to_string(),
    age: 30,
};

let person = factory.create();

In this example, the factory variable is an instance of PersonFactory with a name of “John Doe” and an age of 30. When the create method is called on this instance, it creates a new Person object with the given name and age.

This example can be extended to create different types of objects based on the needs of the application. For example, we can create a VehicleFactory that creates Car and Truck objects:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
enum VehicleType {
    Car,
    Truck,
}

trait Factory<T> {
    fn create(&self, vehicle_type: VehicleType) -> T;
}

struct VehicleFactory {
    make: String,
    model: String,
    year: u16,
}

impl Factory<Vehicle> for VehicleFactory {
    fn create(&self, vehicle_type: VehicleType) -> Vehicle {
        match vehicle_type {
            VehicleType::Car => Car {
                make: self.make.clone(),
                model: self.model.clone(),
                year: self.year,
            },
            VehicleType::Truck => Truck {
                make: self.make.clone(),
                model: self.model.clone(),
                year: self.year,
            },
        }
    }
}

struct Car {
    make: String,
    model: String,
    year: u16,
}

struct Truck {
    make: String,
    model: String,
    year: u16,
}

In this example, the VehicleFactory

Share on

t1
WRITTEN BY
t1
Dev