본문 바로가기
DesignPattern

[Design Pattern] Decorator Pattern

by 봄석 2019. 10. 8.

[Design Pattern] Decorator Pattern

 

Decorator Pattern

Decorator란 뜻 그대로 장식 자라는 뜻을 가지고 있습니다.

즉 Decorator pattern은, 원본에 무언가를 더  입혀 새로운 것을 만드는 것입니다.

바로 예시를 보도록 하겠습니다.


케이크(Decorate Pattern with Java)

public class Cake {
    public String getCake() {
        return "cake";
    }
}
public class CreamDecorator extends Cake{
    private Cake cake;
     
    public CreamDecorator(Cake cake) {
        this.cake = cake;
    }
     
    @Override
    public String getCake() {
        return "*cream* "+cake.getCake() + " *cream*";
         
    }
}
public class StrawberryDecorator extends Cake{
    private Cake cake;
     
    public StrawberryDecorator(Cake cake) {
        this.cake = cake;
    }
     
    @Override
    public String getCake() {
        return "@strawberry@ "+cake.getCake() + " @strawberry@";
    }
}
public class Client {
    public static void main(String[] args) {
        Cake cake = new Cake();
        Cake creamCake = new CreamDecorator(cake);
        Cake strawberryCreamCake = new StrawberryDecorator(creamCake);
        System.out.println(strawberryCreamCake.getCake());
    }
}
//result 
@strawberry@ *cream* cake *cream* @strawberry@

커피( Decorate Pattern with Kotlin )

interface CoffeeMachine {
    fun makeSmallCoffee()
    fun makeLargeCoffee()
}
class NormalCoffeeMachine : CoffeeMachine {
    override fun makeSmallCoffee() = println("Normal: Making small coffee")

    override fun makeLargeCoffee() = println("Normal: Making large coffee")
}

작은사이즈와 큰 사이즈의 커피만 만드는 기본 커피머신이 있습니다.

class EnhancedCoffeeMachine(private val coffeeMachine: CoffeeMachine) : CoffeeMachine by coffeeMachine {

    override fun makeLargeCoffee() {
        println("Enhanced: Making large coffee")
    }

    fun makeCoffeeWithMilk() {
        println("Enhanced: Making coffee with milk")
        coffeeMachine.makeSmallCoffee()
        addMilk()
    }

    private fun addMilk() {
        println("Enhanced: Adding milk")
    }
}

EnhancedCoffeeMachine은

기본 커피머신을 인자로 받고 기본 커피머신에 몇 가지 기능을 더 decorate 합니다. (우유 추가 )

class DecoratorTest {

    @Test
    fun Decorator() {
        val normalMachine = NormalCoffeeMachine()
        val enhancedMachine = EnhancedCoffeeMachine(normalMachine)

        // non-overridden behaviour
        enhancedMachine.makeSmallCoffee()
        // overridden behaviour
        enhancedMachine.makeLargeCoffee()
        // extended behaviour
        enhancedMachine.makeCoffeeWithMilk()
    }
}
//result
Normal: Making small coffee
Enhanced: Making large coffee
Enhanced: Making coffee with milk
Normal: Making small coffee
Enhanced: Adding milk

EnhancedMachine은 NomalMachine을 동적으로 확장하여 추가 기능을 조합하여 설계합니다.

 


정리 

객체에 추가적인 요건을 동적으로 첨가합니다.

데코레이터는 서브클래스를 만드는 것을 통해서 기능을 유연하게 확장할 수 있는 방법을 제공합니다

 

 

 

소스코드 보러가기

https://github.com/qjatjr1108/DesignPattern

 

qjatjr1108/DesignPattern

DesignPattern Sample. Contribute to qjatjr1108/DesignPattern development by creating an account on GitHub.

github.com

 

'DesignPattern' 카테고리의 다른 글

[Design Pattern] Bridge Pattern  (4) 2019.10.10
[Design Pattern] Proxy Pattern  (4) 2019.10.08
[Design Pattern] Facade Pattern  (2) 2019.10.08
[Design Pattern] Adapter Pattern  (4) 2019.10.07
[Design Pattern] Prototype pattern  (2) 2019.09.27

댓글