简单工厂模式

简单工厂模式

简单工厂模式是创建型设计模式的一种,也是最简单的额一种创建型设计模式。在讲他之前,我们先来讲讲创建型设计模式:
创建型设计模式是一类处理对象创建的设计模式,通过某种方式控制对象的创建来避免基本对象创建时可能导致设计上的问题或增加设计上的复杂度。

  • 简单工厂模式概念
    又叫静态工厂方法,由一个工厂对象决定创建某一种产品对象的实例。主要用来创建同一类对象。

  • 简单工厂模式的理解及例子
    这种模式就是说,如果给你几个类,在每次创建时还需要找到相应的类就太麻烦了,那么我们就可以把他们封装在一个函数里面,这样,我们只需记住这个函数的名字,通过这个函数就可以创建我们的对象,我们不需要关注创建的这些对象到底依赖于哪个基类,我们只需知道一个函数就可以了。下面我们来看看一个如果在购买运动设备的一个运动工厂:

      //篮球基本类
      var Basketball = function(){
      	this.intro = '篮球盛行于美国';
      }
      Basketball.prototype = {
      	getMember : function(){
      		console.log('每个队伍需要5名队员');
      	},
      	getBallsize : function(){
      		console.log('篮球很大');
          }
      }
    
      //足球基本类
      var Football = function(){
      	this.intro = '足球在世界范围类都很流行';
      }
      Football.prototype = {
      	getMember : function(){
      		console.log('每个队伍需要11名队员');
      	},
      	getBallsize : function(){
      		console.log('足球很大');
      	}
      }
    
      //网球基本类
      var Tennis = function(){
      	this.intro = '每年有很网球赛事';
      }
      Tennis.prototype = {
      	getMember : function(){
      		console.log('每个队伍需要1名队员');
      	},
      	getBallsize : function(){
      		console.log('网球很小');
      	}
      }
    
      //运动工厂
      var sportsFactory = function(name){
      	switch(name){
      		case 'NBA':
      		    return new Baskketball();
      		case 'wordUp':
      		    return new Football();
      		case 'FrenchOpen':
                  return new Tennis();
    
      	}
      }
    
      var footnall = sportsFactory('wordUp');
      console.log(footnall);
    

这样,我们就只需要记住sportsFactorty这个工厂对象就行了
还有当我们在编写登陆模板的代码时也时常也会用到简单工厂模型:

    //警示框
    var loginAlert = function(text){
    	this.content = text;
    }
    loginAlert.prototype.show = function(){
    	alert(this.content);
    }
    //确认框
    var loginConfirm = function(text){
    	this.content = text;
    }
    loginConfirm.prototype.show = function(){
    	alert(this.content);
    }
    //提示框
    var loginPrompt = function(text){
    	this.content = text;
    }
    loginPrompt.prototype.show = function(){
    	alert(this.content);
    }

    //工厂函数
    var popFactory = function(name){
    	switch(name){
    		case 'alert':
    		    return new loginAlert('请输入用户名');
    		case 'confirm':
    		    return new loginConfirm('请确认');
    		case 'prompt':
    		    return new loginPrompt('请取消');
    	}
    }
    var userAlert = new popFactory('alert');
    console.log(userAlert.content);

我们还可以把不同各类里面相同的部分提取出来,如这里的content是一个相同的属性,所以我们可以这样:

    //警示框
    var loginAlert = function(text){
    	this.content = text;
    }
    loginAlert.prototype.show = function(){
    	alert(this.content);
    }
    //确认框
    var loginConfirm = function(text){
    	this.content = text;
    }
    loginConfirm.prototype.show = function(){
    	alert(this.content);
    }
    //提示框
    var loginPrompt = function(text){
    	this.content = text;
    }
    loginPrompt.prototype.show = function(){
    	alert(this.content);
    }

    //工厂函数
    var creatPop = function(type,text){
    	//创建一个对戏,并对对象拓展和方法
    	var o = new Object();
    	o.content = text;
    	o.show = function(){
    		//显示方法
    		alert(this.content);
    	};
    	if(type == 'alert'){
    		//警示框差异
    		return new loginAlert(text);
    	}
    	if(type == 'prompt'){
    		//确认框差异
    		return new loginConfirm(text);
    	}
    	if(type == 'confirm'){
    		//提示框差异
    		return new loginPrompt(text);
    	}
    }

    var userAlert = new creatPop('prompt', '请确认');
    console.log(userAlert);

工厂模式就是先抽象出几个类的相同点,在把给这几个类创建对象实例的代码封装到一个工厂函数里面,我们要使用或是重用的时候就会很方便,代码的维护性也会提高。

原文地址:https://www.cnblogs.com/yehui-mmd/p/6496209.html