« August 2004 | Inicio | October 2004 »

September 29, 2004

An example of the abstract factory pattern

This is one of the creational patterns, whose intent is to provide an interface for creating families of related or dependent objects without specifying their concrete classes.

I’ll try to explain it using a "real life" example. Let’s suppose I want to decorate my house. And let’s suppose that there are only two different furniture shops in the town I live. One of them sells modern designs, and other one sells classic furniture. And let’s suppose again, that I’m only going to buy a door and a tv set ( I don’t have too much money ). And, finally, let’s suppose that I can buy the same items in both shops ( doors and tv sets ).

So, if I want to buy a tv set, I just have to any of the shops and ask for one tv set ( and pay it, of course ). And after having it properly installed at home, I know I can do some certain things with my tv set ( switch it on and off, pump the volume up,… ). And those actions could be done both with a modern tv and with and old one, although could be applied in a different way.

Well, I know I can go to the shop ( the modern one or the classic one, depending on what I exactly want ), but I could also pay someone to go and buy my tv and bring it home. I will only have to say "please, bring me a modern tv", and he / she should know that, to buy a modern tv, he / she should go to the “modern” shop and pay for a tv set. This guy / gal is the abstract factory. I don’t care how or what he / she does to bring a modern tv, I just want me tv now.

Before the as2 implementation, it’s time to point out something important. ActionScript doesn’t implement abstract classes. Sure, I can write a class that acts as an abstract class, but the fact is that the language doesn’t support them. So, although the GoF book says that the abstract factory and the factories should extend an abstract class, my implementation will be based on interfaces.

Well, back to code. If I go to the shop myself, or if I ask someone to bring me a tv, I just have to say “I want a tv”. So, both the shops and the guy that buys the tv will implement a common interface ( just to say it clearly, they should extend an abstract class, if possible, but as actionscript doesn’t implement them, they will just implement the same interface ).

Here is the interface:

import net.designnation.patterns.AbstractFactory.* interface net.designnation.patterns.AbstractFactory.IFactoryActions { public function getTV( ): ITVActions; public function getDoor( ): IDoorActions; }

There are two actions: getTV and getDoor ( "bring me a tv", and "I want a new door" ).

This is the code for the two shops:

import net.designnation.patterns.AbstractFactory.* class net.designnation.patterns.AbstractFactory.ModernShop implements IFactoryActions { function ModernShop( ) { trace( "I've picked a modern shop" ); } public function getTV( ): ITVActions { return new BrandNewTV( ); } public function getDoor( ): IDoorActions { return new ModernDoor( ); } }

import net.designnation.patterns.AbstractFactory.* class net.designnation.patterns.AbstractFactory.ClassicShop implements IFactoryActions { public function ClassicShop( ) { trace( "I've picked a classic shop" ); } public function getTV( ): ITVActions { return new OldTV( ); } public function getDoor( ): IDoorActions { return new ClassicDoor( ); } }

Both classes ( shops ) implement the same interface : IfactoryActions ( in other words, they implement two methods: getTV, and getDoor ).

Let’s take a closer look at the getTV method:

public function getTV( ): ITVActions { return new OldTV( ); }

Are we creating an instance of the OldTV class, but this method returns an interface?. Sure. That’s what lets us manage all the TV sets the same way. We won’t have to care about where we bought the tv (if it is modern or classic). We just will know that we can do some actions ( switch it on,… ). So, the two classes that represent the two kinds of tv sets will implement the same interface:

interface net.designnation.patterns.AbstractFactory.ITVActions { public function pumpUpTheVolume( ); public function shutUp( ); }

The modern TV will be:

import net.designnation.patterns.AbstractFactory.* class net.designnation.patterns.AbstractFactory.BrandNewTV implements ITVActions { function BrandNewTV( ) { trace( "I've received is my new 42 inches plasma TV" ); } public function pumpUpTheVolume( ) { trace( "sure, let me find the remote..." ); } public function shutUp( ) { trace( "By pressing down this key, the sound dies" ); } }

And the classic one:

import net.designnation.patterns.AbstractFactory.* class net.designnation.patterns.AbstractFactory.OldTV implements ITVActions { function OldTV( ) { trace( "I've bought an old TV" ); } public function pumpUpTheVolume( ) { trace( "My old tv has no remote, so I must wake up and pumpUpTheVolume myself" ); } public function shutUp( ) { trace( "My old tv has no remote, so........" ); } }

And will do the same with the doors. If the door is a modern one or a classic one, there will be the same actions: opening and closing it. So, now, the common interface that both classes will share is:

interface net.designnation.patterns.AbstractFactory.IDoorActions { public function open( ); public function close( ); }

The modern door:

import net.designnation.patterns.AbstractFactory.* class net.designnation.patterns.AbstractFactory.ModernDoor implements IDoorActions { function ModernDoor( ) { trace( "ModernDoor constructor" ); } public function open( ) { trace( "the modern door is opened" ); } public function close( ) { trace( "the modern door is closed" ); } }

The classic one:

import net.designnation.patterns.AbstractFactory.* class net.designnation.patterns.AbstractFactory.ClassicDoor implements IDoorActions { function ClassicDoor( ) { trace( "ClassicDoor constructor" ); } public function open( ) { trace( "the classic door is opened " ); } public function close( ) { trace( "the classic door is closed" ); } }

Well, it’s the moment to stop, and look back. We have two shops. Both shops sell doors, and tv sets. The classic shop sells classic doors and tv sets, and the modern one sells moderns TVs and doors.

The guy we send to the shop to bring us a tv will be the abstract factory:

import net.designnation.patterns.AbstractFactory.* class net.designnation.patterns.AbstractFactory.AbstractFactory { public static var MODERN : Number = 1; public static var CLASSIC : Number = 2; public static function getFactory( shopType: Number ): IFactoryActions { if ( ( shopType & MODERN ) == MODERN ) { return new ModernShop( ); } if ( ( shopType & CLASSIC ) == CLASSIC ) { return new ClassicShop( ); } } }

This class implements the same interface that both ModernShop and Classic shop implement. So, I will only have to say: “hey, abstract factory, bring me a modern tv”, and I could forget about the details of the process. And the end of the process I’ll have a tv set, but I will not know how it came home, where it was built, or sold…. But how?. Look:

import net.designnation.patterns.AbstractFactory.* var factory: IFactoryActions = AbstractFactory.getFactory( AbstractFactory.MODERN ); var myTV: ITVActions = factory.getTV( ); myTV.pumpUpTheVolume( ); var myDoor: IDoorActions = factory.getDoor( ); myDoor.close( );

You can download the code here

September 22, 2004

Thanks for the goodies!

Both my brother Javier and myself will like to thank Mike Chambers and Christian Cantrell for the goodies I've received today, and for the letter about Central that I received yesterday. We really feel that we've received some kind of pat in the back.

Once again, Macromedia has showed us that it really cares for its customers and that it really cares for the community.

We'd also like to thank everybody else that has been involved in this decision.

We know you've seen a lot of pictures of these goodies yet, but, we don't care. Here are our pictures:


That was a really nice picture of the box. The mouse helps to see the size ( yes, it is a big box! ).


And there you can see the guy who writes these posts, completely shocked after opening the box

September 20, 2004

Flashblog: Flash / php / mySQL opensource weblog

Flashblog is an opensource blogging system, based on Flash, php and mySQL, that has been developed by some members of the spanish-speaking community.

Although it is not available yet, an initial release is expected in October. Right now, the project and the website are only in spanish, but here is the link, just in case you want some more info.


September 17, 2004

Central 1.5 is out!

It happended tonight ( in Spain ). Macromedia announced the new version of Central.

I know there will be a lot of traffic today related to this release, but I just wanted to drop a quick note about the new features:

- Limited access to the filesystem
- File upload / download management
- A new license scheme.

You can find more information here

September 09, 2004

Flashtival 2004

The largest Flash and Webdesign conference of the Benelux will take place on Monday 27th September.

Flashtival 2004 will be an informative and educational event, completely related to Macromedia Flash, webdesign and Rich Internet Application development.

The Flashtival 2004 program is divided into 3 concurrent tracks, which are: "development", "animation and design", and "general Flash related topics". This way, every visitor will be able to attend a masterclass at his own level and choose the subject of his personal interest.

At the Flashtival website, http://www.flashtival.nl, you will find a complete list of all speakers.

Flashtival 2004 takes place at Congress Centre De Doelen in Rotterdam, Holland.