spaasteam / spaas-daily-practice

spaas团队的每日一练,欢迎小伙伴们提交踊跃答案!
4 stars 2 forks source link

第五题 要求设计 LazyMan 类,实现以下功能 #7

Open cjfff opened 5 years ago

cjfff commented 5 years ago
LazyMan('Tony');
// Hi I am Tony

LazyMan('Tony').sleep(10).eat('lunch');
// Hi I am Tony
// 等待了10秒...
// I am eating lunch

LazyMan('Tony').eat('lunch').sleep(10).eat('dinner');
// Hi I am Tony
// I am eating lunch
// 等待了10秒...
// I am eating diner

LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food');
// Hi I am Tony
// 等待了5秒...
// I am eating lunch
// I am eating dinner
// 等待了10秒...
// I am eating junk food
cjfff commented 5 years ago
const LazyMan = (function() {
  class LazyMan {

    constructor(name) {
      this.name = name;
      this.tasks = [() => {
        console.log(` Hi I am ${name}`);
      }]
    }

    static of(name) {
      return new LazyMan(name)
    }

    static generaterSleepPromise(time) {
      return () => new Promise((resolve) => {
        setTimeout(() => {
          console.log(`等待了${time}秒`);
          resolve()
        }, time * 1000)
      })
    }

    sleep(time) {
      this.tasks.push(LazyMan.generaterSleepPromise(time))
      return this
    }

    sleepFirst(time) {
      this.tasks.splice(this.tasks.length - 2, 0, LazyMan.generaterSleepPromise(time))
      return this
    }

    eat(food) {
      this.tasks.push(() => {
        console.log(`I am eating ${food}`)
      })
      return this
    }

    async run() {
      for (const fn of this.tasks) {
        await fn()
      }
    }
  }
  return (name)  => new LazyMan(name)
})()

LazyMan('Tony').run();

LazyMan('Tony').sleep(10).eat('lunch').run();

LazyMan('Tony').eat('lunch').sleep(10).eat('dinner').run();

LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food').run();
linrunzheng commented 5 years ago
class LazyMan {
  constructor(name) {
    this.middleFn = [];
    console.log(`Hi I am ${name}`);
  }
  wait(time) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve();
      }, time);
    });
  }
  sleepFirst(time) {
    this.middleFn.unshift(() => {
      return this.wait(time);
    });
    return this;
  }
  sleep(time) {
    this.middleFn.push(() => {
      return this.wait(time);
    });
    return this;
  }
  eat(food) {
    this.middleFn.push(() => {
      console.log(`I am eating ${food}`);
    });
    return this;
  }
  async run() {
    for (const fn of this.middleFn) {
      await fn();
    }
    this.middleFn.length = 0;
  }
}

new LazyMan("Tony")
  .eat("lunch")
  .eat("dinner")
  .sleepFirst(5000)
  .sleep(10000)
  .eat("junk food")
  .run();
Barretem commented 5 years ago
const LazyMan = (function() {
  class LazyMan {
    constructor(name) {
      this.name = name;
      this.sleepFirstTime = 0;
      this.sleepTime = 0;
      this.consoleList = [];
      this.cutDownTime = 0;
      console.log(`Hi I am ${name}`);
      setTimeout(() => {
        setTimeout(() => {
          this.consoleList.forEach(info => {
            if (info.sleepTime) {
             // TODO 需要优化,否则死机的
              setTimeout(
                () => {
                  console.log(`I am eating ${info.foot}`);
                },
                info.sleepTime * 1000,
                info,
              );
            } else {
              console.log(`I am eating ${info.foot}`);
            }
          });
        }, this.sleepFirstTime * 1000);
      }, 0);
    }

    sleep(time) {
      this.sleepTime = time;
      return this;
    }

    sleepFirst(time) {
      this.sleepFirstTime = time;
      return this;
    }

    eat(foot) {
      // 将对应的事件push到数组中去
      this.consoleList.push({
        sleepTime: this.sleepTime,
        foot,
      });
      return this;
    }
  }
  return name => new LazyMan(name);
})();

LazyMan('Tony')
  .eat('lunch')
  .eat('dinner')
  .sleepFirst(5)
  .sleep(10)
  .eat('junk food');
Htongbing commented 5 years ago
class LazyMan {
  constructor(name) {
    this.task = []
    this.timer = setTimeout(() => {
      this.run()
    }, 0)
    console.log(`Hi I am ${name}`)
  }
  sleep(second) {
    const fn = () => {
      setTimeout(() => {
        this.run()
      }, second * 1000)
    }
    this.task.push(fn)
    return this
  }
  eat(food) {
    const fn = () => {
      console.log(`I am eating ${food}`)
      this.run()
    }
    this.task.push(fn)
    return this
  }
  sleepFirst(second) {
    const fn = () => {
      setTimeout(() => {
        this.run()
      }, second * 1000)
    }
    this.task.unshift(fn)
    return this
  }
  run() {
    const first = this.task.shift()
    first && first()
  }
}

new LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food')
qiaoqiao10001 commented 5 years ago
class LazyMan {
  constructor(name) {
    this.middleFn = [];
    console.log(`Hi I am ${name}`);
  }
  wait(time) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve();
      }, time);
    });
  }
  sleepFirst(time) {
    this.middleFn.unshift(() => {
      return this.wait(time);
    });
    return this;
  }
  sleep(time) {
    this.middleFn.push(() => {
      return this.wait(time);
    });
    return this;
  }
  eat(food) {
    this.middleFn.push(() => {
      console.log(`I am eating ${food}`);
    });
    return this;
  }
  async run() {
    for (const fn of this.middleFn) {
      await fn();
    }
    this.middleFn.length = 0;
  }
}

new LazyMan("Tony")
  .eat("lunch")
  .eat("dinner")
  .sleepFirst(5000)
  .sleep(10000)
  .eat("junk food")
  .run();

执行javascript new LazyMan('Tony').eat('lunch').sleep(10).eat('dinner'); 的时候只打印hi i amtony image