Skip to content

Latest commit

ย 

History

History
108 lines (81 loc) ยท 3.36 KB

Composite Pattern.md

File metadata and controls

108 lines (81 loc) ยท 3.36 KB

Composite Pattern

๋ชฉ์ 

compositie pattern์˜ ์‚ฌ์šฉ ๋ชฉ์ ์€ object์˜ hierarchies๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ๊ฐ๊ฐ์˜ object๋ฅผ ๋…๋ฆฝ์ ์œผ๋กœ ๋™์ผํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒํ•œ๋‹ค.

์•„๋ž˜ Composite pattern์˜ class diagram์„ ๋ณด์ž

composite pattenr

์œ„์˜ ๊ทธ๋ฆผ์˜ Leaf ํด๋ž˜์Šค์™€ Composite ํด๋ž˜์Šค๋ฅผ ๊ฐ™์€ interface๋กœ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด์„œ Component abstract ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ์„ ์ฝ”๋“œ๋กœ ํ‘œํ˜„ ํ•˜์˜€๋‹ค.

Component ํด๋ž˜์Šค

public class Component {
    public void operation() {
        throw new UnsupportedOperationException();
    }
    public void add(Component component) {
        throw new UnsupportedOperationException();
    }

    public void remove(Component component) {
        throw new UnsupportedOperationException();
    }

    public Component getChild(int i) {
        throw new UnsupportedOperationException();
    }
}

Leaf ํด๋ž˜์Šค์™€ Compositie ํด๋ž˜์Šค๊ฐ€ ์ƒ์†ํ•˜๋Š” Component ํด๋ž˜์Šค๋กœ Leaf ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์‹œ exception์„ ๋ฐœ์ƒ์‹œํ‚ค๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

Leaf ํด๋ž˜์Šค

public class Leaf extends Component {
    String name;
    public Leaf(String name) {
        ...
    }

    public void operation() {
        .. something ...
    }
}

Composite class

public class Composite extends Component {
    ArrayList components = new ArrayList();
    String name;

    public Composite(String name) {
        ....
    }

    public void operation() {
        Iterator iter = components.iterator();
        while (iter.hasNext()) {
            Component component = (Component)iter.next();
            component.operation();
        }
    }
    public void add(Component component) {
        components.add(component);
    }

    public void remove(Component component) {
        components.remove(component);
    }

    public Component getChild(int i) {
        return (Component)components.get(i);
    }
}

๊ตฌํ˜„ ์‹œ ๊ณ ๋ คํ•ด์•ผํ•  ์‚ฌํ•ญ

  • ์œ„์˜ ์ฝ”๋“œ๋Š” parent๋งŒ์ด child๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ตฌํ˜„ ์ด์ „์— child๊ฐ€ parent๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•˜๋Š”์ง€ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.
  • ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ children์„ ๊ด€๋ฆฌํ•  ๊ฒƒ์ธ๊ฐ€?

Children ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ 2๊ฐ€์ง€ Composite pattern

composite pattenr

์œ„์˜ ์˜ˆ์ œ๋กœ Component ํด๋ž˜์Šค์— add, removem getChild ๊ฐ™์€ method๊ฐ€ ์„ ์–ธ์ด ๋˜์–ด์žˆ์œผ๋ฉฐ Transparency๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

์žฅ์  : Leaf ํด๋ž˜์Šค์™€ Composite ํด๋ž˜์Šค๋ฅผ ๊ตฌ๋ถ„ํ•  ํ•„์š”์—†์ด Component Class๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์  : Leaf ํด๋ž˜์Šค๊ฐ€ chidren ๊ด€๋ฆฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ run time์— exception์ด ๋ฐœ์ƒํ•œ๋‹ค.

composite pattenr

์ด์ „ ์˜ˆ์ œ์—์„œ children์„ ๊ด€๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ Composite ํด๋ž˜์Šค์— ์„ ์–ธ ๋˜์–ด์žˆ์œผ๋ฉฐ Safety๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

์žฅ์  : Leaf ํด๋ž˜์Šค๊ฐ€ chidren ๊ด€๋ฆฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ compile time์— ๋ฌธ์ œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์  : Leaf ํด๋ž˜์Šค์™€ Composite ํด๋ž˜์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

๊ด€๋ จ ํŒจํ„ด

Decorator

๊ณตํ†ต์  : composition์ด ์žฌ๊ท€์ ์œผ๋กœ ๋ฐœ์ƒํ•œ๋‹ค.

์ฐจ์ด์  : decorator ํŒจํ„ด์€ responsibilites๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด์ง€๋งŒ composite ํŒจํ„ด์€ hierarchy๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค.

Iterator

๊ณตํ†ต์  : aggregate object์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•œ๋‹ค.