Back to Blog

Design Patterns Primer

Sun, 07 Apr 2013, by Syrsly

Primers are simply overviews of a subject, a summary, an introduction. This primer is an introduction to computer programming design patterns. Design patterns are the methods of which we put the pieces of code together. Patterns include the factory method, singletons, and many other design principles. This primer will cover the most basic design patterns of computer programming. Not all patterns are mentioned here, though.

The Constructor

Constructors are simply the method of creating an object instance. In ActionScript, you use constructors like so:

var Superman:DCCharacter = new DCCharacter();

You can pass in attributes to the object construct, as well, like so: new DCCharacter(attributes);

The Builder

If you want to make objects with custom attributes, you could use a builder. Builders are functions which output customized objects based on what your needs are. Builders are extremely useful in web development, because you often want to make very repetitive mark-up code. For example, if you are using PHP to make a gallery from a directory of images, you may use a for loop and place each image in a wrapper of <div><img src="&quot;.image.&quot;"></div>. That is a builder design. You can further abstract the mark-up builder by making it into a utility that places your given text inside whatever mark-up you want, like a paragraph or emphasis or whatever. I use builders very often to abstract away annoying code like hyperlinks. Instead of typing <a href="whatever," i="" can="" use="" a="" more="" friendly-looking="" function,="" like="" link()="" or="" outsidelink().="" the="" beauty="" of="" using="" builders="" is="" you="" get="" rid="" repetitive="" tasks="" and="" make="" your="" code="" look="" prettier.=""

The Proxy

Similar to a builder in its behavior is a proxy design. Proxies are go-to objects that redirect you to other objects or resources. For example, if you have to load some images into your application, you may want to make a function that loads that image so you can recycle the function for later use. If you abstracted the object creation functionality into its own object, that object would be a proxy. I use proxies all the time when working in Flash or ActionScript, because I use external resources a lot and like to abstract the loading process and property getters and setters. Proxies are not as common in web development, at least not where I'm involved, but they are still used from time to time.

The Singleton

Singletons are objects which exist only once at any given time. Singletons are not instanced or created, they just exist. Do not confuse singletons for constants. Constants are just variables which you cannot change the value of (which means they aren't variables at all). You would use a singleton design for communicating between objects in a game or for storing global variables. Many scripting languages do not have true singletons, but you can fake it by placing all your objects within a parent object that never gets destroyed. The problem with parent objects is they are not as memory efficient and tend to keep using the memory they needed to store child objects even after those objects are gone. That is a very big overhead for video games, which often have objects appearing and disappearing and often require a lot of memory in the first place. Other applications also try to avoid parenting objects when possible for similar memory efficiency problems.

The Factory

Factories in programming are like assembly lines of code. You have each of your objects in your application based on a base object. That object may be based on another object, as well, which would allow you to abstract many behaviors into their own objects. For example, if you have vehicles in a game, every vehicle, whether it is a car or truck or plane, could be an extension of the vehicle object. By extending the base object, the objects inherit all the base object's behaviors.

The Prototype

Prototypes are objects which you can use to construct a variety of objects through cloning. For example, say you made a car and now want to make a trail of cars behind it based on the original car. Each car would have its own properties but would be a copy of the original object, the prototype. Then, when the other object is removed or changes, the clone can live on and have the original values you gave it.

Learning For Yourself

Everyone learns in their own way, usually through hands-on experience. You could try studying a book for your language of choice. I recommend Advanced AS3 with Design Patterns if you use ActionScript, which is admittedly a dying language, but a lot of what's learned in this book transfers well to other languages. I bought the 2007 edition of the book by Joey Lott a while ago and loved it and I still have the book as a reminder of where I got started. The 2011 edition seems to be by a different author, so you may not get the exact same insights, but judging from the preview on Amazon, the 2011 book is actually better organized and easier to understand. I hope this article helps you in some small way to understand basic design patterns.


This post not yet tagged.