๐Ÿ“– Java OOP

๐Ÿ“– Java OOP

ํ˜„์‹ค์„ ๊ทธ๋Œ€๋กœ ์˜ฎ๊ธด๋‹ค

ํ˜„์‹ค์˜ ๊ฐ์ฒด๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ๋“ค ์ค‘์—์„œ ์ค‘์š”ํ•œ ๊ฒƒ๋“ค๋งŒ ๋‚จ๊ธฐ๊ณ  ๋ถˆํ•„์š”ํ•œ ๊ฒƒ์€ ์—†์• ๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ โ€œ์ถ”์ƒํ™” ํ•œ๋‹ค.โ€ ๋ผ๊ณ  ๋งํ•œ๋‹ค.

โ™ป๏ธ ์žฌ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ

ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ถ”์ƒํ™”๋ฅผ ํ†ตํ•ด ํ™•์žฅ์‹œํ‚ค๋Š”๊ฒƒ์˜ ์˜๋ฏธ์ด๋‹ค

์ผ๋ฐ˜ํ™”์™€ ์ถ”์ƒ ํด๋ž˜์Šค

์ธ์Šคํ„ด์Šค๊ฐ€ ๋  ์ˆ˜ ์—†๋Š” ํด๋ž˜์Šค๋ฅผ “์ถ”์ƒ ํด๋ž˜์Šค"๋ผ๊ณ  ํ•œ๋‹ค.

์ถ”์ƒ ํด๋ž˜์Šค : ์ƒ์†์„ ํ†ตํ•ด ํ™•์žฅํ•˜๋Š” ๊ฐœ๋…์˜ ํด๋ž˜์Šค๋กœ ์–ด๋–ค๊ฒƒ์— ๊ด€ํ•œ ๊ณตํ†ต์ ์ธ ๊ธฐ๋Šฅ, ์†์„ฑ์„ ๋‹ด๊ณ ์žˆ์–ด “์ผ๋ฐ˜ํ™”"๋œ ๊ฒƒ์ด๋‹ค.

Referencing ํ•ด์„œ ๋ถ€๋ชจ๊ฐ์ฒด(์ถ”์ƒ ํด๋ž˜์Šค)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ

Car c = new Bus();

๊ฐ์ฒด์ƒ์„ฑ์‹œ์˜ ์œ ์—ฐํ•˜๊ณ , ํ™•์žฅ์„ฑ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ 

Car c = new Bus();
c.๋‹ฌ๋ฆฌ๋‹ค();
c.์•ˆ๋‚ด๋ฐฉ์†กํ•˜๋‹ค();   // ๋ถˆ๊ฐ€
c.์ฃผ์ฐจ();          // ์˜ค๋ฒ„๋ผ์ด๋“œ

โญ SOLID by ๐Ÿ“” Limky

์ž๊ธฐ ์ž์‹  ํด๋ž˜์Šค ์•ˆ์— ์‘์ง‘๋„๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋†’์ด๊ณ , ํƒ€ ํด๋ž˜์Šค๋“ค ๊ฐ„ ๊ฒฐํ•ฉ๋„๋Š” ๋‚ฎ์ถ”๋Š” High Cohesion - Loose Coupling ์›์น™์„ ๊ฐ์ฒด ์ง€ํ–ฅ์˜ ๊ด€์ ์—์„œ ๋„์ž…ํ•œ ๊ฒƒ์ด๋‹ค.

โœ”๏ธ ๋‹ค์‹œ๋งํ•ด ์‘์ง‘๋„๊ฐ€ ๋†’์€ ๊ฐ ๋ชจ๋“ˆ์„ ์žฌ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, ์ˆ˜์ •์„ ์ตœ์†Œํ™”ํ•˜์—ฌ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์šฉ์ดํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

SRP (Single Responsibility Principle) ๋‹จ์ผ ์ฑ…์ž„ ์›์น™

ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์•ˆ์— ๋งŽ์€ ์†์„ฑ์„ ๋„ฃ์ง€๋ง๊ณ , ํ•„์š”ํ•œ ํด๋ž˜์Šค์˜ ์—ญํ• ๊ณผ ๋ชฉ์ ์— ๋งž๋Š” ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋“ค์„ ์ถ”๋ ค ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋“ค๋กœ ๊ฐ๊ฐ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

โŒ Father harry = new Son(); - ํ•ด๋ฆฌ๋ผ๋Š” ์ด๋ฆ„์˜ ์•„๋“ค์ด ์•„๋ฒ„์ง€์˜ ํ–‰๋™์„ํ•œ๋‹ค. โญ• Animal mickey = new Rat(); - ๋ฏธํ‚ค๋ผ๋Š” ์ด๋ฆ„์˜ ์ฅ๊ฐ€ ๋™๋ฌผ์˜ ํ–‰๋™์„ ํ•œ๋‹ค.

โœ”๏ธ ํด๋ž˜์Šค์˜ํƒ€์ž… ์ด๋ฆ„ = new ํ•˜์œ„ํด๋ž˜์Šค();๋กœ ๊ธฐ์–ตํ•˜์ž.

ISP (Interface Segregation Principle) ์ธํ„ฐํŽ˜์ด์Šค ๋ถ„๋ฆฌ ์›์น™

SRP์™€ ๊ฐ™์€ ์›์ธ์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ํ•ด๊ฒฐ์ฑ…์„ ์ œ์‹œํ•œ๋‹ค. ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค ๋ฅผ ํ†ตํ•ด ํ•„ํ„ฐ๋งํ•˜์—ฌ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋ฐฉ์‹

โœ”๏ธ ์˜๋ฏธ์ ์œผ๋กœ OCP ๊ฐœ๋ฐฉ ํ์‡„ ์›์น™ ๊ณผ ์œ ์‚ฌํ•˜๋‹ค.

๐Ÿ” ๊ฐ์ฒด Reflection

ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•˜๋Š” ๊ธฐ๋ฒ•

#๏ธโƒฃ HASHING

text, address, ๊ฐ์ฒด๋‚ด์˜ int, string, boolean๊ฐ’๋“ค์„ int๊ฐ’์œผ๋กœ ๋งŒ๋“ ๊ฒƒ.

  • ๊ฒ€์ƒ‰์„ ๋นจ๋ฆฌํ•˜๊ธฐ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค
  • ์ค‘๋ณต๋ ์ˆ˜์žˆ๋‹ค
  • ๋ฐ์ดํ„ฐ ์ž์ฒด๊ฐ€ ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๊ณ  ๊ฐ™๋‹ค๋ฉด ๋ฐ์ดํ„ฐ์˜ ๋‚ด์šฉ์ด ๊ฐ™์€์ง€๋ฅผ ๋‹ค์‹œ ๋น„๊ตํ•˜๋Š”์‹์œผ๋กœ ๊ฒ€์ƒ‰

๐Ÿ“Œ Hashcode() ์™€ Equals()

= equals

Point p1 = new Point(10, 20);
Point p2 = new Point(10, 20);
Point p3 = p2;

// == : ๋‘๊ฐ์ฒด์˜ ๋™์ผ์„ฑ
System.out.println(p1 == p2);
System.out.println(p2 == p3);

//equals : ๋‘ ๊ฐ์ฒด์˜ ๋™์งˆ์„ฑ ๋น„๊ตํ•œ๋‹ค. (๋‚ด์šฉ๋น„๊ต)
System.out.println(p1.equals(p2));     
System.out.println(p2.equals(p3));

โœ”๏ธ String์„ equals() ๋ฉ”์„œ๋“œ๋กœ ๋น„๊ตํ• ๋•Œ, String ๋ฌธ์ž์—ด๋ผ๋ฆฌ ๋น„๊ต๋˜๋Š” ๋ฐ˜๋ฉด โœ”๏ธ Point๊ฐ์ฒด๋Š” Override๊ฐ€ ์•ˆ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— == ๋กœ ๋น„๊ต๋˜์–ด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โ“ ์™œ hashcode๋ฅผ equals์™€ ๊ฐ™์ด ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์•ผ๋˜๋Š”์ง€

hashmap, hashset๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์˜ ๋น„๊ต ๊ณผ์ •์—์„œ ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง€๋Š” ๋ฌธ์ œ ๋•Œ๋ฌธ์— hashcode๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ๊ฐ™์ด ์“ด๋‹ค.

Set<Point> set = new HashSet<Point>();

Point p1 = new Point(10, 20);
set.add(p1);

Point p2 = new Point(100, 200);
set.add(p2);

Point p3 = new Point(10, 20);
set.add(p3);

for(Point p : set) {
	System.out.println(p);
}
Point [x=100, y=200]
Point [x=10, y=20]

hashcode()๊ฐ€ Override ๋˜์–ด์žˆ์„๊ฒฝ์šฐ ๋‘๊ฐœ๋งŒ ์ถœ๋ ฅ๋œ๋‹ค
Point [x=100, y=200]
Point [x=10, y=20]
Point [x=100, y=200]

equals๋งŒ ์žˆ๋‹ค๋ฉด ์„ธ๊ฐœ๊ฐ€ ๋ชจ๋‘ ์ถœ๋ ฅ
String str1 = new String("ABC");
		String str2 = new String("ABC");
		System.out.println(str1 == str2);
		System.out.println(str1.equals(str2));

		System.out.println(str1.hashCode() + " : " + str2.hashCode());
		System.out.println(System.identityHashCode(str1) + " : "+System.identityHashCode(str2));
false
true
64578 : 64578       //์ด๋ฏธ Override๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ
366712642 : 1829164700
String str3 = "ABC";
String str4 = "ABC";

System.out.println(str3 == str4);
System.out.println(str3.equals(str4));

System.out.println(str3.hashCode() + " : " + str4.hashCode());
System.out.println(System.identityHashCode(str3) + " : "+System.identityHashCode(str4));
true
true
64578 : 64578
2018699554 : 2018699554

//๋ชจ๋‘ ๊ฐ™์€ ๊ฐ์ฒด์ด๋‹ค.

String str3 = “ABC”; ์˜ ๊ณผ์ •์—์„œ๋Š” ABC๋ผ๋Š” string์˜ hashcode๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•œ๋‹ค.

์ดํ›„ String str4 = “ABC”; ์˜ step์—์„œ๋Š” ๊ฐ™์€๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋’ค ์žˆ๋‹ค๋ฉด ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋„๋ก ํ•œ๋‹ค.

์œ„์™€๊ฐ™์€ ๊ณผ์ •์€ ๋ณ€์ˆ˜์„ ์–ธ์„ Literal๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋ณ€์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ ์šฉํ•˜๋‹ค๋Š” ์ ์„ ์‹œ์‚ฌํ•œ๋‹ค.