Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
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
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

Decorator Pattern - 1 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

Decorator Pattern - 1

Seung__ 2021. 12. 28. 20:06

1. 데코레이터 패턴을 이해하기 위한 예제 코드


 

2. Component


public interface Component {

    String operation();
}

 

 

3. RealComponent


@Slf4j
public class RealComponent implements Component{


    @Override
    public String operation() {
        log.info("Real Component 실행");
        return "data";
    }
}

 

4. Client


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DecoratorPatternClient {

    private Component component;

    public DecoratorPatternClient(Component component) {
        this.component = component;
    }

    public void execute(){
        String result = component.operation();
        log.info("result={}", result);
    }

}

 

 

5. Test


import hello.proxy.pureproxy.decorator.code.Component;
import hello.proxy.pureproxy.decorator.code.DecoratorPatternClient;
import hello.proxy.pureproxy.decorator.code.RealComponent;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

@Slf4j
public class DecoratorPatternTest {

    @Test
    void noDecorator(){
        Component realComponent = new RealComponent();

        DecoratorPatternClient client = new DecoratorPatternClient(realComponent);

        client.execute();
    }

}

 

 

6. GitHub : 211228 DecoratorePattern1


 

GitHub - bsh6463/Spring_Advanced_2

Contribute to bsh6463/Spring_Advanced_2 development by creating an account on GitHub.

github.com

 

Comments