ํฉ์ฑ (Composite) ํจํด
๐ ํฉ์ฑ (Composite) ํจํด์ด๋?
-
๊ฐ์ฒด๋ค์ ๊ด๊ณ๋ฅผ ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ๊ตฌ์ฑํ์ฌ ์ ์ฒด-๋ถ๋ถ ๊ณ์ธต์ ํํํ๋ ํจํด์ผ๋ก ์ฌ๋ฌ ๊ฐ์ ๊ฐ์ฒด๋ค๋ก ๊ตฌ์ฑ๋ ๋ณตํฉ ๊ฐ์ฒด์ ๋จ์ผ ๊ฐ์ฒด๋ฅผ ํด๋ผ์ด์ธํธ์์ ๊ตฌ๋ณ ์์ด ๋ค๋ฃจ๊ฒ ํ๋ค.
-
์ฆ, ์ ์ฒด-๋ถ๋ถ์ ๊ด๊ณ๋ฅผ ๊ฐ๋ ๊ฐ์ฒด๋ค ์ฌ์ด์ ๊ด๊ณ๋ฅผ ์ ์ํ ๋ ์ ์ฉํ๋ค.
- ํด๋ผ์ด์ธํธ๋ ์ ์ฒด์ ๋ถ๋ถ์ ๊ตฌ๋ถํ์ง ์๊ณ ๋์ผํ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๐ ํฉ์ฑ ํจํด์ ๊ตฌ์ฑ ์์
-
Base Component
-
๋ฒ ์ด์ค ์ปดํผ๋ํธ๋ ํด๋ผ์ด์ธํธ๊ฐ composition๋ด์ ์ค๋ธ์ ํธ๋ค์ ๋ค๋ฃจ๊ธฐ ์ํด ์ ๊ณต๋๋ ์ธํฐํ์ด์ค๋ฅผ ๋งํ๋ค.
-
๋ฒ ์ด์ค ์ปดํฌ๋ํธ๋ ์ธํฐํ์ด์ค ๋๋ ์ถ์ ํด๋์ค๋ก ์ ์๋๋ฉฐ ๋ชจ๋ ์ค๋ธ์ ํธ๋ค์๊ฒ ๊ณตํต๋๋ ๋ฉ์๋๋ฅผ ์ ์ํด์ผ ํ๋ค.
-
-
Leaf
-
composition ๋ด ์ค๋ธ์ ํธ๋ค์ ํ๋์ ์ ์ํ๋ค.
-
๊ตฌ์ฒด์ ์ธ ๋ถ๋ถ ํด๋์ค๋ก ๋จ์ผ ๊ฐ์ฒด[Base Component]๋ฅผ ๊ตฌํํ๋ค.
-
Leaf๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ๋ํด ์ฐธ์กฐ๋ฅผ ๊ฐ์ง๋ฉด ์๋๋ค.
-
-
Composite
- ๋ณตํฉ ๊ฐ์ฒด ๊ทธ๋ฃน์ ํํํ ํด๋์ค๋ก ์ ์ฒด ํด๋์ค์ด๋ค.
- ์ญ์ ๋จ์ผ ๊ฐ์ฒด[Base Component]๋ฅผ ๊ตฌํํ๋ค.
- Leaf ๊ฐ์ฒด๋ค๋ก ์ด๋ฃจ์ด์ ธ ์์ผ๋ฉฐ ๋ฒ ์ด์ค ์ปดํผ๋ํธ ๋ด ๋ช ๋ น๋ค์ ๊ตฌํํ๋ค.
- ๋ณตํฉ ๊ฐ์ฒด ๊ทธ๋ฃน์ ํํํ ํด๋์ค๋ก ์ ์ฒด ํด๋์ค์ด๋ค.
๐ BaseComponent - Leaf์ Composite์ ๊ณตํต๋๋ ๋ฉ์๋๋ค์ ์ ์
- Shape.java - ๋ํ์ ๊ทธ๋ฆฌ๋ draw ๋ฉ์๋ ์ ์
package StructurePattern.CompositPattern;
/**
* Base Component - ๋ฒ ์ด์ค ์ปดํฌ๋ํธ๋ ํด๋ผ์ด์ธํธ๊ฐ composition ๋ด์ ์ค๋ธ์ ํธ๋ค์ ๋ค๋ฃจ๊ธฐ ์ํด ์ ๊ณต๋๋ ์ธํฐํ์ด์ค๋ฅผ ๋งํฉ๋๋ค.
* ๋ฒ ์ด์ค ์ปดํฌ๋ํธ๋ ์ธํฐํ์ด์ค ๋๋ ์ถ์ ํด๋์ค๋ก ์ ์๋๋ฉฐ ๋ชจ๋ ์ค๋ธ์ ํธ๋ค์๊ฒ ๊ณตํต๋๋ ๋ฉ์๋๋ฅผ ์ ์ํด์ผ ํฉ๋๋ค.
*/
public interface Shape {
public void draw(String fillColor);
}
๐ Leaf Objects - Leaf ๊ฐ์ฒด๋ค์ ๋ณตํฉ์ฒด์ ํฌํจ๋๋ ์์๋ก, Base Component๋ฅผ ๊ตฌํํด์ผ ํ๋ค.
- Traingle.java
package StructurePattern.CompositPattern;
/*
Leaf ๊ฐ์ฒด๋ค์ ๋ณตํฉ์ฒด์ ํฌํจ๋๋ ์์๋ก, Base Component๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.
*/
public class Triangle implements Shape {
@Override
public void draw(String fillColor) {
System.out.println("Drawing Triangle with color : " + fillColor);
}
}
- Circle.java
package StructurePattern.CompositPattern;
/*
Leaf ๊ฐ์ฒด๋ค์ ๋ณตํฉ์ฒด์ ํฌํจ๋๋ ์์๋ก, Base Component๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.
*/
public class Circle implements Shape {
@Override
public void draw(String fillColor) {
System.out.println("Drawing Circle with color : " + fillColor);
}
}
๐ Composite Objects - Composite ๊ฐ์ฒด๋ Leaf ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ฉฐ, Base Component๋ฅผ ๊ตฌํ
- Drawing.java
package StructurePattern.CompositPattern;
import java.util.ArrayList;
import java.util.List;
/*
Composite ๊ฐ์ฒด๋ Leaf ๊ฐ์ฒด๋ค์ ํฌํจํ๊ณ ์์ผ๋ฉฐ,
Base Component๋ฅผ ๊ตฌํํ ๋ฟ๋ง ์๋๋ผ Leaf ๊ทธ๋ฃน์ ๋ํด add์ remove๋ฅผ ํ ์ ์๋ ๋ฉ์๋๋ค์ ํด๋ผ์ด์ธํธ์๊ฒ ์ ๊ณตํฉ๋๋ค.
*/
public class Drawing implements Shape {
private List<Shape> shapeList = new ArrayList<>();
@Override
public void draw(String fillColor) {
for ( Shape oneShape : shapeList ) {
oneShape.draw(fillColor);
}
}
public void add(Shape s) {
shapeList.add(s);
}
public void remove(Shape s) {
shapeList.remove(s);
}
public void clear() {
System.out.println("Clearing all the shapes form drawing");
this.shapeList.clear();
}
}
๐ Test ์ฝ๋
- TestCompositePattern.java
package StructurePattern.CompositPattern;
import java.util.ArrayList;
import java.util.List;
/*
๊ตฌ์กฐ ํจํด์ด๋ ์์ ํด๋์ค๋ค์ ์์๊ณผ ํฉ์ฑ์ ์ด์ฉํ์ฌ ๋ ํฐ ํด๋์ค๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ ํจํด์
๋๋ค.
์ด ํจํด์ ์ฌ์ฉํ๋ฉด ์๋ก ๋
๋ฆฝ์ ์ผ๋ก ๊ฐ๋ฐํ ํด๋์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ง์น ํ๋์ธ ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋, ์ฌ๋ฌ ์ธํฐํ์ด์ค๋ฅผ ํฉ์ฑ(Composite)ํ์ฌ ์๋ก ๋ค๋ฅธ ์ธํฐํ์ด์ค๋ค์ ํต์ผ๋ ์ถ์์ ์ ๊ณตํฉ๋๋ค.
๊ตฌ์กฐ ํจํด์ ์ค์ํ ํฌ์ธํธ๋ ์ธํฐํ์ด์ค๋ ๊ตฌํ์ ๋ณตํฉํ๋ ๊ฒ์ด ์๋๋ผ ๊ฐ์ฒด๋ฅผ ํฉ์ฑํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค๋ ๊ฒ์
๋๋ค.
์ด๋ ์ปดํ์ผ ๋จ๊ณ์์๊ฐ ์๋ ๋ฐํ์ ๋จ๊ณ์์ ๋ณตํฉ ๋ฐฉ๋ฒ์ด๋ ๋์์ ๋ณ๊ฒฝํ ์ ์๋ค๋ ์ ์์ ์ ์ฐ์ฑ์ ๊ฐ์ต๋๋ค.
*/
public class TestCompositePattern {
public static void main(String[] args) {
Triangle triangle = new Triangle();
Triangle triangle1 = new Triangle();
Circle circle = new Circle();
Drawing drawing = new Drawing();
drawing.add(triangle);
drawing.add(triangle1);
drawing.add(circle);
drawing.draw("Red");
System.out.println("==========================================");
List<Shape> shapes = new ArrayList<>();
shapes.add(drawing);
shapes.add(new Triangle());
shapes.add(new Circle());
for (Shape shape : shapes) {
shape.draw("Green");
}
}
}
-
drawing - Composite[์ ์ฒด] ๊ฐ์ฒด๋ฅผ ํตํด Triangle, Circle ๋ฑ์ Leaf[๋ถ๋ถ] ๊ฐ์ฒด๋ค์ ๊ทธ๋ฃน์ผ๋ก ๋ฌถ์ด์ ํ๋ฒ์ ๋์์ ์ํํ ์ ์๋ค.
-
๋ํ drawing ๊ฐ์ฒด ๋ํ ๋ค๋ฅธ ๋ํ๋ค๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก Shape ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์๊ธฐ ๋๋ฌธ์ drawing์ด ๋ค๋ฅธ ๋ํ๋ค๊ณผ ํจ๊ป ์ทจ๊ธ ๋ ์์๋ค.
-
์ฆ, ํด๋ผ์ด์ธํธ ์ ์ฅ์์ ์ ์ฒด[Composite]์ ๋ถ๋ถ[Leaf] ์ ๊ตฌ๋ณ ์์ด ์ฌ์ฉํ ์ ์๋ค.
๐ ์คํ ๊ฒฐ๊ณผ
Drawing Triangle with color : Red
Drawing Triangle with color : Red
Drawing Circle with color : Red
==========================================
Drawing Triangle with color : Green
Drawing Triangle with color : Green
Drawing Circle with color : Green
Drawing Triangle with color : Green
Drawing Circle with color : Green
Comments