Skip to content

Commit c0ce7df

Browse files
committed
Updated flyweight
1 parent d6c5d7c commit c0ce7df

7 files changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use design_patterns::structural::flyweight::parking::{
2+
car::car_type::{Body, Colour},
3+
Parking,
4+
};
5+
6+
fn main() {
7+
use Body::{Coupe, Sedan, Truck};
8+
use Colour::{Black, Red, Yellow};
9+
10+
let mut parking = Parking::new();
11+
parking.add_car("NFS MW", 13, Coupe, Red);
12+
parking.add_car("MW2017", 15, Truck, Yellow);
13+
parking.add_car("BIG11", 37, Sedan, Black);
14+
parking.add_car("KING", 64, Coupe, Red);
15+
parking.add_car("MAN", 73, Coupe, Red);
16+
parking.add_car("TE64G", 18, Truck, Yellow);
17+
parking.add_car("SMILE", 39, Sedan, Black);
18+
parking.add_car("DARK01", 24, Truck, Black);
19+
parking.add_car("DARK03", 25, Truck, Black);
20+
parking.add_car("DARK05", 26, Truck, Black);
21+
22+
parking.print();
23+
}

src/structural/flyweight/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! # Flyweight
2+
//!
3+
//! ## Type
4+
//!
5+
//! Structural
6+
//!
7+
//! ## Description
8+
//!
9+
//! Use sharing to support large numbers of fine grained objects efficiently.
10+
11+
pub mod parking;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use super::car_type::{Body, CarType, Colour};
2+
3+
pub struct CarFactory {
4+
car_types: Vec<CarType>,
5+
}
6+
7+
impl CarFactory {
8+
pub fn new() -> CarFactory {
9+
CarFactory {
10+
car_types: Vec::new(),
11+
}
12+
}
13+
14+
pub fn get_car_type_id(&mut self, body: Body, colour: Colour) -> u8 {
15+
let position = self
16+
.car_types
17+
.iter()
18+
.position(|ref r| r.body == body && r.colour == colour);
19+
20+
match position {
21+
Some(x) => x as u8,
22+
None => {
23+
let car_type = CarType::new(body, colour);
24+
self.car_types.push(car_type);
25+
(self.car_types.len() - 1) as u8
26+
}
27+
}
28+
}
29+
30+
pub fn get_car_type(&mut self, id: u8) -> Option<&CarType> {
31+
self.car_types.get(id as usize)
32+
}
33+
34+
pub fn print(&self) {
35+
println!("Number of car types: {}", self.car_types.len());
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pub struct CarType {
2+
pub body: Body,
3+
pub colour: Colour,
4+
}
5+
6+
impl CarType {
7+
pub fn new(body: Body, colour: Colour) -> CarType {
8+
CarType { body, colour }
9+
}
10+
}
11+
12+
#[derive(Debug, PartialEq)]
13+
pub enum Body {
14+
Sedan,
15+
Coupe,
16+
Truck,
17+
}
18+
19+
#[derive(Debug, PartialEq)]
20+
pub enum Colour {
21+
Black,
22+
Yellow,
23+
Red,
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pub mod car_factory;
2+
pub mod car_type;
3+
4+
use car_type::CarType;
5+
6+
pub struct Car {
7+
pub license_plate: String,
8+
pub parking_place_number: u8,
9+
pub car_type_id: u8,
10+
}
11+
12+
impl Car {
13+
pub fn new(license_plate: String, parking_place_number: u8, car_type_id: u8) -> Car {
14+
Car {
15+
license_plate,
16+
parking_place_number,
17+
car_type_id,
18+
}
19+
}
20+
21+
pub fn print(&self, car_type: &CarType) {
22+
println!(
23+
"{} - {:?} {:?}, {}",
24+
self.parking_place_number, car_type.colour, car_type.body, self.license_plate
25+
)
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub mod car;
2+
3+
use car::car_factory::CarFactory;
4+
use car::car_type::{Body, Colour};
5+
use car::Car;
6+
7+
pub struct Parking {
8+
cars: Vec<Car>,
9+
car_factory: CarFactory,
10+
}
11+
12+
impl Parking {
13+
pub fn new() -> Parking {
14+
Parking {
15+
cars: Vec::new(),
16+
car_factory: CarFactory::new(),
17+
}
18+
}
19+
20+
pub fn add_car(
21+
&mut self,
22+
license_plate: &str,
23+
parking_place_number: u8,
24+
body: Body,
25+
colour: Colour,
26+
) {
27+
self.cars.push(Car::new(
28+
license_plate.to_string(),
29+
parking_place_number,
30+
self.car_factory.get_car_type_id(body, colour),
31+
));
32+
}
33+
34+
pub fn print(&mut self) {
35+
for car in &self.cars {
36+
let car_type = self.car_factory.get_car_type(car.car_type_id).unwrap();
37+
car.print(car_type);
38+
}
39+
40+
println!("\nNumber of cars: {}", self.cars.len());
41+
self.car_factory.print();
42+
}
43+
}

src/structural/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ pub mod bridge;
1515
pub mod composite;
1616
pub mod decorator;
1717
pub mod facade;
18+
pub mod flyweight;

0 commit comments

Comments
 (0)