工場モデルの三抽象工場モデル

3221 ワード

抽象ファクトリモード:実際のオブジェクトインスタンスではなく、クラスクラスタを作成し、クラスの構造をカスタマイズします.
クラスのファクトリを抽象化することで、クラスのインスタンスを作成するのではなく、製品クラスタの作成にビジネスを使用します.
JavaScriptは他の言語(javaなど)のように抽象クラスを作成することはできませんが、抽象クラスのメソッドでエラーを投げ出して抽象クラスをパターン化することができます.
シミュレーション抽象クラス
製品クラスタを指定し、いくつかの必須メソッドを宣言し、サブクラスが書き換えられていない場合はエラーを放出します.
const Car = function(){}
Car.prototype = {
    getPrice(){
        return new Error('        ')
    },
    getSpeed(){
        return new Error('        ')
    }
}

抽象ファクトリモード
抽象クラスを親として作成するサブクラス
  • 抽象工場方法
  • const VehicleFactory = function(subType,superType){
        //             
        if(typeof VehicleFactory[superType] === 'function'){
            //   
            function F(){}
            //         
            F.prototype = new VehicleFactory[superType]();
            //    constructor    
            superType.constructor = subType
            //      ‘  ’
            subType.prototype = new F()
        }else{
            //           
            throw new Error('       ')
        }
    }
    
  • 乗用車抽象類
  • VehicleFactory.Car = function(){
        this.type = 'car'
    }
    VehicleFactory.Car.prototype = {
        getPrice(){
            return new Error('        ')
        },
        getSpeed(){
            return new Error('        ')
        }
    }
    
  • バス抽象類
  • VehicleFactory.Bus = function(){
        this.type = 'bus'
    }
    VehicleFactory.Bus.prototype = {
        getPrice(){
            return new Error('        ')
        },
        getPassenerNum(){
            return new Error('        ')
        }
    }
    
  • 貨車抽象類
  • VehicleFactory.Truck = function(){
        this.type = 'truck'
    }
    VehicleFactory.Bus.prototype = {
        getPrice(){
            return new Error('        ')
        },
        getTrainload(){
            return new Error('        ')
        }
    }
    

    抽象ファクトリは実はサブクラス継承(寄生式継承)の親を実現する方法です
    抽象ファクトリでは、子クラスの作成(この例では、子クラスに親クラスを継承させ、子クラスを拡張する)を使用します.
    プロダクトサブクラスに対応するプロダクトクラスタ抽象クラスを継承させる
  • BMW自動車サブクラス
  • const BMW = function(price,speed){
        this.price = price
        this.speed =speed
    }
    //       Car      
    VehicleFactory(BMW,'Car')
    BMW.prototype.getPrice = function(){
        return this.price
    }
    BMW.prototype.getSpeed = function(){
        return this.speed
    }
    
  • ビアディバスサブクラス
  • const BYD = function(price,passenger){
        this.price = price
        this.passenger = passenger
    }
    //       Bus      
    VehicleFactory(BYD,'Bus')
    VehicleFactory.prototype.getPrice = function(){
        return this.price
    }
    VehicleFactory.prototype.getPassenerNum = function(){
        return this.passenger
    }
    
  • ベンツ自動車サブクラス
  • const BenzTruck = function(price,trainload) {
        this.price = price
        this.trainload = trainload
    }
    //       Bus      
    VehicleFactory(BenzTruck,'Truck')
    VehicleFactory.prototype.getPrice = function(){
        return this.price
    }
    VehicleFactory.prototype.getTrainload = function(){
        return this.trainload
    }
    

    抽象ファクトリにより,各サブクラスがどのカテゴリに属するかを知ることができ,そのカテゴリに必要な属性と方法も備える.
    const truck  = new BenzTruck(100000,1000)
    console.log(truck.getPrice())
    console.log(truck.type)