본문 바로가기

디자인 패턴29

[TS Design Patterns] 생성 패턴 - 싱글톤 Singleton - 클래스에 인스턴스가 하나만 있도록 하면서 이 인스턴스에 대한 전역 접근 지점을 제공하는 생성 패턴 - 싱글톤 인스턴스를 가져오기 위한 공개된 정적 생성 메서드 선언 - 같은 종류의 객체가 하나만 존재하도록 하고 다른 코드의 해당 객체에 대한 단일 접근 지점 제공 - 앱 전체에서 공유 및 사용되는 단일 인스턴스 - 앱의 전역 상태를 관리하기 적합 - 인스턴스를 하나만 만들도록 강제하면 꽤 많은 메모리 공간 절약 가능 - 하지만 JS에서 안티패턴으로 간주됨 - Java, C++와 다르게 JS에서는 클래스를 작성하지 않더라도 쉽게 객체 만들 수 있음 - 테스트 코드 작성할 때도 까다로움 - React에서 전역 상태 관리를 위해 Singleton 객체를 만드는 것 대신 Redux나 Cont.. 2023. 10. 21.
[TS Design Patterns] 생성 패턴 - 프로토타입 Prototype (Clone) - 코드를 클래스에 의존시키지 않고, 기존 객체들을 복사할 수 있도록 하는 생성 패턴 - 객체를 외부에서부터 복사하는 것은 항상 가능하지 않음 (private 필드 존재 가능) - 객체를 복제하려면 해당 객체의 클래스를 알아야 하므로 의존적임 - 프로토타입 패턴은 실제로 복제되는 객체들에 복제 프로세스를 위임함 - 프로토타입이란 복제를 지원하는 객체임 - 미리 만들어진 프로토타입은 서브 클래스의 대안이 될 수 있음 - 코드를 기하학적 객체들의 클래스들에 결합하지 않고도 해당 객체들의 정확한 복사본 생성 가능 - 동일 타입의 여러 객체들이 프로퍼티를 공유함 - JS 객체의 기본 속성이고 Prototype 체인 활용 가능 - 생성자의 prototype 프로퍼티 혹은 생성된 인.. 2023. 10. 21.
[TS Design Patterns] 생성 패턴 - 빌더 Builder - Lets you construct complex objects step by step - Allows us to produce different types and representations of an object using the same construction code - Suggests that us extract the object construction code out of its own class and move it to separate objects called builders - 예시 /** * The Builder interface specifies methods for creating the different parts of * the Product objects. .. 2023. 10. 21.
[TS Design Patterns] 생성 패턴 - 팩토리 메서드 Factory Method (Virtual Constructor) - Provides an interface of creating objects in a superclass, but allows subclasses to alter the type of objects that will be created - Suggests that we replace direct object construction calls (using the new operator) with calls to a special factory method - Creator, Sub Classes - Sovles the problem of creating product objects without specifying their concret.. 2023. 10. 21.
반응형