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

Inside Scoop: Adobe Flex 4 and Flash Builder 4 official release

Well, the time so many of us have been eagerly awaiting for the last two years has arrived, and its no surprise that the twitter-verse, facebook-verse, linkedin-verse, and other social media universes are buzzing quite loudly with the news already, before it even had a chance to hit the tabloids (no surprise there). I’m sure there are many Flex developers wondering:

What can I expect to see in this new release?

Is it a lot different from the public beta 2 that was on Adobe Labs for Max?

There’s a lot to cover, but here is the start to what will likely be a lot of upcoming posts…

One of the most significant things to take place after the beta 2 was the porting of the rest of the Flex 3 components (with the exception of DataGrid, AdvancedDataGrid, and OLAPDataGrid) to the Spark library, complete with the wonderful new Spark architecture, which adds a huge amount of scalability by separating the display logic from the encapsulated functionality of the component. Personally, that is one of the things I’m most excited about.

Additionally, code hinting and code generation is now a LOT more useful, which becomes exponentially more apparent as your Flex 4 applications get more and more complex. What does this mean for you as a Flex developer?
3 words: WRITE CODE FASTER!

Its true, as you get used to taking advantage of the code hinting, code generation, and service generating features (among other things), you’ll notice that your workflow will improve dramatically. I admittedly have been using the Pre-release and beta versions of Flash Builder 4 (but with SDK 3.4 or 3.5) for enterprise clients for over a year now. While everyone else was still coding in Flex Builder 3, I was consistently able to complete a task comparatively quicker using Flash Builder 4. Unfortunately, being on the version 3 SDK, I didn’t get the level of code-hinting that you get when using version 4, but the code generating facilities and enhanced project navigation alone made it so much easier to build very complex Flex apps.

Of course, a lot of work was also done on stabilization and A LOT of bug fixes!

Now that v4 is out of beta, I’m excited about the fact that clients will be much more inclined to use it, and rightfully so. The bottom line is simple: Flex 4 SDK and Flash Builder 4 extend their predecessors by leaps and bounds. The great news is that there is a lot of support for backward compatibility to ease the transition from 3 to 4 for apps that are in active development and want to take advantage of the features that ship with the new releases.

Ironically, today also marks the day of having completed our revisions to the chapters of Flex 4 in Action, being published by Manning Press. You can find the early access edition here. Our revisions come as the result of the extremely valuable feedback given to us by our reviewers, to whom we are very grateful.

Link: Get Access to Flex 4 in Action Now

Possibly Related Posts:



Posted by Dan Orlando on March 22nd, 2010 :: Filed under Flash Platform News

Second Generation Micro-architectures, Part 1: The Swiz Framework

Second Generation Micro-architectures

One of the primary reasons we have microarchitectures and design patterns in the first place is to maximize simplicity in application code. The problem is that not every architect understands this. Often we find architectures and design patterns being used in ways that do nothing more than increase complexity, which is counter-intuitive. This is why it is important to understand the framework that you are working with before you begin development on a production project that uses it, and Swiz is no exception.

A common characteristic among second-generation micro-architectures is that they are built to empower the developer, rather than over-power. One thing that all of these second-generation microarchitectures have in common, is that they use some form of Inversion of Control (IoC) and Dependency Injection, as defined by the great Martin Fowler in his book Patterns of Enterprise Application Architecture (Addison Wesley, 2002). With that in mind, you will first learn a bit about how these design patterns work from a conceptual standpoint before you get your hands dirty.

A Quick Background on Inversion of Control & Dependency Injection

Generally speaking, a good microarchitecture should allow the developer to decide how to implement an effective model based on the unique characteristics of the application. As previously stated, it should never force the developer into a specific model implementation. The Flex Framework is largely based on principles of Inversion of Control (IoC). In fact, this is true for most event-driven user interface development toolkits and frameworks such as Java Spring, EJB 3, Spring.NET, the ColdSpring framework for Coldfusion, Needle for Ruby, and FLOW3 for PHP 5.

The Inversion of Control pattern originally comes from another pattern described in a very important book published in 1995 that is often referred to as the “gang of four” or “gof” book, titled Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley). If you do not have this book already, I strongly suggest you add it to your collection as it considered a staple by every good developer I have ever met. One of the 22 design patterns described in this book is the Factory Method Design Pattern. IoC is a derivative of this pattern.

Why does ActionScript works well with IoC?

As you have learned, the ActionScript language is an object-oriented language in which objects are instantiated by declaring new instances, like so:

private var thisObject:ThisObject = new ThisObject(params...);

In contrast, IoC instantiates and registers a specific set of pre-defined objects at runtime, and injects them into each other using a concept known as Dependency Injection. By injecting these dependencies at runtime, the dependencies can be quickly and easily modified because the objects remain loosely coupled. The objects are loosely coupled because their dependencies are not “hard-coded” into the classes. This makes managing large code bases much easier.

Objects and object properties can then be wired up through binding. That means that all of your views and components can have what they need before anything ever even shows up on the stage!
With other implementations like Java Spring, an application’s objects and implementations can be declared in an XML document.  However, this particular method is not possible with Flex due to the order of operations that happen at application startup. More specifically, an application is not ready to parse an XML document until after it’s objects have already been instantiated.

Coding Flex Applications with Swiz

Swiz caters specifically to enterprise level Flex development by providing a solid architectural foundation derived from proven enterprise application development patterns. This includes:

  1. Minimizing dependencies to promote loose coupling
  2. Inverting the flow of control
  3. Dynamic mediation

Swiz meta data: Autowire

The Autowire tag is used for component wiring, while the Mediate tag is used for dynamic mediation of events during the bubbling phase of the event cycle. Placing the Autowire meta data above the property of an object tells Swiz that it should inject a specified property from another class into this property. This is written as follows:

[Autowire]
[Bindable]
public var mainViewPModel:MainViewPresentationModel;

In the example above, the MainViewPresentationModel class is injected at runtime by Swiz as a result of the Autowire tag being placed above it. It is also made bindable in this instance so it can be referenced from properties of MXML components. Let’s say you have a Boolean property in MainViewPresentationModel called calendarVisible. Then, let’s say that when the application enters a certain state, calendarVisible gets set to true. (Note that calendarVisible should also be bindable in this case). In the MXML view that has the presentation model autowired into it, you could now state the following:

 <components:CustomCalendar id="calendar" visible="{mainViewPModel.calendarVisible}" />

Ok, so what’s so special about that? Well, now let’s say you have an event called mainStateChanged, which is mediated in the controller class (more on event mediating in a moment). When the event is fired, the controller that is mediating it picks it up during the bubbling phase, and initiates the setting of calendarVisible to true. Since we’re binding to that value in the visible property of the component, the calendar will immediately become visible! Ok, now that you’ve got that figured out, it’s time to learn about mediating events.

Using the Mediate Tag

The first and most important thing I should note in regard to using the Mediate tag, is that events should ONLY be mediated in controller classes. If you place Mediate tags all over the application, mediating events in the model and the view for example, your application will be seem just as much like spaghetti-code as it would if you used no framework at all. Remember that Swiz gives you much flexibility, but with that flexibility comes a responsibility to use best practices when writing your code.

If you are already familiar with the publish-subscribe design pattern, then understanding event mediation should come fairly easily. The following code demonstrates the use of the Mediate tag:

[Mediate( event="DataEvent.SHOW_DATATIP", properties="index,data" )]
public function showDataTip( index:int, data:ArrayCollection ):void {
   var itemData:String = data.getItemAtIndex(index).toString;
   var dataTip:DataTip = new DataTip(itemData);
   addChild(dataTip); }

In the example demonstrated above, Swiz will always be listening for the DataEvent.SHOW_DATATIP event during application runtime. When Swiz finds a Mediate tag, it binds the specified event to the method that appears on the next line. When the event is fired, Swiz automatically calls that method.

In order to declare events with the ClassName.CONSTANT syntax as it is in the example code, you must tell Swiz where your events are located using the eventPackages property of the SwizConfig object that you declared in your main application file. Otherwise, you will need to use the fully qualified path when specifying the event that should be mediated.

Possibly Related Posts:



Posted by Dan Orlando on March 14th, 2010 :: Filed under Enterprise Flex
Tags :: , ,

Flash Camp Phoenix a Huge Success

Flash Camp Phoenix was held in front of a sellout crowd on Friday, and everyone considered it a huge success. I consider this a huge success for the local Phoenix Flash Platform community as this was the FIRST Adobe-based event held in the Phoenix-metro area and the turnout was HUGE. Some of the highlights of the even included Christian Saylor’s inspiring session on User Experience, Ryan Stewart’s keynote which announced Adobe’s upcoming plans for the Flash Platform. Additionally, Sarge Seargent from Adobe did an awesome session on Flash Media Server and the direction of the Open Source Media Framework and Flash video on the web. Kevin Fauth also kept things interesting with his comedy routine on programmatic drawing (you have to see it to believe it).

I was asked to publish my presentation from my session on “Building Custom Spark Components in Flex 4″, so I have included it below:

Possibly Related Posts:



Posted by Dan Orlando on February 1st, 2010 :: Filed under General, Tutorials
Tags :: , ,

Adobe Burned Twice After Apple Shuns Flash One More Time

It’s been hard not to notice the adamant (and sometimes extreme) stance that a number of Apple fans have taken in Apple’s defense to the shocking evidence that Apple has not been telling the entire truth with regard to their reasoning for not allowing Flash on iPhones/iPods. First, let me be perfectly clear: I own 2 MacBook Pro’s, 2 iPhone 3GS’, an iPod Nano, a ton of Apple software, and to top it off – I bought my Dad a new iMac and my sister a MacBook for Christmas. I love Apple’s products. This is my way of pointing out that this post is not meant to be a defensive response to the attacks against the Flash Platform. If anything, this should help clarify some things for the seemingly misinformed…


Argument: “[Flash] crashes my browser/system”

I’ve read a substantial number of claims that Flash is an awful product in the last 2 days because it “…crashes my browser”. I’ve read people say they have Flash disabled as a result of this, and some go as far as to say “Make a product that doesn’t suck and maybe Apple will include you on its devices”.

How these people are misinformed

From a technical standpoint, this is a ridiculous claim. The Flash plug-in is actually incredibly lightweight compared to the majority of plug-ins that people bog down their browsers with. The problem is not in Flash itself, but rather in the poor programming practices on the part of the developers that are writing the programs that crash your browser. If your CPU pins at 100%, it is usually the result of a recursive loop that is firing an event, which calls back on the method to fire the event again. This occurs on an exponential curve, which is why your CPU gets pegged so quickly. If Java applets won over Flash many years ago, you’d be seeing the same thing. It doesn’t matter what language it is, when developers don’t take a best-practices approach to the code that they write, the end user suffers. I see the same stuff happen in the worlds of Java and .NET. Its negligence and carelessness. Developers have a responsibility to uphold when they build a client-side application, and if that contract is broken, well, your browser crashes.

As one would expect, the average response to this logical reasoning is something along the lines of: “Well then its the fault of the programmers writing Adobe code…” which of course is then somehow tied back to Adobe using a similarly unreasonable argument. So here’s a question – what do you think these developers were doing before they started writing Flash programs? They didn’t appear out of thin air. In fact, the larger percentage of Flash Platform developers come from the Java development community. In the world of application development, some individuals simply do not care to follow standards and conventions in programming best practices, and that is where it ends. If you don’t pick up the trash in your house, you end up with a messy house that is hard to move through. If developers do not pick up their trash as a matter of convention in their programming, then you’re running a crappy program. You can’t blame that on the Flash Virtual Machine. It’s just not logical.

Argument: “Adobe is trying to monopolize the web with a closed format”

I have to be honest, this one made me laugh. Here is an actual quote from a comment that was left on the Flash Platform team’s blog site:

You Adobe people are frelling hypocrites, open standards indeed… Your claims will hold water when you donate the Flash platform to the open source movement. Until then you’re just spewing hypocritical BS about open standards.

Now, I’m not sure what a frelling hypocrite is, but I assume it is much like a regular hypocrite (couldn’t help it). Regarding the last point about these same people that are declaring shenanigans on Adobe because of programmers that write bad code, what do you think would happen if the “Flash Platform was donated to the open source movement” as this person suggests? It gets better too. Included in the package of extremely passionate claims of blasphemy and wrong-doing against Adobe comes the next major reasoning as to why Flash should not be allowed on Apple devices…

Argument: “Flash is loaded with security problems”

Here’s an example of how extreme this gets. The following quote is also pulled right from a comment left on the Flash Platform’s blog site:

“Adobe Flash is a complete piece of junk. When Adobe fixes all the security problems and bugs in Flash, maybe then it deserves more widespread adoption. I can’t wait until the day that Flash is gone.”

This essentially takes us round-trip right back to the very reason that the Flash Player is NOT a “donation” to the “open source movement”. I find it interesting that one would refer to Adobe as being “hypocrites” in this regard; and yet, it just shows how far beyond reason the people that are so actively protesting Flash in Apple’s defense truly are.

The truth here is so blatantly obvious that I am perplexed as to how so many people do not see it. It is very simple: it is inconsistent with Apple’s business model to allow Flash on their mobile devices. Apple makes a massive amount of money by controlling (and taking their cut) of the money that is paid for media consumption by owners of Apple mobile products. Uh…DUH! The only reason this whole thing became so controversial is because Apple was not up front and honest about it. Instead, they made promises to include Flash and then went back on those promises…multiple times. The claim was that Flash was simply too resource-intensive, so Adobe began development of a lightweight SDK that catered just to the iPhone. It was not until the announcement of the iPad; a machine with far more than enough resources to run the full-blown version of Flash, did it become so glaringly obvious that they had been giving Adobe the run-around all this time. I don’t know about you, but I don’t like it when I feel like someone has not been completely honest with me…but that’s just me. Apple’s runarounds caused many business strategists a lot of frustration, so while the community may be reacting in such an extreme fashion, Adobe really hasn’t been all that vocal about anything yet (probably too busy watching all of this back-and-forth bickering going on…what a PR nightmare!).

This is capitalism at its finest folks, it has nothing to do with the technology. In the end, its all business and its all about the numbers on the bottom line. it’s the country we live in. But that doesn’t mean we the people do not have the power to facilitate change.

With that said, I will conclude with this –
As long as Adobe and Apple – two of the strongest technological innovators in the world – are unable to find a way to collaborate in the effort of furthering the advancement of mobile technology, WE ALL LOSE.

Ryan Stewart has some interesting things to say on the topic as well, not only from the standpoint of an Adobe Evangelist, but from the standpoint of an Apple consumer. Check it out HERE…

Possibly Related Posts:



Posted by Dan Orlando on January 29th, 2010 :: Filed under Flash Platform News
Tags :: , , , ,

A few words on the state of current web tech: Apple’s iPhone, the iPad, Adobe Flash, and HTML 5

Although I’ve been in pretty deep finishing the book Flex 4 in Action, some recent conversations along with Apple’s iPad announcement this morning inspired me to blog about my take on the current state of the web as I’ve been following market trends in media consumption for a little while now.

HTML 5 vs. Flash

For some odd reason, I’m seeing more and more people putting the new HTML 5 spec up against Flash, largely due to the lack of support from Apple with regard to the iPhone, iPod touch, and now the iPad. As a side note, I love Apple, but did the marketing dept decide to take the year off and leave Jobs in charge of branding? WTF is an iPad? It sounds like a feminine hygiene product. Anyway, I digress.

First, its tough to compare HTML 5 to Flash. To me, one might as well try and argue that eggs taste better in the morning than spaghetti does in the evening… Eggs in the morning makes sense, right? It’s a good breakfast food. Similarly, who doesn’t like a good bowl of spaghetti once in a while for dinner? Seems to make sense… spaghetti = dinner food, as eggs = breakfast food.

To take a more pragmatic approach, this reminds me of Web Designer Magazine’s infamous “AJAX vs. FLASH” issue late in 2009. After furiously trying to find a way to pin the two technologies up against each other in a head-to-head, winner-takes-all, fight to the death; the author ultimately gave up and concluded that each technology had it’s own unique and specific purpose that filled a particular void that the other did not. They same could be said for why we have so many different programming languages and frameworks…I mean, have you seen the number of frameworks for Java alone? I’m not sure how Java devs are able to keep from going mad! And yet, every one of those frameworks has a very specific role and purpose.

Adobe Flash Platform vs. Apple Mobile Devices

The Flash Platform is the undisputed champion in RIA and is continuously running circles around the contenders, the most notable being Silverlight – which has been weak considering the number of .NET shops that have chosen Flex for their client side RIA development. Even more interestingly, the data provided by major research firms suggest that the iPhone will soon lose its dominance to Google’s Android platform, and has been reiterated by a number of different research companies now. WHAT?! ….its true, and the reason is that the public is generally dissatisfied with the full-nelson choke hold that Apple puts on its technologies. Apple lost massive market share last year in Music sales from the iTunes store – primarily to Amazon – because Apple wanted to keep the DRM choke hold on their tunes and Amazon had something else in mind. Even when Apple finally submitted to the pressure and let go of DRM, so many people (myself included) already had a bad taste in their mouth and didn’t really care to go back, and the rest are STILL unaware of the fact that Apple’s tunes are even DRM-free!

The research suggests that we can expect something similar to happen with the iPhone. A massive (and growing) number of powerful and innovative technologies are under development, all on the Flash Platform and all for different media consumption platforms (eg. cable boxes, mobile, computers, etc.) because we have the ability to seamlessly hook a variety of platform-specific client interfaces to the same network data source, which could be providing its data in near-”real time”. Google’s more “open” philosophy will eventually dominate (that is, assuming they can get a better handle on user experience), since developers ultimately decide what stays and what goes, and well, I work with 103 superstar Flex-developing iPhone owners whose patience with Apple’s un-kept promises to support Flash on the iPhone has run very thin.

Where do we go from here?

As far as HTML 5 having any impact whatsoever on the web (let alone on the Flash Platform) in the next two years… all I have to say is – don’t hold your breath. The market is actually in an unusually predictable state right now given the gradual and consistent advancements in technology, the philosophies of company leaders, and what we can derive from the trends of the last decade.

Anyway, that’s my take on things for what it’s worth.

Possibly Related Posts:



Posted by Dan Orlando on January 27th, 2010 :: Filed under Flash Platform News
Tags :: , , ,

Best Drunk on Software Episode Ever

FlashCamp is coming to Phoenix Dec. 4th – Early Bird Registration

I’ll be speaking at the upcoming FlashCamp event along with several of my colleagues, and I guarantee it’s going to be a wild ride. If you’re a Flash Platform developer, you definitely don’t want to miss this all-day event…

Where and When
The event will be held all day at the University of Advancing Technology (about a mile from ASU) in Tempe, AZ on December 4th from 9am to 5pm.

Speakers include:

  • Ryan Stewart (Adobe)
  • Sarge Sargent (Adobe)
  • David Tucker (Author - Adobe AIR Cookbook, Universal Mind)
  • Christian Saylor (Universal Mind)
  • Dan Orlando (Author – Flex 4 in Action, Universal Mind)
  • Kevin Fauth (Universal Mind)
  • Dan Holth (Universal Mind)
  • Carl Smith (Universal Mind)

Topics include:

  • What’s new in Flex 4/Flash Builder 4
  • Optimizing the Designer-Developer workflow with Flash Catalyst
  • What’s coming in AIR 2.0
  • 3D in Flash/Flex
  • Object-Relational Mapping in AIR
  • Advanced Flash video
  • Flash on mobile devices
  • and much more….

More info and Registration: http://flashcampphoenix.eventbrite.com/

Possibly Related Posts:



Posted by Dan Orlando on November 12th, 2009 :: Filed under Announcements
Tags ::

Adobe takes Flash to the iPhone at MAX 2009

Monday’s keynote was interesting, but if you ask most people that were there to give you a rundown, they’ll probably only be able to tell you one thing: Flash support for the iPhone is finally here. I can confidently say that I am one of those people, but from a general perspective, I found it interesting that it was Adobe’s end-of-keynote, anticipation building, biggest announcement of the conference. The one that got the throne wasn’t the new CFBuilder for ColdFusion development with Eclipse; not the Beta 2 releases of Flash Catalyst and Flash Builder/Flex 4 (which were originally supposed to go to full release this MAX and instead got delayed to 2010), or even the announcement of Flash version 10.1. Nope, the big announcement was Flash on the iPhone. Adobe even created a 4-minute video skit to play at MAX as a precursor to the announcement. In case you missed it, here’s what we saw right before finding out the big news:

I had lunch right after the keynote on Monday with several community leaders and people from Adobe. I thought it was interesting that the folks from Adobe were surprised – almost to the point of being appalled – by the fact that none of the community leaders that attended the lunch even cared about the announcement that RIM (i.e. Blackberry) had joined the Open Screen Project and now fully supported Flash. The only thing that was being discussed on the subject of mobile was Flash for the iPhone.

A number of sessions were also included in this year’s MAX session schedule that were focused entirely around developing for the iPhone. I attended one of those sessions, and considering how skeptical I was going into it, I was seriously impressed by what I saw. The graphics acceleration looked incredible, and the 3D objects moved quickly and fluidly around the screen.

The multi-touch interaction with the iPhone Flash Player were smooth even on a 2nd generation iPod

Multi-touch with the iPhone's Flash Player was impressively smooth even on a 2nd generation iPod

I was quite impressed by the full-fledged multi-touch capabilities and how smooth and responsive the demonstration Flash applications were to the various gestures. Even graphically intensive applications were impressively responsive to multi-touch and gestures, even on a 2nd-generation iPod (above).

Here’s a video of one of these sessions, entitled “Building Applications for iPhone with Flash Professional CS5″ :

Overall, considering the response and the buzz that this one announcement generated, to say that there are a lot of eager Flash developers ready to learn this new iPhone Flash SDK would be an understatement.

Possibly Related Posts:



Posted by Dan Orlando on October 9th, 2009 :: Filed under Flash Platform News
Tags :: , ,

Open source clientside RIA frameworks for SaaS

IBM developerWorks recently published a new article that I wrote, titled Use the best open source client-side framework for cloud computing. The article ultimately takes Flex and OpenLaszlo and puts them head-to-head against each other.

Why Flex and OpenLaszlo?

Flex is clearly the market dominator for RIA applications, and though OpenLaszlo may have a lot of catching up to do, its arguably the next best thing. In the article I do my best to make a fair and unbiased comparison, and someone even noted that I did not discuss the advantage of AMF that Flash has for data transfer. With that said, yes, it is slightly weighted to facilitate open-mindedness toward RIA and the fact that there is a legitimate competitor to Flex.

Why not Silverlight or JavaFX?

Silverlight is licensed under the tight, grubby fingers of Microsoft, who isn’t much of an advocate for open source. Not to mention the fact that although it is good with video, that’s about it… even most MS developers are not even bothering with Silverlight when it comes to RIA. Sun on the other hand, is undoubtedly an advocate of open source, but JavaFX is so immature that Sun isn’t even ready to release it as an open source product.

Can OpenLaszlo reasonably be compared to Flex?

Laszlo Systems doesn’t have the marketing dollars and community following that Adobe does, nor does it have 25 years of industry credibility behind it, so it is no surprise that there isn’t a very large community of RIA developers supporting LZX. However, one could logically assert that OpenLaszlo is “the little engine that could” to RIA, and there is only one reason I would give it such credit. The goal of LZX – the declarative syntax that is used with OpenLaszlo – is to have the capability of compiling to any format for the web. Being that it is unlikely Microsoft will release the Silverlight compiler to the open source community, that leaves us with DHTML and SWF. Of course, DHTML isn’t exactly “compiled”. Instead, OpenLaszlo generates the necessary DHTML from your LZX code for publishing to the web. The fact that it can do that AND compile to the native SWF format to run from the Flash Virtual Machine is impressive in and of itself and deserves some street cred at the least.

If you’re interested in learning more about OpenLaszlo, including how to code it, it’s history, and how it stands up against Flex, CLICK HERE to be taken to the article on the IBM developerWorks site.

Possibly Related Posts:



Posted by Dan Orlando on October 8th, 2009 :: Filed under Tools & Innovation
Tags :: , , ,

Enterprise Flex and the Swiz Framework Architecture, Part 3: Using Autowire and Mediate

In the last entry on the Swiz framework architecture, we took a look into the use of the CentralDispatcher. More specifically, we looked at the middle section of the following architectural diagram that I designed to help Swiz make more sense for developers. In this entry, we will get into the upper portion of the diagram, the real meat of the framework – Autowire and Mediate.

Architectural Diagram of the Swiz Framework

Architectural Diagram of the Swiz Framework

In declarative languages such as ActionScript, an object is created by declaring it and assigning the instance of that object to a variable using the new keyword. This has proven to be problematic in larger applications because of it’s “top-down” approach to object instantiation, which results in “tight coupling” (dependencies between otherwise independent objects) among other things.

In contrast, the Inversion of Control pattern used by Swiz reverses this object instantiation process by creating and implementing objects at application runtime. This eliminates dependencies and increases loose coupling because the implementation is created dynamically, as you are about to see.

The Swiz framework solves this problem rather inventively, through the use of two primary facets: the Swiz BeanLoader object and custom meta data tags. The Autowire tag is used for component wiring, while the Mediate tag is used for dynamic mediation of events during the bubbling phase of the event life cycle. These meta data tags are added to the framework simply by adding the following to the compiler settings in the Flex project properties panel:

-keep-as3-metadata +=Autowire,Mediate

Promote Loose Coupling with Autowire

Placing the Autowire meta data above the property of an object tells Swiz that it should inject a specified property from another class into this property, as seen in Listing 1.

Code Listing 1. Using the Autowire meta data tag

[Autowire( bean="displayRoot", property="currentView", twoWay="true" )]
[Bindable]
public var currentView:String;

You may have noticed three additional properties that immediately follow the Autowire declaration: bean, property, and twoWay. A bean represents an object that has been declared within a Swiz BeanLoader object; which brings us to…

The Swiz BeanLoader Object

Swiz does a superb job of separating the view from business logic. The concept of Beans and BeanLoaders are well known in Java GUI development circles, but have never been used before in application development for the Flash Platform. The result is a noticeable improvement in code organization, as well as the realization of a simple and clearly defined application life cycle.

A Swiz BeanLoader is an MXML file that acts similar to an object registry. When you create a bean loader, you’re extending the Swiz BeanLoader class, which registers the objects that you want Swiz to manage, as seen in Code Listing 2.

Code Listing 2. Example of a Swiz BeanLoader

 <?xml version="1.0" encoding="utf-8"?>
<BeanLoader xmlns="org.swizframework.util.*" xmlns:view="com.danorlando.view.*">
 <view:DisplayRoot id="displayRoot"/>
 <view:StatsView id="statsView"/>
 <view:VideoView id="videoView"/>
</BeanLoader>

The property value that is declared after the Autowire meta data pertains to the currentView variable in the displayRoot bean. Since the currentView property in code listing 1 and the respective currentView property that is found in the displayRoot bean are both marked accordingly, Swiz will create a binding between them to ensure that changes that occur to currentView in DisplayRoot are also propogated to the currentView property found in the component that holds the code found in listing 1 above. If that seemed like a lot to swallow, go through it a few times, this time taking it in bite-size chunks. This is one of the most important parts of Swiz, and has allowed me to do things with Flex and ActionScript that would otherwise be impossible.

The last property that is available with the Autowire meta data, called twoWay, is capable of setting up a reverse propagation in the other direction when set to true. In other words, the currentView property in the displayRoot bean will be updated if the currentView property in the component changes.

Save Time and Eliminate Repetitive Code with Dynamic Mediation

If you are already familiar with what a publish-subscribe design pattern is, then understanding dynamic mediation should be a piece of cake. To initialize dynamic mediation, the second Swiz meta data is used, called Mediate. Code listing 3 demonstrates the use of the Mediate meta data key word.

Listing 3. Using dynamic mediation in Swiz with the Mediate meta data key word

[Mediate( event="DataEvent.SHOW_DATATIP", properties="index,data" )]
public function showDataTip( index:int, data:ArrayCollection ):void {
   var itemData:String = data.getItemAtIndex(index).toString;
   var dataTip:DataTip = new DataTip(itemData);
   addChild(dataTip);
}

In the example demonstrated in listing 3, Swiz is always listening for the DataEvent.SHOW_DATATIP event. This happens by binding the event to the method with Mediate. When the event is fired, Swiz automatically runs the method. It is noteworthy to point out however, that in order to declare the event class and type as shown in code listing 3, the respective package containing the event must be registered in the eventPackages configuration setting where <SwizConfig /> is declared in the main application file.

The greatest benefit of dynamic mediation is essentially the same as that which is gained from dependency injection and Autowire meta data. Objects do not have to have any knowledge about each other or the application for which they reside. Not only does this make unit testing ridiculously easy for the application, it takes the concept of “code reuse” to a whole new level.

To see the implementation of Swiz into an actual application, check out Ben Clinkinbeard’s post HERE.

Possibly Related Posts:



Posted by Dan Orlando on August 24th, 2009 :: Filed under General
Tags :: , ,