Design Patterns in ActionScript–Factory Method

Misc-Holiday In our last topic, we talk about the strategy pattern. And it helps us to encapsulate the change of algorithm. Today, we go on talking about the birds. We will consider how to generate the birds, eh, I mean the classes.

Now, we have the classes of eagle and penguin. We can use them anywhere we want. Further more, we can write down the following code to decide which class should be initialized.

public class birdFactory {
public function birdFactory(){

public function getBird(type:String):bird{
if(type == "eagle")
return new eagle();
return new penguin();

Now, we just need to look at the type, and we can decide which kind of birds to be initialized. This is a pattern called simple factory, it seems that everything goes all right.

I don’t want to talk too much about this pattern, because it is not our topic today. And I want to introduce you a principle of OOD, called Open-Close-Principle, which means we should open for extension, and closed for modification.

Let’s take a sight at the UML diagram of the above code. Then discussing where the problem is.


Eh, Here comes the problem. Look at the code I just write down, what’ll happen if I want to add a new kind of bird. It seems I need to change the method of getBird(). That’s right, this is the problem of this pattern. It doesn’t fit the O-C-P. So, we need another solution.

Remember the Open-Close-Principle. We will follow this principle to get the result.

We want a factory to produce the class, and when we also want the factory doesn’t need to be modified, if we want to add a new kind of bird.

Then we’ll need another two classes of factories, named eagleFactory and penguinFactory, paired to the birds.

The birdFactory will change into interface.

public interface birdFactory {
function getBird():bird;

And the concrete factory will implement the birdFactory. Here is the code of eagleFactory.

public class eagleFactory implements birdFactory{
public function getBird():bird{
return new eagle();

It looks like very simple and clean, isn’t it? Another concrete factory, penguinFactory is much the same.

Now the UML diagram will change into:


One factory, one kind of birds. If you want to add a new kind of birds, you need to add two more classes. It sounds a little red-tape, but you’ll get the rewards. You don’t need to change the code, and the code is easy to extension. It means when the requirement is changed, you can easily fit for it. And that’s why we need design patterns, it saves our time.

That’s all about this pattern, and we will talk about another related pattern next time.

Search-256x256 Demo | DownloadDownload Full Project


Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

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

4 Responses to “Design Patterns in ActionScript–Factory Method”

  1. Steven Sacks says:

    Class names should never start with lowercase letters.

  2. says:

    hi, Steven Sacks

    I know that class names should begin with an uppercase first letter and are usually nouns. start with lowercase letters is really bad. But that is only the guidelines apply to naming classes, isn’t it?
    BTW, I read the following post about class name:

    The following guidelines apply to naming classes:

    * Begin a class name with an uppercase letter.
    * Write class names in mixed case when it’s a compound or concatenated word.
    * Begin with an uppercase letter for a compound or concatenated word. A good example is NewMember.
    * Class names are usually nouns or qualified nouns. A qualifier describes the noun or phrase. For example, instead of “member,” you might qualify the noun by using NewMember or OldMember.
    * Clear names are more important than short names.
    * Don’t use acronyms and abbreviations. The exception to this rule is if acronyms or abbreviations represent the standard way to use a term (such as HTML or CFM). For commonly used acronyms, use mixed cases such as NewHtmlParser instead of NewHTMLParser for improved readability.
    * Use meaningful and simple names that are descriptive of the class contents. To avoid being vague or misleading, use generic names.
    * Sometimes a class name is a compound word. A qualifier might describe the noun or phrase. For example, instead of “member,” you might qualify the noun using NewMember or OldMember.
    * Do not pluralize the words you use in the class name (such as Witches or BaldPirates). In most cases, it is better to leave the words as qualified nouns instead. A qualifier describes the noun or phrase. For example, instead of “cat” or “buckaneer,” you might qualify the noun by using BlackCat or OldBuckaneer.
    * Don’t use a class name in the properties of that class because it causes redundancy. For example, it does not make sense to have Cat.catWhiskers. Instead, Cat.whiskers is much better.
    * Don’t use nouns that also might be interpreted as verbs. For example, Running, or Gardening. Using these nouns might lead to confusion with methods, states, or other application activities.
    * Use unique class names for each class in a single application.
    * Do not name classes so that they conflict with the names of built-in classes in Flash.
    * Try to communicate the relationship that a class has within a hierarchy. This helps display a class’s relationship within an application. For example, you might have the Widget interface, and the implementation of Widget might be PlasticWidget, SteelWidget, and SmallWidget.

    anyway, thanks for your advice and I will fix it from next post. ( although flex builder allow me type lowercase letters as my class name. :) )

  3. Daz says:

    It would be good if you made it clear how you would actually use the second, more complex factory. If I wanted to create a new eagle, for example, how would I go about this. If I created a new birdFactory and called getBird how would the function know if I wanted an eagle or a penguin?

  4. [...] > Gang of Four (GOF) Design Patterns in ActionScript–Factory Method – [...]

Leave a Reply