Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package decoratorPattern;

public class Americano extends Coffee {

@Override
public void brewing() {
System.out.println("Americano Coffee");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package decoratorPattern;

public abstract class Coffee {
public abstract void brewing();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package decoratorPattern;

// 여러가지 데코레이터의 상속 목적으로만 사용할 거라서 `abstract` class로 선언
public abstract class Decorator extends Coffee {

Coffee coffee;

public Decorator(Coffee coffee) {
this.coffee = coffee;
}

@Override
public void brewing() {
coffee.brewing();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package decoratorPattern;

import java.io.*;

public class DecoratorMain {


public static void main(String[] args) throws FileNotFoundException {
Coffee coffee = new Americano();
coffee.brewing();

Coffee latteCoffee = new Latte(new Americano());
latteCoffee.brewing();

Coffee mochaCoffee = new Mocha(new Latte(new Americano()));
mochaCoffee.brewing();


Coffee whippedCreamCoffee = new WhippedCream(new Mocha(new Latte(new Americano())));
whippedCreamCoffee.brewing();

// 자바 API 속의 데코레이터 패턴
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("a.out")));

/**
* public class FileInputStream extends InputStream {
* public FileInputStream(String name) throws FileNotFoundException {
* this(name != null ? new File(name) : null);
* }
* }
*/

/***
* public class InputStreamReader extends Reader {
* public InputStreamReader(InputStream in) {
* super(in);
* sd = StreamDecoder.forInputStreamReader(in, this,
* Charset.defaultCharset()); // ## check lock object
* }
* }
*/

/**
* BufferedReader extends Reader {
* public BufferedReader(Reader in) {
* this(in, defaultCharBufferSize);
* }
* }
*/
}
}
14 changes: 14 additions & 0 deletions oop-in-spring/practice/design-pattern/decoratorPattern/Latte.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package decoratorPattern;

public class Latte extends Decorator {

public Latte(Coffee coffee) {
super(coffee);
}

@Override
public void brewing() {
super.brewing();
System.out.println("Adding Milk");
}
}
14 changes: 14 additions & 0 deletions oop-in-spring/practice/design-pattern/decoratorPattern/Mocha.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package decoratorPattern;

public class Mocha extends Decorator {

public Mocha(Coffee coffee) {
super(coffee);
}

@Override
public void brewing() {
super.brewing();
System.out.println("Adding Mocha syrup");
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package decoratorPattern;

public class WhippedCream extends Decorator {

public WhippedCream(Coffee coffee) {
super(coffee);
}

@Override
public void brewing() {
super.brewing();
System.out.println("Adding Whipping Cream");
}
}