View Full Version : Design pattern question...is this the best way?
slopps
June 2nd, 2008, 05:07 PM
I have multiple instances of a square class that will all be clickable to link to things when clicked. However, when the user changes to "editMode" in my application, all the squares should become draggable and temporarily lose their linking functionality until the user leaves editMode.
What's the best way to approach this? A static "editMode" variable in my square class maybe? If so, do I just have a listener waiting for when editMode changes to change the mouse events or something?
SmoothDime
June 2nd, 2008, 06:51 PM
try it like this:
class App {
var editMode:Boolean;
var squares:Array;
public function App() {
editMode = false;
}
public function toggle() {
editMode = !editMode;
updateSquares();
}
private function updateSquares() {
for each(i:String in squares) {
squares[int(i)].updateMe(editMode);
}
//get the idea?
//have your button call toggle()
//write another function in here to populate the squares array
...
}
McGuffin
June 2nd, 2008, 07:50 PM
It depends on whether the edit mode is a feature of the application or whether it needs to be specific to the Square instances. Personally, I would have a variable within your application class to dictate what the current edit mode is, and then use a setter/getter for that variable to tell your Square instances what they need to be doing (you can also check this within the individual Square instances by accessing the variable).
public class App {
private var _editMode:Boolean = false;
public function App() {}
public function set editMode(value:Boolean):void {
_editMode = value;
if (value) {
// tell squares we're now in edit mode
} else {
// tell squares we're no longer in edit mode
}
}
public function get editMode():Boolean {
return _editMode;
}
A toggle method like SmoothDime's might be helpful, but personally I'd rather separate the two, keeping the getter/setter design and using the toggle variable for other things such as listening for a toggle button click.
Felixz
June 3rd, 2008, 06:20 AM
I would only manipulate events; when saetting editMode ON I would remove some listeners and added another. This should be done on container (regarding bubbling).
This would be best avoidiance of canstant checking of bool variable on any action.
slopps
June 3rd, 2008, 09:25 AM
Thanks guys. The set method seems most logical, but if I'm going to have, say, 1000 squares in my application, wouldn't it be better to just have some sort of static variable for the square class instead of my app having to loop through each and every square to set the editMode variable?
slopps
June 3rd, 2008, 10:02 AM
I think Colin is right. It would make a lot more sense to have an editMode variable in the overall application, rather than just the squares in the application.
Ok, so given that, would it be best for the application to loop through all of the square instances to toggle their editMode when editMode is changed, or should this somehow be done with listeners?
Felixz
June 3rd, 2008, 12:04 PM
Use bubbling feature: register a listener only at parent of those squares.
slopps
June 3rd, 2008, 12:37 PM
That makes sense. Rather than having 1000 listeners for 1000 squares, just have 1 listener on the parent and have that event bubble through its children? I've never used bubbling but I guess this is a good time to learn.
So I would have a button to enter edit mode, and it would broadcast the editMode change event?
Is this bubbling method actually more efficient than looping through all the squares to toggle their editMode?
Felixz
June 3rd, 2008, 12:42 PM
if u need to use only MouseEvents there is no problem, just add and remove listener on editMode ON/OFF
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.