AS2 and design model (I) --- simple factory model:
"The person who comes up with the design pattern is a genius, and the person who learns it can only be a good craftsman" ---- A senior
The simple factory model is probably the best of all design models. No wonder I can write an article to introduce it:
The simple factory model works like this:
The user uses an interface parameter (integer or String) to tell the processor the specific product you want, and then the processor passes
If (argu = 0)
Return new ConcreteProduct0 ();
Else if (arug = 1)
Return new ConcreteProduct1 ();
Else if (argu = 2)
Return new ConcreteProduct2 ();
....
How to Create related products? It's easy to understand.
Next, I will describe it with a preliminary physical demonstration system I recently developed:
The presentation system has two top-level objects. One is SceneManager, which is responsible for scenario switching, which is equivalent to the status of the manufacturer.
There is also a tmanager object pointing to the IEffect interface class. Wait for the SceneManager command at any time and ask for the effect of the current demo,
It demonstrates the effect, that is, it acts as the role of our product.
The initialization code in the home scene is as follows:
Import Sdgp. SMag. SceneMag;
Import Sdgp. SEffect .*;
Fscommand ("allowscale", false );
Var SceneManager: SceneMag = new SceneMag ();
Var EffectManager: ISEffect = new CS2BallConserve (); // a demonstration where the default state is two-ball dynamic conservation.
SceneManager. init ();
EffectManager. init ();
// Code segment used to implement the simple factory mode in the SceneMag class.
Class Sdgp. SMag. SceneMag extends RCSMag
{....
Private var mComMag: ISComMag;
....
Public function changeItem (num: Number): ISEffect
{
If (num = 0) return new CS2BallConserve ();
Else if (num = 1) return new CS3BallConserve ();
Else if (num = 2) return new CSMultiBallCol ();
Else if (num = 3) return new CSZMBall ();
Else
{
Trace ("unfinished items ");
Return undefined;
}
}
}
// The "Manufacturing" action is triggered by the change event of menuCombo.
Public function msgChange (evt: Object): Void
{....
If(evt.tar get ==_ root. menuCombo)
{...
// Some determination and scanning work ....
_ Root. EffectManager = _ root. SceneManager. changeItem (_ root. menuCombo. selectedIndex); // This sentence is used for manufacturing ~~~
...
_ Root. inittmanager. init ();
_ Root. fpsCombo. enabled = true;
}
}
How are you doing? I want to make it clear ~~~ :)
Click here to demonstrate the effect:
Http://www.flash8.net/bbs/UploadFile/2005-3/2005326211019860.swf