Simple factory pattern and factory method pattern
Copyright notice: This article is originally published BY CSDN blogger “Cheng Ke ‘ai”. It follows CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement.
Original link blog.csdn.net/qq_33479841…
1. Simple Factory
The simple Factory pattern, also known as the Static Factory Method, consists of a Factory object that decides to create instances of a class of products. If there is a requirement to display the number of participants and some related information according to the input name of the ball game, using the simple factory mode, it is roughly as shown in the following picture:
In a pattern that doesn’t use simple factories, I need to have three base classes, which means I need to remember the names of the three classes and make them present the information I want to know when I call their functions. Such as we need to know the number of Basketball team needs, can call its getMem method, shows the information we need, the premise is that we remember also the class, the drawback is that when the ball games too much, can’t remember the name of each ball accurately, then we can take these classes encapsulate function in a factory, Pass in a different ball name as a parameter, you can return the corresponding ball object, we instantiate the object, we can call its corresponding method. Specific code implementation can refer to the following:
let Basketball = function () {
this.info='basketball';
}
Basketball.prototype = {
getMem : function(){
console.log('Five people per team')}}let Football = function () {
this.info='football';
}
Football.prototype = {
getMem : function(){
console.log('Each team needs 11 people.')}}let PingPong= function () {
this.info='table tennis';
}
PingPong.prototype = {
getMem : function(){
console.log('1 person per team')}}Copy the code
The above are the base classes of all kinds of balls. When I want to use the corresponding class, I can call the corresponding class or re-instantiate one. However, if there is a certain number of base classes and their internal forms are similar, we can consider packaging them into a factory class:
let BallFactory = function (name) {
switch (name) {
case 'basketball':
return new Basketball();
case 'football':
return new Football();
case 'pingpong':
return newPingPong(); }}Copy the code
So when we use the BallFactory, just remember the factory object BallFactory, and it will get us the information about the ball.
Advantages of simple factory: the corresponding product instance can be called according to different incoming parameters, which is convenient and logical to use.
Disadvantages of simple factory: the coupling degree is high, once the factory class has problems, the use of other base classes will be affected, more code needs to be changed when the base class is added, and the factory class will be bloated and complex when there are too many products. 支那
2. Factory Method
In the simple factory pattern described above, when requirements change, not only do classes need to be added, but factory functions need to be modified, and the factory method pattern postpones the actual object work to subclasses. The advantage of this is that when adding classes, you only need to add them in the factory class prototype. See the following code for concrete implementation:
/ / the factory class
let Factory = function(name,info){
if(this instanceof Factory){
let str = new this[name](info)
rerurn str;
}else{
return new Factory(name,info)
}
}
// The factory prototype sets the base class for creating the object
Factory.prototype = {
Basketball:function(info){
this.info = info;
}
Football:function(info){
this.info = info;
}
PingPongBall:function(info){
this.info = info; }}// Add data
let data=[
{name:"Basketball".info:"Basketball is very interesting"},
{name:"Football".info:"Football is very creazy"}
{name:"PingPongBall".info:"PingPongBall is very difficult"}
{name:"PingPongBall".info:"Chinese PingPongBall is very outstanding"}]Copy the code
We used the safe mode to create the Factory object above. When we need to add the base class, we only need to add it once in the factory. prototype method instead of adding it at least twice in the simple Factory mode.
Factory mode advantage: only add in the factory method in the prototype can be called, avoid multiple add, reduce the coupling degree.
Disadvantages of factory pattern: When adding new products, new product and factory classes need to be written, which increases function complexity and system overhead.