前端两道设计模式经典面试题🔥

458 阅读2分钟

image.png

第一题

打车面试题如下条件:

  • 打车时可以打专车或者快车,任何车都有车牌号和名称。
  • 不同车价格不同,快车每公里1元,专车每公里2元。
  • 行程开始时,显示车辆信息。
  • 行程结束时,显示打车金额(假定行程就5公里)
// 抽象车类
class Car {
  constructor(name, number) {
    this.name = name;
    this.number = number;
  }
}
// 行程类
class Trip {
  constructor(car) {
    this.car = car;
  }
  start() {
    console.log(`行程开始, 车辆:${this.car.name},车牌号:${this.car.number}。`);
  }
  end () {
    console.log(`行程结束, 车辆:${this.car.name},打车金额:${this.car.money * 5}}。`);
  }
}
// 具体类 快车
class FastCar extends Car{
  constructor(name, number) { 
    super(name, number);
    this.money = 2;
  }
} 
// 具体类 专车
class SpecialCar extends Car{
  constructor(name, number) {
    super(name, number);
    this.money = 1;
  }
}
// 实例化
const newCar = new SpecialCar('专车','110');
const newTrip = new Trip(newCar);
newTrip.start();
newTrip.end();

第二题

打车面试题如下条件:

  • 某停车场,分3层,每层100车位
  • 每个车位都能监控到车辆的驶入和离开
  • 车辆进入前,显示每层的空余车位数量
  • 车辆进入时,摄像头可识别车牌号和时间
  • 车辆出来时,出口显示器显示车牌号和停车时长
// 车
class Car {
  constructor(number) {
    this.number = number;
  }
}
// 摄像头
class Camera {
  shot(car) {
    return {
      number: car.number,
      inTime: Date.now()
    }
  }
}
class Screen {
  show(car, inTime) {
    console.log(`车牌号:${car.number}`);
    console.log(`停车时长:${(Date.now() - inTime) / 1000}秒`);
  }
}
// 车位
class Place {
  constructor() {
    this.empty = true;
  }
  in() {
    this.empty = false;
  }
  out() {
    this.empty = true;
  }
}
// 层
class Floor {
  constructor(index, places) {
    this.index = index;
    this.places = places || [];
  }
  getEmptyPlaces() {
    let count = 0;
    this.places.forEach(place => {
      if (place.empty) count++;
    });
    return count;
  }
}
// 停车场
class Park {
  constructor(floors) {
    // 摄像机
    this.camera = new Camera();
    // 显示屏
    this.screen = new Screen();
    // 所有停车位
    this.floors = floors || [];
    // 停车场层数
    this.floorsLen = floors.length || 0;
    // 用来存储停进来的车辆
    this.carMap = {};
  }
  // 进入前
  beforeIn() {
    let allFloorCount = 0;
    let res = '';
    this.floors.forEach(floor => {
      const currentCount = floor.getEmptyPlaces();
      res += `当前层剩余停车位: ${currentCount}个 \n`;
      allFloorCount += currentCount;
    });
    return res += `停车场总剩余停车位: ${allFloorCount} 个`;
  }
  // 进入
  in(car) {
    let info = this.camera.shot(car);
    // // 随机层
    // const randomFloor = parseInt(Math.random() * this.floorsLen) + 1;
    // 随机车位位置
    const randomPlace = parseInt(Math.random() * 100);
    // 涉及停满问题 暂不深入
    const place = this.floors[0].places[randomPlace];
    // 进入停车位
    place.in();
    info.place = place;
    // 存储
    this.carMap[info.number] = info;
  }
  // 出来
  out(car) {
    const info = this.carMap[car.number];
    // 出停车位
    info.place.out();
    // 屏幕展示信息
    this.screen.show(info, info.inTime);
    // 删除存储
    Reflect.deleteProperty(this.carMap, car.number);
  }
}

// 初始化停车场
const floors = [];
for (let i = 0; i < 3; i++) {
  const places = [];
  for (let j = 0; j < 100; j++) {
    places[j] = new Place();
  }
  floors[i] = new Floor(i + 1, places);
}
const park = new Park(floors);

// 初始化车辆
const car1 = new Car(100);
const car2 = new Car(200);
const car3 = new Car(300);

console.log('第一辆车进入');
console.log(park.beforeIn());
park.in(car1);
console.log('第二辆车进入');
console.log(park.beforeIn());
park.in(car2);
console.log('第一辆车离开');
park.out(car1);
console.log('第二辆车离开');
park.out(car2);

console.log('第三辆车进入');
console.log(park.beforeIn());
park.in(car3);
console.log('第三辆车离开');
park.out(car3);