Instantiation Patterns
자료구조 Stack, Queue 으로 구현
Class 선언이 나오기 전 사용되었던 4가지 방법
Pseudoclassical (prototype 선언 방식은 현재도 많이 쓰임)
1. Functional
2. Functional-shared
3. Prototypal
4. Pseudoclassical
1. Functional
const Stack = function() {
let stackInstance = {};
let storage = {};
let count = 0;
stackInstance.push = function(value) {
storage[count] = value;
count++;
};
stackInstance.pop = function() {
if (count === 0) return;
let deletedItem = storage[count - 1];
delete storage[count - 1];
count--;
return deletedItem;
};
stackInstance.size = function() {
return count;
};
return stackInstance;
};
let myStack = Stack();
myStack.push('a');
myStack.push('b');
myStack.pop();
myStack.pop();
myStack.pop();
myStack.size();
- Stack 이라는 Constructor 함수 생성
- 함수안에 stackInstance라는 객체를 생성하고 그 안에 메소드를 넣는다.
- return stackInstance를 통해 생성된 객체를 반환한다.
2. Functional-shared
인스턴스가 사용할 메소드를 따로 만들어서 주소값을 참조하는 형식
각각의 인스턴스가 같은 메소드를 참조하기 때문에 메모리 효율이 좋아짐
- (불필요한 메모리 사용 방지)
Functional-shared - Queue
const extend = function(to, from) {
for (let key in from) {
to[key] = from[key];
}
};
const queueMethods = {
enqueue: function(value) {
this.storage[this.index] = value;
this.index++;
},
dequeue: function() {
if (this.index === this.delIndex) return;
let delItem = this.storage[this.delIndex];
delete this.storage[this.delIndex];
this.delIndex++;
return delItem;
},
size: function() {
return this.index - this.delIndex;
}
};
const Queue = function() {
let queueInstance = {
storage: {},
index: 0,
delIndex: 0
};
extend(queueInstance, queueMethods);
return queueInstance;
};
3. Prototypal
Functional-shared 와 비슷하지만 method를 참조하는 방식이 다름
Object.create 를 통해 method를 prototype으로 하는 객체를 생성
const stackMethods = {
push: function(value) {
this.storage[this.count] = value;
this.count++;
},
pop: function() {
if (this.count === 0) return;
let delItem = this.storage[this.count - 1];
delete this.storage[this.count - 1];
this.count--;
return delItem;
},
size: function() {
return this.count;
}
};
const Stack = function() {
let stackInstance = Object.create(stackMethods);
stackInstance.storage = {};
stackInstance.count = 0;
return stackInstance;
};
4. Pseudoclassical
클래스가 될 Queue 함수 작성
Queue의 prototype에 method를 추가
인스턴스 생성시 new 키워드 사용
const Queue = function() {
this.storage = {};
this.index = 0;
this.delIndex = 0;
};
Queue.prototype.enqueue = function(value) {
this.storage[this.index] = value;
this.index++;
};
Queue.prototype.dequeue = function() {
if (this.index === this.delIndex) return;
let delItem = this.storage[this.delIndex];
delete this.storage[this.delIndex];
this.delIndex++;
return delItem;
};
Queue.prototype.size = function() {
return this.index - this.delIndex;
};
let myQueue = new Queue();
myQueue.enqueue('a');
myQueue.dequeue();
myQueue.size();