Abstract Factory mode, which is a kind of creation pattern, is actually a method of implementing subclass inheriting parent class, in this method, by passing subclasses and inheriting the name of the parent class, and adding a judgment to the existence of the abstract class in the abstract factory method, if present, the subclass inherits the method of the parent class. The subclass then inherits through a parasitic type.
Abstract Factory is used to create subclasses, itself can be regarded as a class family, it has developed the structure of subclasses, let subclasses inherit abstract factory methods and properties, so the abstract Factory mode can create multi-class objects.
Example: Defining an abstract Factory method:
varVehiclefactory =function(Subfun, subclass) {if(typeofVehiclefactory[subclass] = = = ' function ') { functionF () {} F.prototype=NewVehiclefactory[subclass] () subfun.constructor=Subfun Subfun.prototype=NewF () Console.log (Subfun.prototype)}Else { Throw NewError (' class object not created ') }}//Car abstract classVehiclefactory.cars =function () { This. Type = ' car '}vehiclefactory.cars.prototype={getprice:function () { return NewError (' abstract method cannot be called, please customize subclass method ') }}
To generate subclasses with an abstract factory:
var function (Price) { the. Price =' Cars 'function () { Returnthis. Price;} var New Jili (200000); Console.log (Jili.getprice ()); // 200000
JavaScript design mode-Abstract Factory mode