๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ˜•Java

[Java ์ดํ•ดํ•˜๊ธฐ] 20. Java์˜ has-a ๊ด€๊ณ„

by Luvbit 2024. 5. 27.


 

 

๐Ÿ’œ Java์˜ has-a ๊ด€๊ณ„๋ž€?

"has-a" ๊ด€๊ณ„๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฐœ๋… ์ค‘ ํ•˜๋‚˜๋กœ,
ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ด๋Š” ์ปดํฌ์ง€์…˜(composition)๊ณผ ์—ฐ๊ด€(composition and aggregation)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.

 

 

 

 

 

"has-a" ๊ด€๊ณ„๋Š” ํ•œ ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ž์‹ ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ํฌํ•จํ•˜๋Š” ๊ด€๊ณ„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
์ด ๊ด€๊ณ„๋Š” ๋‘ ํด๋ž˜์Šค ๊ฐ„์˜ ์˜์กด์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ƒ์†(inheritance)๊ณผ ๋Œ€๋น„๋˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.
์ƒ์†์ด "is-a" ๊ด€๊ณ„(์˜ˆ: A๋Š” B์ด๋‹ค)๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค๋ฉด, "has-a" ๊ด€๊ณ„๋Š” "A๋Š” B๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค"๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ƒ์†์€ ์ถ”ํ›„ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

๐Ÿ’™has-a์˜ ํŠน์ง•

์ปดํฌ์ง€์…˜
(Composition)
์ปดํฌ์ง€์…˜์€ ๊ฐ•ํ•œ "has-a" ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ํฌํ•จ๋œ ๊ฐ์ฒด์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๊ฐ€ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด์˜ ์ƒ๋ช… ์ฃผ๊ธฐ์— ์ข…์†๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜๋ฉด ํฌํ•จ๋œ ๊ฐ์ฒด๋„ ํ•จ๊ป˜ ์†Œ๋ฉธ๋ฉ๋‹ˆ๋‹ค.
 ์—ฐ๊ด€
(Aggregation)
์—ฐ๊ด€์€ ์•ฝํ•œ "has-a" ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ํฌํ•จ๋œ ๊ฐ์ฒด์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๊ฐ€ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด์™€ ๋…๋ฆฝ์ ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜๋”๋ผ๋„ ํฌํ•จ๋œ ๊ฐ์ฒด๋Š” ๋ณ„๋„๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

 


๐Ÿ”has-a ์˜ˆ์ œ 

 

์ปดํฌ์ง€์…˜ ์˜ˆ์ œ ) 

์•„๋ž˜๋Š” ์ปดํฌ์ง€์…˜์„ ์‚ฌ์šฉํ•œ "has-a" ๊ด€๊ณ„์˜ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. 

'Car' ํด๋ž˜์Šค๋Š” 'Engine" ํด๋ž˜์Šค๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

 

class Engine {
	//Engine ํด๋ž˜์Šค๋Š” type์ด๋ผ๋Š” ํ•„๋“œ๋ฅผ ๊ฐ€์ง.
    private String type; 

    public Engine(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

class Car {
	//Car ํด๋ž˜์Šค๋Š” engine์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง.
    private Engine engine;


	//์ƒ์„ฑ์ž์—์„œ ๊ฐ์ฒด ์ดˆ๊ธฐํ™”(์ƒ์„ฑ)
    public Car(String engineType) {
        this.engine = new Engine(engineType);
    }

	//์ž๋™์ฐจ ์‹œ๋™ ์‹œ
    public void start() {
        System.out.println("Starting the car with " + engine.getType() + " engine.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("V8");
        car.start(); // ์ถœ๋ ฅ: Starting the car with V8 engine.
    }
}

 

์œ„ ์˜ˆ์ œ์—์„œ Car ํด๋ž˜์Šค๋Š” Engine ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ด๋Š” "Car has an Engine"์ด๋ผ๋Š” "has-a" ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Car ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜๋ฉด Engine ๊ฐ์ฒด๋„ ํ•จ๊ป˜ ์†Œ๋ฉธ๋ฉ๋‹ˆ๋‹ค.

Car๋Š” Engine์ด ์—†์œผ๋ฉด ๋‹ฌ๋ฆด ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ Compsition ๊ด€๊ณ„๋ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

 

์—ฐ๊ด€ ์˜ˆ์ œ ) 

์—ฐ๊ด€์„ ์‚ฌ์šฉํ•œ "has-a" ๊ด€๊ณ„์˜ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

Library ํด๋ž˜์Šค๋Š” ์—ฌ๋Ÿฌ Book ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

class Book {
	//์ฑ…์€ ์ œ๋ชฉ์„ ํ•„๋“œ๋กœ ๊ฐ€์ง„๋‹ค.
	private String title;
    
	public Book(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}
}

class Library {
	//๋„์„œ๊ด€์€ ์ฑ…'๋“ค'์„ ๊ฐ€์ง„๋‹ค.
	private Book[] books;
	private int count;

	public Library(int size) {
		this.books = new Book[size];
		this.count = 0;
	}

	//์ฑ…์„ ์ถ”๊ฐ€ํ• ๋•Œ, ์ฒ˜์Œ ์ •ํ–ˆ๋˜ ์ฑ…์žฅ์˜ ์‚ฌ์ด์ฆˆ (์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€)
	//์„ ๋„˜์œผ๋ฉด ์—๋Ÿฌ์ฒ˜๋ฆฌ.
	public void addBook(Book book) {
		if (count < books.length) {
			books[count] = book;
			count++;
		} else {
			System.out.println("Library is full, cannot add more books.");
		}
	}

	//์ฑ…๋“ค์„ ๋ชจ๋‘ ํ™•์ธํ•˜๋Š” ๋กœ์ง
	public void showBooks() {
		for (int i = 0; i < count; i++) {
			//์ฑ…๋“ค์˜ ์ œ๋ชฉ ์ถœ๋ ฅ
			System.out.println("Book: " + books[i].getTitle());
		}
	}
}

public class Main {
	public static void main(String[] args) {
		Library library = new Library(5); // ์ตœ๋Œ€ 5๊ถŒ์˜ ์ฑ…์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋„์„œ๊ด€
        
		//์ฑ… ๋‘๊ถŒ ์ƒ์„ฑ
		Book book1 = new Book("Effective Java");
		Book book2 = new Book("Clean Code");

		library.addBook(book1);
		library.addBook(book2);

		//์ถœ๋ ฅ
		library.showBooks();
	}
}

 

์œ„ ์˜ˆ์ œ์—์„œ Library ํด๋ž˜์Šค๋Š” ์—ฌ๋Ÿฌ Book ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” "Library has Books"๋ผ๋Š” "has-a" ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Library ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜๋”๋ผ๋„ Book ๊ฐ์ฒด๋Š” ๋ณ„๋„๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„์„œ๊ด€์— "Effective Java"๋ผ๋Š” ์ฑ…์ด ์—†์–ด๋„ ๋„์„œ๊ด€์€ ์šด์˜ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒŒ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

 

" ์š”์•ฝ " 

  • "has-a" ๊ด€๊ณ„: ํ•œ ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ž์‹ ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ํฌํ•จํ•˜๋Š” ๊ด€๊ณ„
  • ์ปดํฌ์ง€์…˜(Composition): ํฌํ•จ๋œ ๊ฐ์ฒด์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๊ฐ€ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด์— ์ข…์†๋˜๋Š” ๊ฐ•ํ•œ "has-a" ๊ด€๊ณ„.
  • ์—ฐ๊ด€(Aggregation): ํฌํ•จ๋œ ๊ฐ์ฒด์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๊ฐ€ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด์™€ ๋…๋ฆฝ์ ์ธ ์•ฝํ•œ "has-a" ๊ด€๊ณ„.
  • ์˜ˆ์ œ: Car์™€ Engine์˜ ๊ด€๊ณ„๋Š” ์ปดํฌ์ง€์…˜์„, Library์™€ Book์˜ ๊ด€๊ณ„๋Š” ์—ฐ๊ด€์„ ๋‚˜ํƒ€๋ƒ„.

 

 

 

 

 

 

 

 

 

 

has-a ๊ด€๊ณ„๊ฐ€ ์ผ์ƒ ์ƒํ™œ์—์„œ ๋ฌด์—‡์ด ์žˆ์„์ง€ ์ƒ๊ฐํ•ด ๋ณด๊ณ ,
์ง์ ‘ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์„ธ์š”!

์งˆ๋ฌธ์€ ์–ธ์ œ๋“ ์ง€ ๋Œ“๊ธ€์— ๋‚จ๊ฒจ์ฃผ์„ธ์š”.
๐Ÿฉต๐Ÿฐ๐Ÿฉต