Friday, June 17, 2011

Strategy Pattern in ActionScript 3.0

Definition:
「Stratery Pattern defined algorithm family, were packaged together so that they can replace each other between, so that the algorithm change will not affect users using the algorithm.」

Features:
• Define and encapsulate a family of algorithms.
• Make encapsulated algorithms interchangeable.
• Allow algorithms to change independently from the clients that use it.

UML:




Example:
Build a [ Magic System ] interface,Name it:MagicSystem.as.

MagicSystem.as
package{
    public interface MagicSystem {
        //use magic
        function magic();
        //show magic name
        function showMagicName():String;
    }
}

MagicSystem has magic() and showMagicName()
Here's three MagicSystem below...

FireBallBullet.as
package{
    public class FireBallBullet implements MagicSystem{
        public function magic() {
                trace("use Fire Ball");
        }
        public function showMagicName():String {
                return "Fire Ball";
        }
    }
}

IceStormBullet.as
package{
    public class IceStormBullet implements MagicSystem{
        public function magic() {
                trace("use Ice Storm");
        }
        public function showMagicName():String {
                return "Ice Storm";
        }
    }
}

LightningBullet.as
package{
    public class LightningBullet implements MagicSystem{
        public function magic() {
                trace("use Lightning");
        }
        public function showMagicName():String {
                return "Lightning";
        }
    }
}

We implement MagicSystem interface and build three magic classes.
(Here's we only use trace() to represent different magic)

Than we build a new class,name it "Magician.as".
(It has MagicSystem)

Magician.as
package{
    class Magician{
        //MagicSystem
        private var magicSystem:MagicSystem;

        public function Magician() {
            //The default is FireBallBullet
            magicSystem= new FireBallBullet();
        }

        //Use magic
        public function magic() {
            magicSystem.magic();
        }

        //Change magic
        public function changeMagicSystem(ms:MagicSystem) {
            trace("Change magic ==> "+ms.showMagicName());
            this.magicSystem = ms;
        }
    }
}

Now we can test it.

Open a new file(GameScene.as),follow the code below...

GameScene.as
package{
    import flash.display.Sprite;
    public class GameScene extends Sprite{

        //creat a player
        private var player:Magician = new Magician();

        public function GameScene() {

            //use default magic
            player.magic(); //output:use Fire Ball

            //change magic
            player.changeMagicSystem(new IceStormBullet()); //output:Change magic ==> Ice Storm
            player.magic(); //output:use Ice Storm
            
            //change magic
            player.changeMagicSystem(new LightningBullet()); //output:Change magic ==> Lightning
            player.magic(); //output:use Lightning
            
        }
    }
}

Publish it!!!

If there is no typing error,you will see those output information.

output:
use Fire Ball
Change magic ==> Ice Storm
use Ice Storm
Change magic ==> Lightning
use Lightning

Example file:
Strategy_Pattern.rar

Reference:
邦邦的部落格 - Design Patterns - 策略模式
wikipedia -Strategy pattern


.

7 comments:

  1. vimax is a herbal medicine devoted to the men, who serves as the vimax canada permanent and extend appropriate in want, quickly and accurately without side effects and with permanent results, vimax asli has been the choice of international consumers. Vimax PiIls already well known parts of the world and is often used by men to increase male virility and maximize the size of the penis, in order to give satisfaction to their partners. obat pembesar penis Vimax has been recognized by scientists the world as an herbal remedy for pembesar penis and how this increase is no longer a secret mighty man, this is extraordinary usefulness fast, mutually agree and permanent.

    ReplyDelete
  2. Ciao a tutti, questo è molto interessante e molto creativo. Spero che ci sia una buona risposta da voi, Grazie desain rumah minimalis

    ReplyDelete