๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Code-States

[D+52] ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด (OOP)

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2020. 10. 28.

 

D+52

-  ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด (OOP) -

(OOP, ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•, Prototype)

 


 

 

OOP (Object-Oriendted-Programming)

OOP๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋“ค์ด ๊ฐ์ฒด(์˜ค๋ธŒ์ ํŠธ)๋กœ ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ๋„๋ก ์งœ์—ฌ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

 

๊ฐ์ฒด๋ฅผ ๊ด€์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

๊ฐ์ฒด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฐจ๋ก€์ฐจ๋ก€ ์ž…๋ ฅํ•ด๋‚˜๊ฐ€๋Š” ์ ˆ์ฐจ ์ง€ํ–ฅ์ ์–ธ์–ด๋ณด๋‹ค ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

1 > ์บก์Šํ™”

์บก์Š์„ ๊บผ๋‚ด ๋ฐ˜์œผ๋กœ ์ชผ๊ฐœ๋ณด๋ฉด ๊ฐ€๋ฃจ๋“ค์ด ๋ชจ์—ฌ์žˆ๋‹ค.

 

์ด ๊ฐ€๋ฃจ๋“ค์„ ๊ทธ๋ƒฅ ํ•œ๋ฒˆ์— ํ„ธ์–ด๋จน๋Š” ๊ฒƒ๊ณผ ๊ฐ€๋ฃจ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ์บก์Š ํ•œ ์•Œ์„ ๋จน๋Š” ๊ฒƒ์€

๋ชธ์†์—์„œ๋Š” ๊ฐ™์€ ๋ฐ˜์‘์„ ๋‚˜ํƒ€๋‚ผ์ง€๋Š” ๋ชฐ๋ผ๋„ ์•„๋งˆ ๊ฐ์ž์˜ ์“ด ๋ง›์„ ๋‚ด๋Š” ๊ฐ€๋ฃจ๋ฅผ ํ„ธ์–ด๋จน๋Š” ๊ฒƒ์ด

์•„๋งˆ ์“ด๋ง›์œผ๋กœ ๊ณ ํ†ต์Šค๋Ÿฌ์šธ ๊ฒƒ์ด๋‹ค.

 

๋ฐ˜๋ฉด์— ์บก์Š์€ ๊ฐ™์€ ์–‘์˜ ๊ฐ€๋ฃจ๋ฅผ ๋จน์—ˆ์Œ์—๋„ ๊ฐ€๋ฃจ๊ฐ€ ์บก์Š์•ˆ์— ๋ชจ์—ฌ ์žˆ๊ธฐ์— ์“ด ๋ง›์„ ๋Š๋‚„ ์ˆ˜ ์—†๋‹ค.

 

์ด์ฒ˜๋Ÿผ ์บก์Šํ™”๋„ ์บก์Š์ฒ˜๋Ÿผ ๋น„์Šทํ•œ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ

์บก์Šํ™”๋Š” ๊ด€๋ จ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋‚˜ ์ •๋ณด๋“ค์„ ํ•œ ๊ณณ์— ๋ชจ์•„ ์บก์Š ํ˜•ํƒœ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

 

// ์ ˆ์ฐจ ์ง€ํ–ฅ์ 

let baseSalary = 30_000;
let overtime = 10;
let day = 20;

function getWage(baseSalary, overtime, rate) {
	return baseSalary + (overtime * rate);
}


// ๊ฐ์ฒด ์ง€ํ–ฅ์  (์บก์Šํ™”)

let employee = {
	let baseSalary = 30_000,
	let overtime = 10,
	let day = 20,
    getWage: function() {
    	return this.baseSalary + (this.overtime * this.day);
    }
};
employee.getWage();

๋งŒ์•ฝ์— ์ง์›๋“ค์˜ ์ถ”๊ณผ์ˆ˜๋‹น์„ ๊ณ„์‚ฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค๋ฉด

์šฐ๋ฆฌ๋Š” ์ง์›์˜ ์‹œ๊ธ‰, ์ดˆ๊ณผ๊ทผ๋ฌด ์‹œ๊ฐ„, ์ผ์ˆ˜๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ์ด๋‹ค.

 

์ด๋•Œ ๋‹จ์ˆœํžˆ ๊ฐ ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋„ฃ๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ์‹์„ ๋งŒ๋“ค์–ด ๊ณ„์‚ฐ์„ ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด

๋‹ค์Œ ์ง์›2์„ ๊ณ„์‚ฐํ•  ๋•Œ๋Š” ๋˜ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ๋‹ค์Œ ์ง์› 3์˜ ๋ณ€์ˆ˜๋ฅผ ๋˜ ๋งŒ๋“ค๊ณ ...

์ด๋ ‡๊ฒŒ ๋œ๋‹ค๋ฉด ํšจ์œจ์„ฑ์ด ์ƒ๋‹นํžˆ ๋–จ์–ด์ง„๋‹ค.

 

ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์ด๊ฒƒ๋“ค์„ ๊ฐ์ฒด ์•ˆ์— ๋„ฃ์–ด ๊ณ„์‚ฐํ•  ๊ฒฝ์šฐ

๋ˆˆ์œผ๋กœ ๋ณด๊ธฐ์—๋„ ํŽธํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ์—๋„ ์ƒ๋‹นํžˆ ํŽธ๋ฆฌํ•˜๋‹ค.

 

2 > ์ƒ์†

๋ถ€๋ชจ๊ฐ€ ์ž์‹์„ ๋‚ณ์œผ๋ฉด ์ž์‹์˜ ๋ชจ์Šต๊ณผ ๋ถ€๋ชจ์˜ ๋ชจ์Šต์ด ๋น„์Šทํ•œ ๋ชจ์Šต์„ ๋ณด์ด๋Š”๋ฐ

์ด๋Š” ๋ถ€๋ชจ์˜ ์œ ์ „์ž๋ฅผ ์ž์‹์—๊ฒŒ ๋ฌผ๋ ค์คฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์ด์ฒ˜๋Ÿผ ์ƒ์†์€ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ์„ ํ•˜์œ„ํด๋ž˜์Šค๊ฐ€ ๋ฌผ๋ ค๋ฐ›๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

 

์ƒ์†์œผ๋กœ ์ธํ•ด ์ฝ”๋“œ์˜ ์ค‘๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋•Œ์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจํด๋ž˜์Šค๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ์„ ๋ฌผ๋ ค๋ฐ›์œผ๋ฉด์„œ

์ถ”๊ฐ€์ ์œผ๋กœ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

 

class Convertible {
	// Speed: 155 (miles / hour)
    // Weight: 1600kg
    // Engine: 3.2L S54 inline-6
}

// Roadster๋Š” Convertible์˜ ์†์„ฑ์„ ์ƒ์† ๋ฐ›์•˜๋‹ค.

class Roadster extends Convertible {
	// Speed : 165 (miles / hour)
    // Weight: 1399kg
}

์ด์ฒ˜๋Ÿผ Roadaster๋Š” Convertible์˜ ์†์„ฑ์„ ๋ฌผ๋ ค๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์—

๋”ฐ๋กœ ์ ํ˜€ ์žˆ์ง€ ์•Š์•„๋„ Roadster๋Š” Engine์˜ ์†์„ฑ์„ ๋ฌผ๋ ค๋ฐ›์€ ๊ฒƒ์ด๋‹ค.

 

3 > ์ถ”์ƒํ™”

์ถ”์ƒํ™”๋Š” ์ฝ”๋“œ๊ฐ€ ๋Œ์•„๊ฐ€๊ธฐ ์œ„ํ•ด ๋ณต์žกํ•œ ์ฝ”๋“œ๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์ง€๋งŒ

์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋Š” ์ž…์žฅ์—์„œ๋Š” ์ •ํ•ด์ง„ ๋ฒ”์œ„์˜ ํŠน์ • ๊ฐ’๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋˜๋ฏ€๋กœ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

์Šค๋งˆํŠธํฐ์„ ์˜ˆ๋กœ ๋“ค์ž๋ฉด, ์Šค๋งˆํŠธํฐ์€ ๋ฐ˜๋„์ฒด, ์•ก์ •, ๋ฐฐํ„ฐ๋ฆฌ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ฒƒ๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋งŽ์€ ๋ถ€ํ’ˆ๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์ง€๋งŒ ์†Œ๋น„์ž ์ž…์žฅ์—์„œ๋Š” ์ด๊ฒƒ๋“ค์„ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์•Œ์ง€ ๋ชปํ•œ๋‹ค๊ณ  ํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ ์Šค๋งˆํŠธํฐ ์‚ฌ์šฉ์„ ๋ชปํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—

์ด ๋˜ํ•œ ์ถ”์ƒํ™”๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

4 > ๋‹คํ˜•์„ฑ

๋Œ€๋ถ€๋ถ„์˜ ๋™๋ฌผ๋“ค์€ ์šธ์Œ์†Œ๋ฆฌ๋ฅผ ๋‚ด์ง€๋งŒ ๋™๋ฌผ๋“ค๋งˆ๋‹ค ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์šธ์Œ์†Œ๋ฆฌ๋Š” ๋‚ธ๋‹ค.

 

์ด์ฒ˜๋Ÿผ ๋™๋ฌผ์ด๋ผ๋Š” ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฒƒ์„ ๋‹คํ˜•์„ฑ์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

 


์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

ES6 classํ‚ค์›Œ๋“œ๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „ ์‚ฌ์šฉํ–ˆ๋˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋‹ค์–‘ํ•œ ํŒจํ„ด๋“ค์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

 

Class

ํด๋ž˜์Šค๋Š” ๋ชจํ˜•ํ‹€๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜๋ฉฐ, ํด๋ž˜์Šค๋ผ๋Š” ๋ชจํ˜•ํ‹€์„ ๋งŒ๋“ค์–ด๋‘๋ฉด

ํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์Šคํ„ด์Šค๋ผ๋Š” ๋ณต์ œํ’ˆ์„ ์ฐ์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

1 > Functional

๊ทธ๋Œ€๋กœ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ฐ์–ด๋‚ด๋Š” ๋ฐฉ์‹์ด๋‹ค.

var Car = function(position) {
	var someInstance = {};
    someInstance.position = position;
    someInstance.move = function() {
    	this.position += 1;
    }
    return someInstance;
};

var car1 = Car(5);

 

2 > Functional Shared

Functional๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ฉ”์„œ๋“œ์™€ ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋‚˜๋ˆ  ์ค€ ๋’ค์—

ํ•ฉ์น˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์–ธ์ œ๋“ ์ง€ ์ฐ์–ด๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

 

Functional๋ณด๋‹ค ๋ณต์žกํ•ด ๋ณด์ผ ์ˆ˜๋Š” ์žˆ๊ฒ ์ง€๋งŒ

์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์‹œ ๋งค๋ฒˆ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๋Š” Functional๊ณผ๋Š” ๋‹ฌ๋ฆฌ

Functional Shared๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋งŒ์„ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ด ์ข‹๋‹ค.

var extend = function(to, from) {
	for(var key in from) {
    	to[key] = from[key];
    }
};

var someMethods = {};
someMethods.move = function() {
	this.position += 1;
};

var Car = function(position) {
	var someInstance = {
    	position: position,
    };
    extend(someInstance, someMethods);
    return someInstance;
};

var car1 = Car(5);
var Car2 = Car(10);

 

3 > Protoypal

Protoypal์€ Functional Shared์™€ ๋น„์Šทํ•˜์ง€๋งŒ Object.create()๋ฅผ ์“ด๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋Š”๋ฐ

์ด๋•Œ Object.create()๋Š” ์ง€์ •๋œ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด ๋ฐ ์†์„ฑ์„ ๊ฐ–๋Š” ์ƒˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

var someMethods = {};
someMethods.move = function() {
	this.position += 1;
};

var Car = function(position) {
	var someInstance = Object.create(someMethods);
    someInstance.position = position;
    return someInstance;
};

var car1 = Car(5);
var car2 = Car(10);

 

4 > Pseudoclassical

Pseudoclassical์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋ฉฐ,

์ธ์Šคํ„ด์Šค๋ฅผ ์ฐ์–ด๋‚ผ ๋•Œ new๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋์ด๋‹ค.

var Car = function(position) {
	this.position = position;
};

Car.prototype.move = function() {
	this.position += 1;
};

var car1 = new Car(5);
var car2 = new Car(10);

 

 


Prototype

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ž์‹ ์˜ ๋ถ€๋ชจ ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” ๊ฐ์ฒด์™€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์œผ๋ฉฐ,

์ด๊ฒƒ์€ ๊ฐ์ฒด ์ง€ํ–ฅ์˜ ์ƒ์†์˜ ๊ฐœ๋…๊ณผ ๊ฐ™์€ ๋ถ€๋ชจ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

 

์ด๋Ÿฌํ•œ ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€