Created
April 16, 2011 21:27
-
-
Save mrdavidlaing/923516 to your computer and use it in GitHub Desktop.
A simple example of the "switch" code smell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CreditCard(http) { | |
this.process = function(cardType, cardNumber) { | |
var cardProcessor = { | |
"VISA": this.processVisa, | |
"MASTERCARD": this.processMasterCard, | |
"AMEX": this.processAmex | |
}; | |
cardProcessor[cardType](cardNumber); | |
}; | |
this.processVisa = function(cardNumber) { | |
var trimmedCardNumber=cardNumber.replace(/\s/g,''); | |
if (trimmedCardNumber.length!==16) { | |
throw new Error("Invalid card number"); | |
} | |
if(trimmedCardNumber[0]!=='4') { | |
throw new Error("Invalid card number"); | |
} | |
http.POST('http://visa.com', { "cardNumber": cardNumber}); | |
}; | |
this.processMasterCard = function(cardNumber) { | |
var trimmedCardNumber=cardNumber.replace(/\s/g,''); | |
if (trimmedCardNumber.length!==16) { | |
throw new Error("Invalid card number"); | |
} | |
if(trimmedCardNumber[0]!=='5') { | |
throw new Error("Invalid card number"); | |
} | |
http.POST('http://mastercard.com', { "cardNumber": cardNumber}); | |
}; | |
this.processAmex = function(cardNumber) { | |
var trimmedCardNumber=cardNumber.replace(/\s/g,''); | |
if (trimmedCardNumber.length!==15) { | |
throw new Error("Invalid card number"); | |
} | |
if(trimmedCardNumber[0]!=='3') { | |
throw new Error("Invalid card number"); | |
} | |
http.POST('http://amex.com', { "cardNumber": cardNumber}); | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CreditCardStrategy(http) { | |
this.http = http; | |
this.process = function(cardType, cardNumber) { | |
cardProcessor = { | |
"VISA": new VisaCreditCardProcessor(this.http), | |
"MASTERCARD": new MasterCardCreditCardProcessor(this.http), | |
"AMEX": new AmexCreditCardProcessor(this.http) | |
}; | |
cardProcessor[cardType].process(cardNumber); | |
}; | |
}; | |
function CreditCardProcessor(http) { | |
this.http = http; | |
this.getUrl = function() { | |
throw {"message": "must override"}; | |
}; | |
this.validate = function(cardNumber) { | |
throw {"message": "must override"}; | |
}; | |
this.process = function(cardNumber) { | |
this.validate(cardNumber); | |
this.http.POST(this.getUrl(), { "cardNumber": cardNumber}); | |
}; | |
}; | |
function VisaCreditCardProcessor(http) { | |
CreditCardProcessor.call(this, http); | |
this.getUrl = function() { | |
return 'http://visa.com'; | |
}; | |
this.validate = function(cardNumber) { | |
var trimmedCardNumber=cardNumber.replace(/\s/g,''); | |
if (trimmedCardNumber.length!==16) { | |
throw new Error("Invalid card number"); | |
} | |
if(trimmedCardNumber[0]!=='4') { | |
throw new Error("Invalid card number"); | |
} | |
}; | |
} | |
function MasterCardCreditCardProcessor(http) { | |
CreditCardProcessor.call(this, http); | |
this.getUrl = function() { | |
return 'http://mastercard.com'; | |
}; | |
this.validate = function(cardNumber) { | |
var trimmedCardNumber=cardNumber.replace(/\s/g,''); | |
if (trimmedCardNumber.length!==16) { | |
throw new Error("Invalid card number"); | |
} | |
if(trimmedCardNumber[0]!=='5') { | |
throw new Error("Invalid card number"); | |
} | |
}; | |
} | |
function AmexCreditCardProcessor(http) { | |
CreditCardProcessor.call(this, http); | |
this.getUrl = function() { | |
return 'http://amex.com'; | |
}; | |
this.validate = function(cardNumber) { | |
var trimmedCardNumber=cardNumber.replace(/\s/g,''); | |
if (trimmedCardNumber.length!==15) { | |
throw new Error("Invalid card number"); | |
} | |
if(trimmedCardNumber[0]!=='3') { | |
throw new Error("Invalid card number"); | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CreditCard(http) { | |
this.process = function(cardType, cardNumber) { | |
var cardProcessor = { | |
"VISA": this.processVisa, | |
"MASTERCARD": this.processMasterCard, | |
"AMEX": this.processAmex | |
}; | |
cardProcessor[cardType](cardNumber); | |
}; | |
this.processVisa = function(cardNumber) { | |
http.POST('http://visa.com', { "cardNumber": cardNumber}); | |
}; | |
this.processMasterCard = function(cardNumber) { | |
http.POST('http://mastercard.com', { "cardNumber": cardNumber}); | |
}; | |
this.processAmex = function(cardNumber) { | |
http.POST('http://amex.com', { "cardNumber": cardNumber}); | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CreditCard(http) { | |
this.http = http; | |
this.process = function(cardType, cardNumber) { | |
cardProcessor = { | |
"VISA": new VisaCreditCardProcessor(this.http), | |
"MASTERCARD": new MasterCardCreditCardProcessor(this.http), | |
"AMEX": new AmexCreditCardProcessor(this.http) | |
}; | |
cardProcessor[cardType].process(cardNumber); | |
}; | |
}; | |
function CreditCardProcessor(http) { | |
this.http = http; | |
this.getUrl = function() { | |
throw {"message": "must override"}; | |
}; | |
this.process = function(cardNumber) { | |
this.http.POST(this.getUrl(), { "cardNumber": cardNumber}); | |
}; | |
}; | |
function VisaCreditCardProcessor(http) { | |
CreditCardProcessor.call(this, http); | |
this.getUrl = function() { | |
return 'http://visa.com'; | |
}; | |
} | |
function MasterCardCreditCardProcessor(http) { | |
CreditCardProcessor.call(this, http); | |
this.getUrl = function() { | |
return 'http://mastercard.com'; | |
}; | |
} | |
function AmexCreditCardProcessor(http) { | |
CreditCardProcessor.call(this, http); | |
this.getUrl = function() { | |
return 'http://amex.com'; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CreditCard(http) { | |
this.http = http; | |
this.process = function(cardType, cardNumber) { | |
switch(cardType) { | |
case "VISA": | |
this.http.POST('http://visa.com', { "cardNumber": cardNumber}); | |
break; | |
case "MASTERCARD": | |
this.http.POST('http://mastercard.com', {"cardNumber": cardNumber}); | |
break; | |
case "AMEX": | |
this.http.POST('http://amex.com', {"cardNumber": cardNumber}); | |
break; | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment