Simple Factory model

The simple factory pattern, also known as the static factory method, lets a factory object decide to create an instance of a product object class. Look at the situation directly: for example, a store has basketball, soccer ball, tennis ball. When a customer goes to buy a ball, he just needs to tell the clerk, I want a basketball. It will give you a basketball.

// Basketball base class
var Basketball = function() {
  this.intro = 'This is USA basketball.'
}
Basketball.prototype = {
  getMember: function() {
    console.log('Five players per team');
  },
  getBallSize: function() {
    console.log('Basketball is big'); }}// Soccer base class
var Football = function() {
  this.intro = 'Football is popular all over the world'
}
Basketball.prototype = {
  getMember: function() {
    console.log('Eleven players for each team');
  },
  getBallSize: function() {
    console.log('The football is big'); }}// Basketball base class
var Tennis = function() {
  this.intro = 'There are many tennis series every year'
}
Basketball.prototype = {
  getMember: function() {
    console.log('One man per team');
  },
  getBallSize: function() {
    console.log('Tennis balls are small'); }}// Sports factory
var SportsFactory = function(name) {
  switch(name) {
    case 'BasketBall': 
        return new Basketball()
    case 'FootBall':
        return new Football()
    case 'Tennis': 
        return new Tennis()
  }
}

var football = SportsFactory('FootBall')
console.log(football);
console.log(football.intro);
console.log(football.getMember);
Copy the code

This way, we can just type in what we want into the SportFactory factory function, and we don’t have to worry about which base class it comes from, saving us a lot of time.

On the other hand, one object can sometimes take the place of many classes and the idea of the simple factory pattern is to create objects that are instantiated by different classes, but in addition to that, the simple factory pattern can also be used to create similar objects. These created classes and objects are all similar. Image understanding factory model, is a factory made, the form is similar. Next, take a look at how the simple factory pattern creates similar objects.

function createBook(name, time, type) {
  var book = new Object()
  book.name = name
  book.time = time
  book.type = type
  book.getName = function() {
    console.log(this.name);
  }
  // Return the object
  return book
}

var book1 = createBook('js book'.2016.'js')
var book2 = createBook('css book'.2011.'css')
book1.getName()
book2.getName()
Copy the code



Isn’t that crazy? Isn’t that the code I write every day, right there, right there, that’s factory mode? Yes, that’s the factory model.

The difference between

The first is created by instantiating an object with a class, and the second is achieved by creating a new object and then wrapping it to enhance its properties and functionality classes. The difference between them also makes it possible to share the methods on the superclass prototype of objects created from classes that inherit from the same parent class. The object created by the later parasitic method is a new individual, so their methods cannot be shared. Of course, which factory model you choose to implement your requirements depends on the specific business requirements.