不暴露创建对象的具体逻辑, 由子类决定实例化哪一类的接口(函数)
    用途: 代替new实例化对象

    1. // 产品类
    2. class Product {
    3. deliver () {
    4. console.log('运输')
    5. }
    6. }
    7. class Truck extends Product {
    8. deliver () {
    9. console.log('陆运')
    10. }
    11. }
    12. class Ship extends Product {
    13. deliver () {
    14. console.log('海运')
    15. }
    16. }
    17. // 工厂类
    18. class Factory {
    19. }
    20. class CreateTruck extends Factory {
    21. create () {
    22. return new Truck();
    23. }
    24. }
    25. class CreateShip extends Factory {
    26. create () {
    27. return new Ship();
    28. }
    29. }
    30. let t = new CreateTruck();
    31. t.create().deliver();
    32. let s = new CreateShip();
    33. s.create().deliver();
    34. // 陆运
    35. // 海运