Vorleak Chy's Blog
I have passion for technologies
Factory Method Pattern in JavaScript
Last two months I posted about Factory Method Design Pattern in C#. Now I do the same thing in JavaScript.
This is a simple example of the factory method pattern in JavaScript, The factory method getPhone() will generate Phone object. I just keep as organizing with namespace “Patterns” as in C#.
var Patterns = {};
Patterns.Nokia = function(){
this.getPrice = function(){
return 200;
};
};
Patterns.Motorola = function(){
this.getPrice = function(){
return 100;
};
};
Patterns.PhoneFactory = {
getPhone : function(type){
switch(type){
case "Motorola":
return new Patterns.Motorola();
case "Nokia":
default:
return new Patterns.Nokia();
}
}
};
var phone = Patterns.PhoneFactory.getPhone("Nokia");
console.log(phone.getPrice());
Subscribe-
Search-
Tags-
Categories-
Recent Comments-
- Using UUID as Primary Key in Ruby on Rails Thanks @Chamnap, I have...
- Using UUID as Primary Key in Ruby on Rails Not working. You need to...
- Installing GeoServer on Ubuntu Thanks for that! You saved me a lot...
- Change background color of TextBox or ComboBox in Windows Forms Hi....
- SQL Server 2005 Update Trigger Effect Multiple Rows Its really helpful...
- Copyright 2010 Vorleak Chy's Blog. All Rights Reserved. Powered by Wordpress | Theme designed by Chris Murphy
- Back To Top
- Home


Leave a Comment-