Actionscript3 Design Patternsfactory Method

Title: Actionscript3 Design Patterns:Factory Method Date: 2009-11-17 17:55 Author: eamonnfaherty Category: development Tags: Actionscript, design patterns Slug: actionscript3-design-patternsfactory-method

Background Reading
The creation method pattern

The Problem?
You want to use objects that are created else where and are of a set type but the implementations may vary.

SocketManager, or any thing that needs to be replace during unit testing.

The constructor of my complex object:

public class CharacterFactory {

    public static var factory : NonPlayerCharacterFactory;
    public static function getAPlayer() : IPlayer {
        return factory.getAButton();

The using of the creation method:

var player:IPlayer = CharacterFactory. getAPlayer();

Why is it good?
• It is easy to understand; it is a simple pattern that needs little extra code. It builds on the singleton pattern, which is well known.
• You can get objects without knowing how they are created or where they come from.
• You can share objects without the users of the factory knowing the objects are shared.

Why is it bad?
• It is confusing to read code where objects are not being created directly.
• Debugging is hard, as you have to know what was in the factory when you used it in order to know what object you have.

Further Reading
Read about the IOC pattern.

Factories are great! They allow you to define scope for your application. This means that you can set a testing scope when you are running unit tests. The involves setting the factories used in your factory to a test factory which provides test doubles. Changing the scope of the factories outside of your application means that you test your code without making any special changes.

Published: November 17 2009

  • category:
  • tags:
blog comments powered by Disqus