Archive for October, 2006

Should you use Cairngorm?

Thursday, October 5th, 2006

Since the release of Flex 2.0 there has been alot of interest generated within the Flex community about the use of the Cairngorm micro-architecture. There is also alot of new Flex developers who are not yet familiar with both Flex and ActionScript that are jumping into everything all at once. I think that this is a really bad idea for developers who are new to Flex.

Cairngorm provides an architectural solution for common design problems. It has been tried and tested in both Flex 1.5 and Flex 2.0. Cairngorm is currently considered the defacto-standard for developing enterprise RIA’s with Adobe Flex. However, if you are new to Flex and ActionScript you should first concentrate on learning all of the new tools that are now available to you. Trying to learn Cairngorm on top of all of this is only going to confuse you, and even worse, might discourage you.

So when should you use Cairngorm? I would suggest you only use Cairngorm once you have a good understanding of how both Flex and ActionScript work. If you try to tie it all together without really understanding how Flex works you are bound to run into problems. Once you have learned the basics of Flex and ActionScript you should apply your new knowledge to build some small applications.

Once you have built a few small applications are you then ready for Cairngorm? I would say only if you have a good understanding of Design Patterns, why they are used, and how they apply to the development of Flex applications. And you should also have a clear understanding of the different patterns that are implemented in the Cairngorm architecture, most of which have been adopted from the core J2EE patterns such as the Front Controller Pattern, View Helper Pattern and so on. Once you have a solid understanding of Flex, ActionScript and Design Patterns, as well as some experience under your belt, then you are ready to start developing applications with Cairngorm.

As I have mentioned in a previous entry, I will be posting various sample applications which demonstrate some of the most common Design Patterns and how they are implemented in ActionScript 3, as well as tutorials on Cairngorm. You can view my previous posts which demonstrate a simple implementation of the Singleton Pattern and the Factory Pattern (both of which are implemented in cairngorm).

AS3 Factory Pattern Implementation

Monday, October 2nd, 2006

Occasionally when developing Flex applications you will run into a situation where the application calls for certain objects to be instantiated without knowing what type of objects they will be. For example, consider the following scenario: You are building a store in which there are multiple products available. You have created various value objects that represent each product in the store, but you have no way of knowing which products a user will select at runtime, therefore you have no way of knowing which VO’s to instantiate? This is a good example of where implementing the Factory Pattern comes in handy.

In case you are not familiar with a Factory Pattern let me give you a quick overview. The Factory Pattern is a creational pattern that models an interface for creating an object which at runtime delegates instantiation to it’s subclasses. This is called a factory pattern since it is intended to “Manufacture” objects. When using the factory pattern your code is loosely coupled since it eliminates the need to embed logic and application specific classes into your code.

For example, you can just as easily add logic to your code that determines which class to instantiate based on the product selected. The only problem with this approach is that you may not want to have the product classes available to the application in general, but rather provide a specific class which is responsible for deciding which class to instantiate, and then return the object to the application. This way your application can stay loosely coupled and the product classes can be abstracted from the application. This approach encourages encapsulation and delegation which is always a good thing in Object Oriented Programming.

I developed a basic sample application which implements the Factory pattern in ActionScript 3 that you can view here.

You can also click here to view the source code as well as the ASDocs

Design Patterns in AS3

Sunday, October 1st, 2006

As a software developer I spend a great deal of my time implementing various design patterns to solve common problems. As I explore a particular problem domain I can easily identify which pattern or patterns can be applied in order to create a viable solution.There are currently 23 common software design patterns around today. These 23 patterns can be organized into three separate categories: Creational, Structural and Behavioral.

Many of these standard patterns are commonly used by developers in all areas of programming and many apply to RIA development. 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 every time. 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.

Personally, when designing an application I prefer to use the Adobe Cairngorm micro-architecture, whereas there are various patterns combined to create an architectural framework which can be implemented to provide a common solution. An Architectural framework is a framework that does not provide additional services or API’s (such as the Flex framework) but instead provides a consistent, generic architectural framework that an application can be built upon.

Over the course of the next few weeks I will be posting examples of various pattern implementations in ActionScript 3 and how these patterns can be utilized in order create solutions to common problems.