Design Patterns:
Adapter Patterns :-
Category : Design Pattern Author : Raina
Adapter pattern is one of my favorite design patterns. In this section I will explain how to add different weapon to warrior using Mouse Click.
To understand Adapter Pattern lets start creating a simple Warrior class for adventure game.
This only contains warrior parameterized constructor, function setLoc to set the x and y coordinates of warrior on stage and two parameterized (w: MovieClip) public function addObject, removeObject to receive Weapon objects.
code: Warrior.as
package {
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
public class Warrior extends Sprite{
public function Warrior(target: Stage, xLoc: int, yLoc: int){
this.setLoc(xLoc, yLoc);
target.addChild(this);
}
private function setLoc(xLoc:int, yLoc:int){
this.x=xLoc;
this.y=yLoc;
}
public function addObject(w:MovieClip){
this.addChild(w);
}
public function removeObject(w:MovieClip){
this.removeChild(w);
}
}
}
This will display the warrior on stage;
Suppose now our Client is asking to add weapons (Flag, Sword) to warrior.
Note: as per Object Oriented Programming rules, concrete class once created is closed for modification but open for extension, so to achieve this without violating the golden rules, we use Adapter Pattern.
An adapter Pattern implements functionality in two ways. It can
use either composition or inheritance to access an existing class. In OOP, composition is generally preferred over inheritance, so for this example we are following composition
To add new functionality / Objects we will create interface Iweapon.as ,this interface needs several methods to meet the requirements of the new context.
Code:
// On clicking the buttons Flag and sword will add new weapon and remove the earlier one. So we will define functions attach and remove
package {
public interface IWeapons {
function attachFlag( ):void;
function removeFlag():void;
function attachSword( ):void;
function removeSword():void;
}
}
Now lets create Adapter class (Weapon.as) which will implement the ITarget interface. The main objective of an adapter pattern is to convert the interface of an existing class to fit into a new context.
Code : Weapon.as
package {
public class Weapons implements IWeapons {
private var warrior:Warrior;
private var sword:Sword;
private var flag:PeaceFlag;
public function Weapons(w:Warrior) {
this.warrior = w;
}
public function attachFlag( ):void {
flag=new PeaceFlag(); // create insatnce of PeaceFlag
this.warrior.addChild(flag);
}
public function removeFlag( ):void {
this.warrior.removeChild(flag)
}
public function attachSword( ):void {
sword=new Sword(); // create insatnce of Sword
this.warrior.addChild(sword);
}
public function removeSword( ):void {
this.warrior.removeChild(sword);
}
}
}
As Weapon class implements interface Iweapon and has a parameterized constructor that receives a Warrior instance. It contains four public methods as defined in the interface. All four methods use one of the public methods specified in the Warrior class.
Now its time to create Main class
Main class creates a new instance of Warrior and passes
Three parameters to the constructor that places it on the stage with x and y coordinates. Then we will creates the instance of Weapon and pass the previously created Warrior
instance to it as shown
Next we will use addEventListener(Mouseevent.CLICK,onClick)
I will prefer to make it little generic so that we donot have to add addListener to each button instance kept on stage
To achive this we will create the btn array
private var btnArray:Array=new Array();
or simply private var btnArray:Array=[]
In constructor function we will pass instance of all btns from stage (Instance names) to
btnArray=[btn_Sword , btn_Flag]
now implement this array
for (var i:int=0; i<=btnArray.length-1; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK,onClick);
btnArray[i].buttonMode=true;
}
This will add listener to all the btns .
Code:
package {
import flash.display.*;
import flash.display.SimpleButton;
import flash.text.*;
import flash.events.*;
import flash.ui.*;
public class Main extends MovieClip {
private var weapons:IWeapons;
var btnArray:Array;
public function Main( ) {
btnArray=[btn_Sword,btn_Flag];
btnArray[1].visible=false;
var warrior:Warrior =
new Warrior(this.stage,this.stage.stageWidth * 0.25,
this.stage.stageHeight * .25);
this.weapons = new Weapons(warrior);
weapons.attachFlag( );
for (var i:int=0; i<=btnArray.length-1; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK,onClick);
btnArray[i].buttonMode=true;
}
}
private function onClick(e:MouseEvent) {
//to track particular btn we will use e.target
e.target.visible=false;
// used to truncate the full btn name like btn_Sword to string 'Sword'
var values:String=e.target.name.substr(4,e.target.name.length);
if (values=="Sword" ) {
this.weapons.attachSword();
this.weapons.removeFlag();
btnArray[1].visible=true;
}
if (values=="Flag" ) {
this.weapons.attachFlag();
this.weapons.removeSword();
btnArray[0].visible=true;
}
}
}
copyright ©2010 flashsource.in Contact : Administrator@flashsource.in