如何使用javascript中的解釋器模式-創(chuàng)新互聯(lián)

這篇文章主要講解了如何使用javascript中的解釋器模式,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比花溪網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式花溪網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋花溪地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。

介紹:之前在做java開發(fā)時(shí),數(shù)據(jù)庫的增刪改查特別頻繁,并且場景不同需要用到的SQL語句頁都不同,如何用調(diào)用方法的形式來使用sql語句,拼接sql?這就是這一節(jié)我們要講的解釋器模式。

定義:定義一個(gè)語言的文法,并且建立一個(gè)解釋器來解釋該語言中的句子,這里的語言是指使用規(guī)定格式和語法的代碼。解釋器模式是一種類行為型模式。

場景:我們實(shí)現(xiàn)一個(gè)解釋器,用來判斷傳遞的數(shù)字是奇數(shù)還是偶數(shù),是正數(shù)還是負(fù)數(shù),是正奇數(shù)還是負(fù)奇數(shù)。

示例:

var TerminalExpression = function(data){
  this.data = data;
 
  this.interpret = function(context){
    if(context === this.data){
      return true;
    }
    return false;
  }
}
 
var OrExpression = function(exprArr){
  this.exprArr = exprArr;
 
  this.interpret = function(context){
    var isMatch = false;
    this.exprArr.map(function(item){
      if(item.interpret(context)){
        isMatch = true;
      }
    })
    return isMatch;
  }
}
 
var AndExpression = function(exprArr){
  this.exprArr = exprArr;
 
  this.interpret = function(context){
    var isMatch = true;
    this.exprArr.map(function(item){
      if(!item.interpret(context)){
        isMatch = false;
   }
    })
    return isMatch;
  }
}
 
function getEvenExpression(){
  var ExpressionList = [];
  for(var i = -10; i<100; i++){
    if(i % 2 == 0){
      ExpressionList.push(new TerminalExpression(i));
    }
  }
  return new OrExpression(ExpressionList);
}
 
function getOddExpression(){
  var ExpressionList = [];
  for(var i = -10; i<100; i++){
    if(i % 2 != 0){
      ExpressionList.push(new TerminalExpression(i));
    }
  }
  return new OrExpression(ExpressionList);
}
 
function getNegativeOddExpression(){
  var ExpressionList = [];
  for(var i = -10; i<100; i++){
    if(i < 0){
      ExpressionList.push(new TerminalExpression(i));
    }
  }
  return new OrExpression(ExpressionList);
}
 
var isEven = getEvenExpression();
var isOdd = getOddExpression();
var isNegative = getNegativeOddExpression();
var isNegativeAndOdd = new AndExpression([isNegative,isOdd]);
 
console.log('2是偶數(shù)嗎? ' + isEven.interpret(2));//2是偶數(shù)嗎? true
console.log('3是偶數(shù)嗎? ' + isEven.interpret(3));//3是偶數(shù)嗎? false
console.log('3是奇數(shù)嗎? ' + isOdd.interpret(3));//3是奇數(shù)嗎? true
console.log('3是負(fù)數(shù)嗎? ' + isNegative.interpret(3));//3是負(fù)數(shù)嗎? false
console.log('-3是負(fù)數(shù)嗎? ' + isNegative.interpret(-3));//-3是負(fù)數(shù)嗎? true
console.log('-3是負(fù)奇數(shù)嗎? ' + isNegativeAndOdd.interpret(-3));//-3是負(fù)奇數(shù)嗎? true
console.log('-4是負(fù)奇數(shù)嗎? ' + isNegativeAndOdd.interpret(-4));//-4是負(fù)奇數(shù)嗎? false
console.log('3是負(fù)奇數(shù)嗎? ' + isNegativeAndOdd.interpret(3));//3是負(fù)奇數(shù)嗎? false 

文章標(biāo)題:如何使用javascript中的解釋器模式-創(chuàng)新互聯(lián)
文章路徑:http://muchs.cn/article30/cocjpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站維護(hù)、網(wǎng)站營銷、做網(wǎng)站、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)