Friend spotlight!
Whimsical Animations course
Friend spotlight!
NEW Whimsical Animations course
Friend spotlight! NEW Whimsical Animations course
huge discount only this week
Friend spotlight! Want to make your project stand out? NEW Whimsical Animations course huge discount only this week
데코레이터

러스트로 작성된 데코레이터

데코레이터는 구조 패턴이며 새로운 행동들을 특수 래퍼 객체들 내에 넣어서 이러한 행동들을 객체들에 동적으로 추가할 수 있도록 합니다.

데코레이터를 사용하여 객체들을 제한 없이 래핑할 수 있습니다. 왜냐하면 대상 객체들과 데코레이터들은 같은 인터페이스를 따르기 때문입니다. 결과 객체는 모든 래퍼의 스태킹된 행동을 가질 것입니다.

Input streams decoration

There is a practical example in Rust's standard library for input/output operations.

A buffered reader decorates a vector reader adding buffered behavior.

let mut input = BufReader::new(Cursor::new("Input data"));
input.read(&mut buf).ok();

main.rs

use std::io::{BufReader, Cursor, Read};

fn main() {
    let mut buf = [0u8; 10];

    // A buffered reader decorates a vector reader which wraps input data.
    let mut input = BufReader::new(Cursor::new("Input data"));

    input.read(&mut buf).ok();

    print!("Read from a buffered reader: ");

    for byte in buf {
        print!("{}", char::from(byte));
    }

    println!();
}

Output

Read from a buffered reader: Input data

다른 언어로 작성된 데코레이터

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