[Design pattern] Decorator Pattern ๐Ÿ“ฟ, (Structural patterns)

ํ™์ •์™„ยท2021๋…„ 8์›” 13์ผ
0

Design pattern

๋ชฉ๋ก ๋ณด๊ธฐ
7/7
post-thumbnail

Decorator pattern ๐Ÿ“ฟ

๊ธฐ๋ณธ ๊ฐ์ฒด์— ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ๋™์ ์œผ๋กœ ์œ ์—ฐํ•˜๊ฒŒ ์ฒจ๊ฐ€ํ•˜๋Š” ํŒจํ„ด

  • ์žฅ์ 

    • ๊ฐ์ฒด์— ๋™์ (dynamic)์œผ๋กœ ๊ธฐ๋Šฅ ์ถ”๊ฐ€๊ฐ€ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ€๋Šฅ
  • ๋‹จ์ 

    • Decorator class๋“ค์ด ๊ณ„์† ์ถ”๊ฐ€๋˜์–ด class๊ฐ€ ๋งŽ์•„์งˆ ์ˆ˜ ์žˆ๋‹ค.

ํ™œ์šฉ ์ƒํ™ฉ

GUI ํˆดํ‚ท์—์„œ ์–ด๋–ค ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ์š”์†Œ์—๋งŒ scrolling๊ณผ ๊ฐ™์€ ํ–‰๋™์ด๋‚˜ border์™€ ๊ฐ™์€ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ, ํ•˜๋‚˜์˜ ๊ฐ์ฒด์— ์†์„ฑ์ด ์ถ”๊ฐ€๋จ์œผ๋กœ ๋‹ค๋ฅธ ์ฑ…์ž„์ด ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•œ๋‹ค.


์ฆ‰, ์ „์ฒด ํด๋ž˜์Šค์— ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ,
๊ฐœ๋ณ„์ ์ธ ๊ฐ์ฒด์— ์ƒˆ๋กœ์šด ์ฑ…์ž„์„ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.


์ž๋ฐ” API ํŒŒ์ผ I/O ๊ด€๋ จ ๋ถ€๋ถ„์—์„œ๋„ ์‚ฌ์šฉ๋œ๋‹ค. ๐Ÿ‘‡

BufferedReader bufferedreader = new BufferedReader(new FileReader(new File("daydream.txt")));

๊ตฌ์กฐ โš™

  • Tree

    • DefaultTree ์™€ Decorator๊ฐ€ ๊ตฌํ˜„ํ•  interface
  • DefaultTree

    • Decorate ๋ฐ›์„ ๊ฐ์ฒด
    • ๊ธฐ๋Šฅ ์ถ”๊ฐ€๋ฅผ ๋ฐ›์„ ๊ธฐ๋ณธ ๊ฐ์ฒด
  • Decorator

    • Decorate ํ•  ๊ฐ์ฒด์˜ abstract class
    • ๊ธฐ๋Šฅ ์ถ”๊ฐ€๋ฅผ ํ•  ๊ฐ์ฒด๋Š” DefaultTree๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
  • Red, Blue

    • Decorator๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„ํ•  ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ ๊ฐ์ฒด
    • DefaultTree์— ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์—ˆ๋‹ค.
  • DecoratorPattern

    • ๋ฉ”์ธ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ class

๊ตฌํ˜„ ๐Ÿ› 

  • Tree
interface Tree {

	String decorate();

}

  • DefaultTree
class DefaultTree implements Tree {

	@Override
	public String decorate() {
		return "Tree";
	}

}

  • Decorator
abstract class Decorator implements Tree {

	private Tree tree;

	public Decorator(Tree tree) {
		this.tree = tree;
	}

	@Override
	public String decorate() {
		return tree.decorate();
	}

}

  • Red, Blue
class Red extends Decorator {

	public Red(Tree tree) {
		super(tree);
	}

	public String addRed() {
		return " with Red";
	}

	@Override
	public String decorate() {
		return super.decorate() + addRed();
	}

}

class Blue extends Decorator {

	public Blue(Tree tree) {
		super(tree);
	}

	public String addBlue() {
		return " with Blue";
	}

	@Override
	public String decorate() {
		return super.decorate() + addBlue();
	}

}

  • DecoratorPattern
public class DecoratorPattern {

	public static void main(String[] args) {

		Tree tree = new DefaultTree();
		System.out.println(tree.decorate());

		Tree treeWithRed = new Red(new DefaultTree());
		System.out.println(treeWithRed.decorate());

		Tree treeWithRedWithBlue = new Blue(new Red(new DefaultTree()));
		System.out.println(treeWithRedWithBlue.decorate());
	}

}

์‹คํ–‰ ๊ฒฐ๊ณผ ๐Ÿง

Tree
Tree with Red
Tree with Red with Blue
  • super๋ฅผ ํ†ตํ•ด ๋“ค์–ด์˜ค๋Š” Tree์˜ operation((decorate)) ์„ ๋จผ์ € ์ˆ˜ํ–‰

์œ„ ์‹คํ–‰ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์œ ์—ฐํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.


profile
์Šต๊ด€์ด ์ „๋ถ€๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€