My Profile Photo

Personal Webpage of David Duggins


Well, I was born on a normal day in July, 1981 and have been creating chaos ever since. Born in North Carolina, but raised in the aftermath of the Soviet Union, Kazakhstan, I have been messing around with computers nearly my entire life. I wrote my first program in assembly when I was 11. In my early teens I ran a BBS connected to Fidonet and started building a website for my band. In 1999 I was introduced to Linux, and it was love at first compile. I started my career in IT in the early 2000's doing IT for a Car Dealership in Charlotte NC. I wrote my first major web app in Cold Fusion (an ecom app) at that time. In 2006 I left Charlotte and moved down to Columbia where starting working as a developer, freelancer and consultant. Currently I am working as a freelance developer and DevOps consultant!!


JavaScript Design Patterns

\n I was thinking recently about design patterns and realized that though I am familiar with them and their uses in php and ruby, I have never used them in JavaScript. I quick search found some code on Wikipedia for a singleton…but that is arguably not a very useful pattern. I looked under the entry for Factory Method patterns and found that while the singleton had an example for nearly every language under the sun, the Factory Method Design Pattern only had an example in Java (and a rather tasty one at that!) So there I was, the challenge was on. I decided to try and implement a Factory Method Design pattern in JavaScript without looking, using the Java source as my guide point. So here it is, using prototype in all it’s glory. I present to you, a JavaScript Factory Method Design Pattern:

Now, the point of a Factory pattern, is the creation of various objects types. So, as you can see I selected the pizza. I create three pizza types, all of them extend a base pizza class. Cheese, Pepperoni and Deluxe. Now, each type prints out a lable telling us what it is once it is created. FYI I did this in jsfiddle and you are more than welcome to play around and experiment…this is why I used jsfiddle. :)

The factory method uses a simple switch to figure out which object we want and then create it. I use an array to load the three types and some main body code to run through the array and fire off the factory method for each type. It’s a rather smooth little test if I say so myself!

Now, please feel free to pick me apart and offer up suggestions or comments! This is done entire as a learning process for me, so I do not give any assurances that this is the best way to implement such a pattern. Please let me know if you have a better way!

© 2024 David Duggins. All rights reserved.