AS3 Design Patterns, " />

FlashPlatformist
Articles, Information, News, & Tutorials for Adobe Flash Platform Developers and Architects

Micro-architectures for Flex Enterprise Development

The key to writing solid enterprise Flex applications is to fully understand the overall architecture of Flex as a framework, as well as the various design patterns contained within it. By maintaining consistency between your micro-architecture and the Flex framework’s architecture, the application design and development process is guaranteed to be smooth, effective, and simple.

I must first point out that there exists a significant different between an application framework and a micro-architecture.  Flex is an application framework based on ActionScript 3.0, whereas Cairngorm – for example – is a micro-architecture for Flex. Currently, there exist two architectural frameworks for Flex that are in full release; Cairngorm and PureMVC.   Micro-architectures are good for three primary things:

  1. They provide a starting point for the architecture of a software program
  2. They establish a set of standards to be followed during development
  3. Standards lead to consistency, which makes the code within enterprise applications easier to maintain and add new features to later


Understanding Flex Architecture

The first thing to know as an enterprise Flex developer is that Flex is based around the Model-View-Controller (MVC) architecture. MVC became popular in 1995, when SmallTalk was the language of choice for a large portion of client side application development.

The second thing to know is that MVC is a global architecture in that it is a model that is meant to represent an entire body of code, whether it is an application or an application framework.  In other words, it is a method of organization for a large amount of code. Therefore, expect to find a number of design patterns contained within an application or framework that models MVC architecture, including Flex.

These design patterns depend entirely on the problems that must be solved with the application or framework. It is therefore theoretically possible to have billions of architectural frameworks for a single language, all based on the same MVC structure. However, the problems we encounter in enterprise software development tend to remain relatively constant (although they to evolve and change over time), and therefore it is unlikely that we would ever see that happen (although J2EE does have 72 so far!).


Cairngorm

Cairngorm is a micro-architecture for enterprise Flex applications initially created by a company known as Iteration II, which was bought by Adobe Systems and merged with Adobe Consulting.  Since then, Adobe Consulting has made strong efforts at focusing the continued development and maintenance of Cairngorm around that of Flex. It is for this reason that Cairngorm is worth more than just a look when considering a micro-architecture for your enterprise Flex application. Furthermore, if you’ve already commenced development on an enterprise level Flex or AIR application and you’re just reading this now, not to worry. The Cairngorm documentation does an excellent job at explaining in a step by step process how to go about integrating the Cairngorm framework into an already existing enterprise Flex application.

Iteration II was a firm that did much of it’s development in J2EE. As a result, Cairngorm implements a number of design patterns that are most popular in J2EE development. With that in mind, Flex developers that come from an enterprise Java background should be able to get used to Cairngorm fairly quickly and easily.  Even if you don’t have an enterprise Java background, Cairngorm is still an easy platform to understand as long as you are familiar with basic object-oriented programming concepts and design patterns.

Some of the more distinguishing factors of Cairngorm include the use of a central event and central event dispatcher, and the use of business delegates. Cairngorm is centered largely on the use of the observer and command design patterns and does a pretty decent job overall with regard to abstraction and encapsulation of code.

The strongest criticism against Cairngorm has been that it implements J2EE design patterns that are “force-fit” into the application architecture. An example of this is the use of business delegates.


PureMVC

The PureMVC micro-architecture focuses on maintaining strict adherence to the MVC architecture.  PureMVC actually started as an enterprise architecture for ActionScript 3.0 including Flex, Flash, and AIR. The architects soon realized that their architecture could apply to a number of other languages as well. Since then, versions of PureMVC have been created for C#, ActionScript 2.0, ColdFusion, Haxe, Java, JavaScript, Objective C, PHP, Python, and Ruby.

The most obvious characteristic of PureMVC is that it relies heavily on the Singleton design pattern, in that there exists a Singleton class that holds all references for each of the three MVC layers.  Whereas Cairngorm relies on “value objects” for data transfer (also known as DTO’s or Data Transfer Objects in Java), PureMVC uses proxies to manage data objects.  Other design patterns used in PureMVC include: Façade, Mediator, and Command.

The biggest argument against PureMVC is the lack of true abstraction and encapsulation due to the fact that pretty much any object or class can access any other object or class. The Singleton design pattern is probably the most controversial of design patterns among architects due to the fact that Singletons and Multitons are not necessarily meant to be used liberally, and should only exist for certain specific instances. It is therefore argued whether or not PureMVC is truly using what is considered to be “best practices” in the world of software architecture and design patterns.


Alternatives to Cairngorm and PureMVC

Comparatively speaking, AS3, Flex, and AIR are in their early stages of evolution, as are most technologies that fall under the RIA classification.  For example, JavaEE has over seventy different architectural frameworks available, each of which is focused on solving a specific type of problem. This supports the argument that there is no “one size fits all” solution, which is what both Cairngorm and PureMVC attempt to be.  If this were true, there wouldn’t be a new architectural framework released for J2EE practically every month.

Most Flex developers are still coming to grips with the architecture that is contained within the Flex framework, so it has not become common practice yet to propose new architectural frameworks for enterprise Flex applications.  It is noteworthy however, to point out one exception, called Fireclay.


Fireclay

Shashank Tiwari, a well-respected software architect and author in the Flex community, is the sole author of the Fireclay framework, which is currently in its infancy.  It is however currently available on Google Code, and well worth consideration for your next enterprise Flex application.  His most recent book, Advanced Flex 3 (Friends of ED), describes Fireclay in further detail in the first chapter. Other than that, there is little documentation, although the author has promised to provide a fair amount of documentation and explanatory examples in the near future.  The code is well commented though, so experienced architects should be able to quickly come to grips with this framework. If you do decide to use Fireclay as a starting point for your next enterprise Flex application, I strongly recommend that you pick up Advanced Flex 3 and use the first chapter to help you understand the underlying concepts that drive the Fireclay framework.

There are several notable differences that make Fireclay stand apart from Cairngorm and PureMVC.   Fireclay goes beyond the traditional page-based web architecture constrained and governed by the HTTP protocol and attempts to leverage the benefits of the thick and rich client. To start with, it provides a clear separation of the interaction and business logic from the view and model elements. It accomplishes this by placing the commands and services on the controller layer of the MVC architecture and uses an “event bus” for communications between the model, view and the controller.  The “event bus” facilitates the implementation of the event collaboration design pattern and realizes the loose coupling goals of a scalable and modular application.

Additionally, models are separated into two categories, a “local model” and a “proxy model”. This allows for appropriate source binding, caching, paging and lazy loading of data. For large applications, the framework provides registries to catalog commands, services and model elements. This reduces complexity and avoids code clutter and obfuscation. The views are assisted by helpers where required and are updated on model change events. The views read model elements and updates using the traditional observer-observable pattern.  This is consistent with the Flex framework, and as previously stated, consistency between the design patterns used in the Flex framework and that of your enterprise application architecture is a primary key to success.


Roll Your Own Architecture

The other alternative option you have is to roll your own architecture. In order to do this however, you should – at a minimum – have a firm understanding of the 23 architectural design patterns as outlined in the book titled Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, Helm, Johnson, Vlissides; Addison-Wesley; 1995). If you do not have this book, then get it…Now.

Additionally, you must have experience working with the Flex framework from an architectural standpoint and understand the design patterns that are used within it so you can maintain consistency with it in your architecture.

Lastly, and most importantly, don’t succumb to the false authority of tools or models. There is no substitute for creativity, intuition, and inventiveness. Keep in mind that the context of the application is critical. There comes a point where architectural rules are counter-productive.

Generally speaking, the time it takes to come up with workable solutions within your architecture depends on how complex the problems are that it is responsible for solving.


Conclusion

It seems quite common these days to mistake such micro-architectures for being a one-size-fits-all set of rules and methodologies that must be blindly adhered to and precisely followed. This type of mentality is counter-productive and stifles creativity. Every application is full of unique problems that require unique solutions, and to think that there is any “one size fits all” solution to enterprise application development projects is just pure ignorance.  It is therefore important to use your best judgment based on the situation when choosing how to approach an enterprise level software development project, and Flex applications are no exception.

Possibly Related Posts:



Posted by Dan Orlando on March 31st, 2009 :: Filed under Enterprise Flex
Tags :: ,

Understanding Flex Communication Protocols

Developing enterprise applications with Flex or AIR often requires a paradigm shift, regardless of your development background.  For web developers, it is the shift from stateless development to stateful. For Flash designers and AS2 developers, it is the painful realization that most of your programming up to this point hasn’t exactly been what is commonly referred to as “object-oriented” and a steep learning curve may lie ahead. For Java developers, it is the fact that although ActionScript 3.0 sometimes looks much like Java, this can be deceiving when developing enterprise applications with Flex and AIR.

For all intents and purposes, this article should serve as an informative resource for both Flex developers and technical business managers.  The goal is to provide a high-level view of enterprise Flex and AIR development, and assist with a global understanding of the technologies involved and when they should and should not be used.


Client-Server Communication

Choosing a server side language and communication method with Flex and AIR depend largely on facets of the business that are rarely taken into consideration by most developers. Usually, the technical decision-makers or lead architect will choose the technology that she knows best. This sometimes leads to disastrous results. The key to success here is to focus on the business model, and the tool will present itself.


Language doesn’t matter

While each server side platform has it’s own costs and benefits to the business; the Flash Virtual Machine really doesn’t care what is running on the server side. All it cares about is how the data is served up and whether or not it needs to deserialize it.  In fact, the FVM is usually completely unaware of what is running on the server side, especially if it is aggregating data from multiple sources.  That is the beauty of the separation between the server side business logic and the client side application that runs on the desktop or within the browser.  With that said, it is more practical to discuss method of communication.


Action Message Format (AMF)

The Action Message Format (hereafter referred to as “AMF”) is the binary data protocol officially supported by the Flash Virtual Machine. In other words, Serialization and Deserialization (otherwise known as marshalling and unmarshalling) of data objects is already built into – and is easily handled by – the Flash VM during runtime. AMF can be best described as a SOAP-RPC hybrid, in which data is transferred via Remote Procedure Calls. It is hard to place AMF in its own category here since it is really nothing more than an RPC protocol. However, the fact that it is the protocol officially supported by Adobe for client-server communications, and has built-in support with the Flash Virtual Machine makes it worth placing in its own category.

Advantages of AMF include: rapid data transfer, automatic marshalling and unmarshalling of data objects by the FVM, support across the entire spectrum of server side languages including .NET, PHP, Java, Ruby, and Python.

Disadvantages of AMF include: proprietary protocol that is not compatible with other client-side RIA development platforms such as DOJO, Scriptaculous, and SilverLight.  However, JavaScript communication via AMF with a server language is made possible through the Adobe AIR platform.
Requirements: Integration is fast and simple, and is made possible by integrating one of the following frameworks into your respective server environment:
PHP:  Zend, AMFPHP,  WebOrb for PHP, SabreAMF
Java: BlazeDS, Red5, GraniteDS, WebOrb for Java
.Net: FluorineFx, WebOrb for .Net, AMF.NET
Python: PyAMF
Ruby on Rails: RubyAMF, WebOrb for Ruby on Rails

Representational State Transfer (REST)

REST is built on simple http. However, support for RESTful services in Flex 3 is limited. Therefore, you will need to use either BlazeDS or a proxy in order to extend the Flex functionality for RESTful services beyond GET and POST.

Advantages include: direct communication with xml data sources, useful for web syndication

Disadvantages include: it is slow in comparison to the AMF protocol


SOAP, WSDL, RPC, and Everything Else

Most communication protocols (AMF included) are really just a REST-RPC hybrid that uses the http protocol in some way for request-response transfers. These types of communications are usually accomplished by simply calling a specific URI via the HTTPService class in Flex.

Possibly Related Posts:



Posted by Dan Orlando on March 31st, 2009 :: Filed under Enterprise Flex, Tutorials
Tags :: , , , ,

New Toolkit for Eclipse Released by Amazon Web Services

Amazon has released the AWS Toolkit for Eclipse in tandem with a how-to tutorial that I finished last week. I’ve spent a lot of time recently learning about the services offered under the Amazon Web Services umbrella, and I’m pleased to say that Amazon has truly taken a lot of initiative in the way of cloud computing in the last few years. There’s a bit of a learning curve involved, but once you figure out the architecture of their Simple Storage Service (S3), Elastic Compute Cloud (EC2), and SimpleDB, the benefits will be very clear, particularly for network administrators responsible for deploying scalable enterprise clusters of systems.

The lastest and greatest innovation from AWS is the Toolkit for Eclipse, which allows you to deploy and manage Tomcat EC2 server clusers (i.e. “instances”) and debug Java applications on the remote server – all right from the Eclipse IDE!

Find out more about this new technology and how to install it (its FREE!) in my new article just recently published in the “Featured Tutorials” section of the AWS web site.

Possibly Related Posts:



Posted by Dan Orlando on March 28th, 2009 :: Filed under Tools & Innovation
Tags :: , ,

Has the Economy Impacted the Evolution of RIA Technology?

Its been a while since I’ve posted anything new, and needless to say, I’ve been pretty busy. Six weeks ago I moved from Las Vegas to Phoenix after the job market in Vegas closed up. Ironically, as soon as I got settled in Phoenix, the RIA job market here closed up too. The smaller software shops (less than 100 employees) were already few and far between, and now many of them are no longer in business.

Phoenix is big in the telecom and microchip arena, with the likes of AT&T, Microchip, Intel, and IBM employing a significant percentage of IT specialists. Unfortunately, RIA development seems to only be happening in California and New York right now, with most enterprises cutting research and development out of the picture rather than investing in it.

The ironic thing about that is that the past suggests that a down economy opens up the market for new technology. Recessions and depressions make for a society that is vulnerable. In Western civilization, the more vulnerable a society becomes, the more effortless it is to introduce bleeding-edge technologies that change the way people live. The reason? People are already out of their comfort zone. You don’t have to force them to “think outside the box” in order to adapt to new technologies. They’re already outside the box, regardless of whether or not they are fighting to get back in it.

The point here is that as developers, while we may dedicate ourselves to the platforms that will be most prominent in the future, the risk we take is getting too far ahead of the curve, and when the economy causes technological evolution and innovation to slow way down, we either wait for society to catch up again or take a step backwards ourselves.  Or, we keep moving forward at the same rate – albeit as a much smaller community. Those of us who stay determined to put to use what we have spent an unimaginable amount of time learning in recent years, we form our own corporations and build the things that everyone else is afraid to invest in. Why? Because we believe in it.

I’ve talked to upper managers of 71 companies across the U.S. in the last 6 weeks, and I’ve heard every possible excuse for putting the virtual choke hold on RIA development, the most prevalent one being “It’s too risky right now to be dumping money into unproven technology.” These are usually the same people that find themselves with a lot of regret five years later. Why? Because a new generation of businesses led by technological innovators is being born, and by the time big business recognizes the threat, it will be too late to jump on board.

Expect to see a flurry of innovations come to market from companies no one has ever heard of in the next year. As a matter of fact, this will likely be a major contributing factor to what brings us out of this recession.

Possibly Related Posts:



Posted by Dan Orlando on March 23rd, 2009 :: Filed under General
Tags ::