<script type= "Text/javascript" >
The previous one was the first time I wrote an article on a blog, and this time I came again.
The last is a simple form of Factory mode, this time is the abstract version of the factory model, what is abstract, not to let people look bad understanding, but I try to use a good understanding of the way to write
This is a factory constructor, there is a property, the value of the property is an object, the object has three properties, respectively, representing three kinds of vehicles
Well, in fact, this is an abstract version of the factory constructor, like someone with you to say that you want to build a three kinds of vehicles, but how to build, he will not;functionFactory () { This. goods={motor:' Motor ', Bike:' Bike ', Car:' Car ' }}
In the factory's prototype added a method, this is an abstract method, is no concrete implementation how to build; Factory.prototype={createp:function(product) {Throw NewError ("This method should not being called directly");//If this method is called on an instance of an abstract factory, an exception is thrown}}
The speaker has no intention of the listener, and when he goes back, you want to achieve him.
functionFactory_one () {factory.apply ( This, arguments);//So you secretly borrowed his ideas}factory_one.prototype=NewFactory ();//Inherited his method here is the basic knowledge of object-oriented, what object is impersonating, the prototype chain inherits, Factory_one.prototype={constructor:factory_one,//Here to point your factory's constructor attribute to the constructor of your own factory, in fact there is a place where we miss a place, Createp:function(product) {//You have to implement his method.varp=NULL;//define an empty variable firstSwitch(product) { Case This. Goods. motor://here to understand, think, factory.apply (this,arguments), is not the abstract factory constructor This.goods This attribute is assigned to, Factory_one p=NewMotor_one ();//This class is below. Break; Case This. Goods. Bike:p=NewBike_one ();//This class is below Break; Case This. Goods. Car:p=NewCar_one ();//This class is below Break; default: P=NewMotor_one (); Break; } returnp;//Returns an instance of the vehicle you want}}
It's the same as the one above.functionFactory_two () {factory.apply ( This, arguments);} Factory_two.prototype=NewFactory (); Factory_two.prototype={constructor:factory_two, Createp:function(product) {varp=NULL; Switch(product) { Case This. Goods. Motor:p=NewMotor_two (); Break; Case This. Goods. Bike:p=NewBike_two (); Break; Case This. Goods. Car:p=NewCar_two (); Break; default: P=NewMotor_two (); Break; } returnp; }}
The following are some of the classesfunctionMotor_one () { This. type= ' Motor_one '; This. say=function() {Console.log ("I am" + This. Type); }}functionMotor_two () { This. type= ' Motor_two '; This. say=function() {Console.log ("I am" + This. Type); }}functionBike_one () { This. type= ' Bike_one '; This. say=function() {Console.log ("I am" + This. Type); }}functionBike_two () { This. type= ' Bike_two '; This. say=function() {Console.log ("I am" + This. Type); }}functionCar_one () { This. type= ' Car_one '; This. say=function() {Console.log ("I am" + This. Type); }}functionCar_two () { This. type= ' Car_two '; This. say=function() {Console.log ("I am" + This. Type); }}
First, get an example of two factoriesvarfactory=NewFactory_one ();varfactory=NewFactory_two ();
Call the factory's Createp method, and pass in the vehicle you want to create, actually there is a better way to pass the argument, in the abstract factory has not already defined the name of these vehicles?varP_one=factory.createp ("Motor");varP_two=factory.createp ("Motor");p _one.say ();p _two.say ();</script>
Abstract Factory mode for JavaScript design patterns