ํ๋ก๊ทธ๋๋ฐ์์์ "Composite relationship"์ "์์(Inheritance)"์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP)์์ ๊ฐ์ฒด ๊ฐ์ ๊ด๊ณ๋ฅผ ์ค๋ช ํ๋ ๋ ๊ฐ์ง ๋ค๋ฅธ ๊ฐ๋ ์ ๋๋ค.
Composite relationship์ ์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ฒด๋ค ๊ฐ์ "has-a" ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, ์ด๋ ํ ๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํฌํจํ๊ฑฐ๋ ๊ทธ ์ผ๋ถ๋ก์ ์กด์ฌํจ์ ์๋ฏธํฉ๋๋ค. ๋ฐ๋ฉด์ ์์์ "is-a" ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ฉฐ, ๊ฐ์ฒด ๊ฐ์ ์ผ๋ฐํ์ ํน์ํ๋ฅผ ๋ํ๋ ๋๋ค.
A๋ B๋ค (A is B) → Inheritance (10%)
A๋ B๋ฅผ ๊ฐ์ง๊ณ ์๋ค (A has B) → Composition (90%)
// ๋ถ๋ถ ๊ฐ์ฒด
class Engine {
void start() {
System.out.println("Engine starting");
}
}
// ์ ์ฒด ๊ฐ์ฒด
class Car {
private Engine engine;
Car() {
// Composite relationship: Car has-a Engine
this.engine = new Engine();
}
void start() {
System.out.println("Car starting");
engine.start(); // Car๋ Engine์ ํฌํจํ๊ณ ์์
}
}
์ฌ๊ธฐ์ Car ํด๋์ค๋ Engine ํด๋์ค๋ฅผ ํฌํจํ๊ณ ์์ต๋๋ค. Car๋ Engine์ ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก Composite relationship์ด ์ฑ๋ฆฝํฉ๋๋ค.
์์๊ณผ Composite relationship์ ๊ฐ๊ฐ ๋ค๋ฅธ ๋ชฉ์ ์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์ด๋ค ์ํฉ์์ ์ด๋ค ๊ฒ์ ์ฌ์ฉํ ์ง๋ ์ค๊ณ์ ์๋์ ํน์ ํ ์๊ตฌ์ฌํญ์ ๋ฐ๋ผ ๊ฒฐ์ ๋ฉ๋๋ค. ์์์ ์ฝ๋๋ฅผ ์ฌ์ฌ์ฉํ๊ณ ํด๋์ค ๊ฐ์ ์ผ๋ฐํ์ ํน์ํ๋ฅผ ํตํด ๊ณ์ธต์ ํ์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๊ณ , Composite relationship์ ํ ํด๋์ค๊ฐ ๋ค๋ฅธ ํด๋์ค๋ฅผ ํฌํจํ์ฌ ๋ณตํฉ์ ์ธ ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
A composite relationship refers to a connection between two objects or entities where one is considered a part or component of the other. It implies a "whole-part" association, often involving shared lifecycles between the objects.
Inheritance (IS-A) : IS-A relationship signifies that one object is a type of another. It is implemented using ‘extends’ and ‘implements’ keywords.
Composition : HAS-A relationship signifies that a class has a relationship with another class. For instance, Class A holds Class B’s reference and can access all properties of class B.
'Programming > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
18 Polymorphism (0) | 2024.01.20 |
---|---|
17 Abstraction (0) | 2024.01.20 |
15 Inheritance (0) | 2024.01.20 |
14 Encapsulation (feat. Access modifier) (0) | 2024.01.20 |
13 Method (0) | 2024.01.20 |
๋๊ธ