봄맞이 세일
프로토타입

러스트로 작성된 프로토타입

프로토타입은 객체들​(복잡한 객체 포함)​을 그의 특정 클래스들에 결합하지 않고 복제할 수 있도록 하는 생성 디자인 패턴입니다.

모든 프로토타입 클래스들은 객체들의 구상 클래스들을 알 수 없는 경우에도 해당 객체들을 복사할 수 있도록 하는 공통 인터페이스가 있어야 합니다. 프로토타입 객체들은 전체 복사본들을 생성할 수 있습니다. 왜냐하면 같은 클래스의 객체들은 서로의 비공개 필드들에 접근할 수 있기 때문입니다.

Built-in Clone trait

Rust has a built-in std::clone::Clone trait with many implementations for various types (via #[derive(Clone)]). Thus, the Prototype pattern is ready to use out of the box.

main.rs

#[derive(Clone)]
struct Circle {
    pub x: u32,
    pub y: u32,
    pub radius: u32,
}

fn main() {
    let circle1 = Circle {
        x: 10,
        y: 15,
        radius: 10,
    };

    // Prototype in action.
    let mut circle2 = circle1.clone();
    circle2.radius = 77;

    println!("Circle 1: {}, {}, {}", circle1.x, circle1.y, circle1.radius);
    println!("Circle 2: {}, {}, {}", circle2.x, circle2.y, circle2.radius);
}

Output

Circle 1: 10, 15, 10
Circle 2: 10, 15, 77

다른 언어로 작성된 프로토타입

C#으로 작성된 프로토타입 C++로 작성된 프로토타입 Go로 작성된 프로토타입 자바로 작성된 프로토타입 PHP로 작성된 프로토타입 파이썬으로 작성된 프로토타입 루비로 작성된 프로토타입 스위프트로 작성된 프로토타입 타입스크립트로 작성된 프로토타입