본문 바로가기
TIL

추상화 팩토리 패턴

by 은지:) 2024. 2. 6.
728x90
반응형

추상화 팩토리 패턴

 

 

팩토리 패턴의 단점을 보완하고자 나온 거 같음

 

 

팩토리 패턴

 

// 각 차종의 클래스
class Sedan {
  configure() {
    this.seats = 4;
    this.engine = 'V6';
  }

  displayInfo() {
    console.log(`Sedan with ${this.seats} seats and ${this.engine} engine.`);
  }
}

class SUV {
  configure() {
    this.seats = 7;
    this.engine = 'V8';
  }

  displayInfo() {
    console.log(`SUV with ${this.seats} seats and ${this.engine} engine.`);
  }
}

// 팩토리 클래스
class CarFactory {
  createCar(type) {
    let car;

    // 생성된 차종에 따라 다양한 설정
    if (type === 'Sedan') {
      car = new Sedan();
    } else if (type === 'SUV') {
      car = new SUV();
    }

    car.configure(); // 각 차종의 설정 메서드 호출
    return car;
  }
}

// 팩토리 인스턴스 생성
const carFactory = new CarFactory();

// Sedan과 SUV 생성 및 정보 출력
const sedan = carFactory.createCar('Sedan');
const suv = carFactory.createCar('SUV');

sedan.displayInfo(); // Sedan with 4 seats and V6 engine.
suv.displayInfo();   // SUV with 7 seats and V8 engine.

 

개별 객체의 생성을 담당하는 단일 팩토리 클래스를 제공하는 패턴

특정 클래스의 인스턴스를 생성하고 반환하는 책임을 가지는 팩토리를 제공함

 

 

=> 객체를 만드는 팩토리 클래스를 가지고 있음

class CarFactory {
  createCar(type) {
    let car;
    if (type === 'Sedan') {
      car = new Sedan();
    } else if (type === 'SUV') {
      car = new SUV();
    }
    return car;
  }
}

 

이 예제에서 CarFactory는 다양한 차종을 생성하면서 각 차종에 대한 설정 가능함

이렇게 하면 각 차종이 다양한 속성과 특성을 가지도록 할 수 있으며

팩토리를 통해 일관된 방식으로 차량을 생성하고 설정함

 

 

 

 

추상 팩토리 패턴

 

// 추상 팩토리 클래스
class CarFactory {
  createCar(type) {
    let car;
    
    // 생성된 차종에 따라 다양한 설정
    if (type === 'Sedan') {
      car = new Sedan();
      car.configure({ seats: 4, engine: 'V6' });
    } else if (type === 'SUV') {
      car = new SUV();
      car.configure({ seats: 7, engine: 'V8' });
    }

    return car;
  }
}

// 각 차종의 클래스
class Sedan {
  configure(options) {
    this.seats = options.seats;
    this.engine = options.engine;
  }

  displayInfo() {
    console.log(`Sedan with ${this.seats} seats and ${this.engine} engine.`);
  }
}

class SUV {
  configure(options) {
    this.seats = options.seats;
    this.engine = options.engine;
  }

  displayInfo() {
    console.log(`SUV with ${this.seats} seats and ${this.engine} engine.`);
  }
}

// 팩토리 인스턴스 생성
const carFactory = new CarFactory();

// Sedan과 SUV 생성 및 정보 출력
const sedan = carFactory.createCar('Sedan');
const suv = carFactory.createCar('SUV');

sedan.displayInfo(); // Sedan with 4 seats and V6 engine.
suv.displayInfo();   // SUV with 7 seats and V8 engine.

 

여러 관련 객체의 생성을 캡슐화하는 패턴으로, 객체의 패밀리를 생성하도록 하는 추상화된 인터페이스를 제공

팩토리 클래스는 특정 객체 패밀리를 생성하는 책임

 

 

위의 코드에서 팩토리 패턴을 사용하여 CarFactory 클래스가 차종을 생성하고 설정하는 역할

각 차종 클래스(Sedan 및 SUV)에서는 자신의 설정 메서드(configure)를 호출 => 객체의 속성을 설정

객체 생성과 설정을 분리한 형태

 

class CarFactory {
  createCar(type) {
    let car;
    
    // 생성된 차종에 따라 다양한 설정
    if (type === 'Sedan') {
      car = new Sedan();
      car.configure({ seats: 4, engine: 'V6' });
    } else if (type === 'SUV') {
      car = new SUV();
      car.configure({ seats: 7, engine: 'V8' });
    }

    return car;
  }
}

 

역할이 다름

각 설정을 CarFactory에서 정함

 

 

 

차량 엔진, 타이어, 인테리어 등을 생성하는 팩토리를 통해 전체 차량을 생성하는 추상 팩토리 패턴의 예

 

다른 예시

 

// 추상 팩토리 인터페이스
class CarAbstractFactory {
  createEngine() {}
  createTire() {}
  createInterior() {}
}

// 구체적인 제품 클래스들
class LuxuryEngine {
  // LuxuryEngine의 구현
}

class LuxuryTire {
  // LuxuryTire의 구현
}

class LuxuryInterior {
  // LuxuryInterior의 구현
}

// 구체적인 팩토리 클래스
class LuxuryCarFactory extends CarAbstractFactory {
  createEngine() {
    return new LuxuryEngine();
  }

  createTire() {
    return new LuxuryTire();
  }

  createInterior() {
    return new LuxuryInterior();
  }
}

// 다른 구체적인 팩토리 클래스
class EconomyCarFactory extends CarAbstractFactory {
  createEngine() {
    return new EconomyEngine();
  }

  createTire() {
    return new EconomyTire();
  }

  createInterior() {
    return new EconomyInterior();
  }
}

// 다른 제품 클래스들
class EconomyEngine {
  // EconomyEngine의 구현
}

class EconomyTire {
  // EconomyTire의 구현
}

class EconomyInterior {
  // EconomyInterior의 구현
}

 

팩토리 패턴은 객체를 만들어주는 class가 있고 이걸 통해서 객체를 만드는 거 같음 (단일)

추상화 패턴은 이런 단일 객체를 추상화 시키는 과정을 한번 더 거치는 거 같음 

 

728x90
반응형

'TIL' 카테고리의 다른 글

리액트 - 순수 함수  (0) 2024.03.05
Facade Pattern  (0) 2024.03.05
호이스팅 관련 const, let, var 에러 정리  (0) 2024.02.05
팩토리 패턴  (0) 2024.02.05
얕은 복사 깊은 복사 다시보기  (0) 2024.01.28

댓글