Design Patterns in ActionScript–Abstract Factory

favorites-128x128 Do you still remember the factory method pattern we talked about last time? If you’re already forgot it. May be you can take a look at the following intent, which was defined by the GOF book.

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

— By THE GOF BOOK

In out last example, we use eagleFactory and penguinFactory to decide which bird should be instantiated. And we can easily extend this program only by adding some new classes; we don’t need to change the main framework.

OK, now let’s do something more about the last example.

First, let’s add a new class named littleEagle, and the little eagle doesn’t have the ability to fly, and not migration. So, we can write the code as follows.

class LittleEagle extends Bird {
public function LittleEagle(){

super(new neverFly(), new neverMigration());
}
}

Then, we need to add a bird related to penguin, eh, I wonder that whether there is some kind of penguin flies and not migration. It seems that god doesn’t make that kind of creature. So, we need to make it, and we can call it littlePenguin. Of course, it was faked :) The code is as below.

class LittlePenguin extends Bird {

public function LittlePenguin(){

super(new fly(),new neverMigration());

}
}

According to the factory method pattern, we need to add two more factories to produce these two new products. And the UML diagram becomes like this.

clip_image002

How’s your feeling about this diagram?

Four factories correspond to four birds. Looking deep inside, we can found that littleEagle is much the same as eagle, and the same to the other two classes. If we want to add bigEagle and oldEagle, we’ll need another two more factories, and it sounds unreasonable. The eagles can be looked upon as series products. Maybe they should be produced by the same factory.

Now, I should show you the new pattern, named abstract factory pattern. Its intent was defined as follows.

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

– By THE GOF BOOK

Using this pattern to change our existing code, we can get the following UML diagram.

clip_image004

Of course, the code has to be changed. Let me show you the code of eagleFactory.

public class EagleFactory implements BirdFactory{

public function getBird():bird{
return new eagle();
}

public function getLittleBird():bird{

return new littleEagle();
}
}

Abstract Factory and Factory Method are both about generating the new object, while abstract factory cares more about a family of object.

Search-256x256 Demo | DownloadDownload Full Project

Enjoy!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “Design Patterns in ActionScript–Abstract Factory”

  1. [...] > Gang of Four (GOF) Design Patterns in ActionScript–Abstract Factory – Ntt.cc [...]

Leave a Reply