axios 를 여러개 쓸 경우 배열로 들어감
(async () => {
const data: Array<Promise<IResponse>> = await Promise.all([
axios
.get("https://api.publicapis.org/entries")
.then((res) => res.data.entries)
.catch((e) => console.log(e)),
axios
.get("https://api.publicapis.org/entries")
.then((res) => res.data.entries)
.catch((e) => console.log(e)),
axios
.get("https://api.publicapis.org/entries")
.then((res) => res.data.entries)
.catch((e) => console.log(e)),
]);
console.log("fetch result1", data[0]);
console.log("fetch result2", data[1]);
console.log("fetch result3", data[2]);
})();
동일한 클래스를 무한 실행을 방지하기 위해 사용(통일성, 충돌의 위험)
private 을 사용해 클래스 내에서만 생성할 수 있도록 수정 (getInstance()의 if문을 통해 한 번의 인스턴스 생성 가능)
동일한 인스턴스를 뽑아쓰는 형태가 됨
class Singleton {
// private static _instance: Singleton;
private static _instance = new Singleton();
private constructor() { // private 을 붙여서 아무데서나 인스턴스 생성하는 것을 막음
console.log("싱글턴 생성!!");
}
public static getInstance() {
// if (this._instance) {
// return this._instance;
// }
// this._instance = new Singleton();
return this._instance;
}
}
const single1: Singleton = Singleton.getInstance();
const single2: Singleton = Singleton.getInstance();
const single3: Singleton = Singleton.getInstance();
const single4: Singleton = Singleton.getInstance();
const single5: Singleton = Singleton.getInstance();
// 실행 결과는 한번만 나옴
두 객체 사이의 래퍼 역할
클라이언트 인터페이스를 변경하지 않고 새로운 시스템의 기능을 제공할 수 있게 함
interface ICalculator {
addAandB(a: number, b: number): number;
minusAandB(a: number, b: number): number;
}
class CalculatorImpl {
add(a: number, b: number): number {
return a + b;
}
minus(a: number, b: number): number {
return a - b;
}
}
class Adapter extends CalculatorImpl implements ICalculator {
addAandB(a: number, b: number): number { // 인터페이스 메소드 사용
return this.add(a, b); // 추상클래스 메소드 사용
}
minusAandB(a: number, b: number): number {
return this.minus(a, b);
}
}
const cal: ICalculator = new Adapter();
console.log(cal.addAandB(1, 2));
console.log(cal.minusAandB(5, 3));