抽象工厂模式

Abstract Factory

概述
不直接生成实例,而是创建不同的类
每个类可以创建自己的方法
实际前端开发中,一个简单工厂模式就够用了。哈哈
参考代码
Abstract Factory
class User {
  constructor(name, role, pages) {
    this.name = name
    this.role = role
    this.pages = pages
  }
  greeting() {
    console.log('hello,' + this.name);
  }
  dataShow() {
    console.log('data');
  }
}

class SuperAdmin extends User {
  constructor(name) {
    super(name, 'SuperAdmin', ['home', 'news', 'user', 'right'])
  }
  dataShow() {
    console.log('your datashow');
  }
  addUser() {
    console.log('you can add user');
  }
  addRight() {
    console.log('you can add right');
  }
}

class Admin extends User {
  constructor(name) {
    super(name, 'Admin', ['home', 'news', 'user'])
  }
  dataShow() {
    console.log('your datashow');
  }
  addUser() {
    console.log('you can add user');
  }
}

class Editor extends User {
  constructor(name) {
    super(name, 'Editor', ['home', 'news'])
  }
  dataShow() {
    console.log('your datashow');
  }
}

function AbstractFactory(role) {
  switch (role) {
    case 'SuperAdmin':
      return SuperAdmin;
      break;
    case 'Admin':
      return Admin;
      break;
    case 'Editor':
      return Editor;
      break;
    default:
      return new Error('Wrong Para')
      break;
  }
}          
Usage
let fac = AbstractFactory('SuperAdmin')
let user = new fac('glin')