You are viewing the Articles in the Design Patterns Category

40 years worth of Objects

Of the many facets of software development which I take part in my true passion has always been Object Oriented Design, that is, I naturally tend to gravitate towards the design, relationships and interactions between objects within a system.

Currently I tend to concentrate primarily on ActionScript 3.0, however I am language agnostic in the sense that I am not driven by a particular language or simply by the solution in which a language provides, but rather by the gratification I receive from working with a language that adheres to Object Oriented Principles and the results of this practice.

I brought to my teams attention the other day at work that this year marks the 40th anniversary of the first Object Oriented language. I also mentioned this to a few former co-workers of mine whom are also fellow OO enthusiasts. They said I should blog about this milestone, and so I am as I feel many of you will find the origins of Object Oriented Programming interesting.

In the late 1960s two Norwegian programmers, Kristen Nygaard and Ole-Johan Dahl were developing a new language which would be able to naturally describe complex systems at a higher level using concepts that were much easier to grasp. This new language was called Simula 1 and was derived from an existing language Algol. Algol was a block structured language which was comprised of functions containing variables and sub functions. The owning function amounts to a data structure on the stack. The sub functions can access the variables defined within the data structure. It has been said that they experimented with moving this data structure from the stack to the heap, this way the variables and sub functions would outlive the owning function once it had returned.

Now if you read the last paragraph again you will notice that something sounds very familiar; the owning function is a constructor, the variables defined within the owning function are instance variables and the sub functions are methods.

Simula-67 – the very first Object Oriented Programming language was born!

And so I thought I would take a moment to reflect on the origins of the OO concepts which we work with on a daily basis and have come to enjoy so much.

Pseudo-abstract classes in AS3

I was developing an API which required specific abstract base classes to be developed in order to provide a default implementation for derived concrete classes, and, as you may know ActionScript 3.0 doesn’t support Abstract Classes (classes which can not be instantiated) so I had to develop my own solution that I thought I would share.

Abstract classes in general are classes which can not be instantiated directly, but rather would be used as a base class in which sub classes would provide a specific implementation. For example, a “Mammal” could be considered an abstract class as there are no concrete examples of a generic “Mammal” per say, but there are instances of it’s decedents; People, Dogs, Cats etc., which would more than likely also be abstract types themselves, however, eventually in the inheritance chain there would be concrete classes which would override the methods defined in the base class. The base class would provide a default implementation for it’s decedents but would also require that derived classes override methods which it can not provide a default implementation.

In ActionScript 3.0 you can not use the private access modifier in a constructor definition and ActionScript does not provide an actual abstract keyword, therefore you can not create a true abstract class.

So how do you create a “pseudo abstract class” in ActionScript 3? My solution is very simple:

In the above example all I am doing is creating an inner class: Private, which is only accessible from within the defined class Abstract. This enforces that only an object of type Private can be passed as an argument to the constructor.

Now were on to something. As you can clearly see from the example above, only a sub class of Abstract has access to an instance of Private via the protected static function getAccess(). Therefore only a sub class can instantiate an instance of Abstract, and why would it ever need to if it is a sub class? The sub class would then call super in the constructor and pass the instance of Private via getAccess(); as follows:

This is a pretty elegant solution in my book. Yes, it is obviously not an abstract class in the true sense of the word but it does the trick if you want to enforce that a class is never directly instantiated.

I would suggest restricting all Abstract classes access to internal if possible in order to help further avoid potential misuse by limiting access to package level classes only.

01.04.08 – I have since implemented a utility class for enforcing Abstract types which I recommend using over the implementation in this example. The AbstractEnforcer and examples can be found here

AS3 Iterator Factory API for Flex 2

I have been posting updates of my utils package lately but haven’t had the time to walk through how to use the API’s in detail other than the provided ASDoc.

One of the really useful API’s in the utils package is the Iterator implementation. The Iterator API provides a standard implementation of the Iterator pattern and is useful for traversing an object, array or collection of objects. The Iterator API provides an Array Iterator, ArrayCollection Iterator and an Object Iterator. You can use them individually as needed but one of the coolest features is the Iterator Factory. The Iterator Factory handles iterator instantiation and allows a single iterator instance, typed as an IIterator interface, to use any of the iterators, via the Iterator Factory.

You can click here to view an example of how to use the Iterator Factory.

AS3 Utility classes available for Flex 2

I have compiled some common AS3 utility APIs for public use as specified under the MIT license.

The utilities APIs are comprised of the following:

DeepCopy:
Creates a Deep Copy of an object reference to a new memory address and returns the cloned object

HashMap:
Creates a HashMap of object key / values as well as provides a standard API for working with a HashMap

ICollectionViewSortHelper:
Provides an API for collection alpha / numerical, ascending / descending sorting

Introspection:
Provides a robust API which performs detailed object introspection on a specified type

IteratorFactory:
Factory Pattern implementation which provides an API for Iterator instantiation

ObjectIterator:
Iterator implementation which provides methods for iterating over an object

ArrayCollectionIterator:
Iterator implementation which provides methods for iterating over a Collection

ArrayIterator:
Iterator implementation which provides methods for iterating over an array

StringUtil:
All static class which extends mx.utils.StringUtil to add additional methods for working with Strings

AVM2MemoryUtil:
All static class which returns the current memory allocated to a Flash Player instance in bytes, kilobytes or megabytes

I will continue to add to the utils package and make the updates regularly available. You can download the swc as well as view the ASDoc

www.cairngormdocs.org

One of the good things about Enterra Solutions is that we are partners with Adobe, and through this partnership we get alot of useful tips from the our client engagement partners at Adobe consulting.

I was informed of www.cairngormdocs.org, a really useful site which alot of people don’t know about. Cairngormdocs.org is a site dedicated to providing documentation and learning resources for the Cairngorm Microarchitecture for Adobe Flex. I am a strong advocate of the Cairngorm micro-architecture so if you are a Flex developer new to Cairngrom you should definitely check this site out.

I will also be posting a new tutorial called “Cairngorm made simple!”, which is intended to be a simple “Hello World” application which walks you through the sequence of events in a Cairngorm application. So if you are a bit confused about Cairngorm and how it is implemented, I guarantee that after reading this short tutorial you will have a very clear understanding of Cairngorm, what it does, what it doesn’t do and how to implement Cairngorm step-by-step in an application. So stay posted.

AS3 Iterator Pattern Implementation

The greatest thing about design patterns is that they are typically not much more than an efficient, structured way of doing things that most of us have already been doing for a long time. Design Patterns allow us to simplify common design problems into standard, common named solutions. By implementing common design patterns and best practices we can keep our applications consistant without re-inventing the wheel everytime. They allow us to solve the same problems over and over again in the same way. Design patterns also allow us to write code that is common amongst other developers. This is helpful as it allows other developers who are familiar with these patterns to easily and intuitively work with our code, and vice-versa.

Every Software Developer iterates over objects on a regular basis. That is, every software developer has the need to loop thru an array or traverse an object at some point during the development of an application. This is obviously a basic part of programming so we usually don’t give it much thought. However, once in while it is good to reflect upon the things that have become routine to determine if there is a common solution out there. We often run into situations where we create an API that consists of a collection of objects that a client may want to iterate over. In order to provide the required functionality we have to define an interface in which the client can iterate over our collection without exposing the collections underlying implementation. This is where the Iterator Pattern comes in handy. Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation. Iterators are intended to remove traversal responsibility out of the aggregate object.

I have created an AS3 Iterator API which consists of an interface (IIterator) that defines the methods which implement the Iterator Pattern. The IIterator interface is implemented by an internal base class (Iterator) which is sub classed by concrete iterator implementations (ArrayIterator, ArrayCollectionIterator and ObjectIterator). I have also provided an IteratorFactory which handles instantiating Iterator sub-classes so that they may be referenced by the same iterator instance.

You can view the example and ASDoc as well as download the swc for the AS3 Iterator API.