brianemsu
08-20-2009, 12:00 AM
I have been working with OOP concepts for a little while now and I am starting to grasp some of the concepts. Although, I am wondering about when and if I should wrap certain functionality in it's own class. For instance, the example I'm working on now contains a Sprite object that is loaded with text fields stacked vertically to make a tall text box sprite object. I want this sprite object to move up and down on mouse input. Now, I could just include the mouse input listeners inside the class containing the sprite object to be moved OR I could wrap the mouse input functionality inside its own class. I'm thinking the "MouseUtil" class, as I've come to think of it, would be a Singleton, as there is only one mouse ever attached to the computer ( in most cases ).
It may look something like this:
package com.bee.utils
{
import flash.display.DisplayObjectContainer;
import flash.errors.IllegalOperationError;
import flash.events.MouseEvent;
public class MouseUtil
{
private static var _instance:MouseUtil;
public function MouseUtil( enforcer:SingletonEnforcer )
{
// throw error if instantiated with the 'new' operator
if( enforcer == null ) throw new IllegalOperationError( "Singleton Class!" );
}
// singleton method
public function get instance():MouseUtil
{
if( MouseUtil._instance == null )
{
MouseUtil._instance = new MouseUtil( new SingletonEnforcer() );
return MouseUtil._instance;
}
else
{
throw new IllegalOperationError( "An instance of MouseUtil already exists" );
}
}
public function startMouseMove( target:DisplayObjectContainer ):void
{
// target would most likely be a reference to the stage
target.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMoveEvent );
}
private function onMouseMoveEvent( e:MouseEvent ):void
{
// --add functionality here to move the sprite object up and down--
//
// being so specific to the one sprite object, it would create a strong
// dependency here... this doesn't seem to be what I want, but I have to create
// the dependency somewhere... where is the best place?
}
// also would add more mouse functionality later, as well as ways to remove the listeners
//
// is all of this just me trying to reinvent the wheel?
}
}
class SingletonEnforcer {}
Am I just adding unneeded complexity by wrapping mouse functionality in its own utility class? What would you recommend?
It may look something like this:
package com.bee.utils
{
import flash.display.DisplayObjectContainer;
import flash.errors.IllegalOperationError;
import flash.events.MouseEvent;
public class MouseUtil
{
private static var _instance:MouseUtil;
public function MouseUtil( enforcer:SingletonEnforcer )
{
// throw error if instantiated with the 'new' operator
if( enforcer == null ) throw new IllegalOperationError( "Singleton Class!" );
}
// singleton method
public function get instance():MouseUtil
{
if( MouseUtil._instance == null )
{
MouseUtil._instance = new MouseUtil( new SingletonEnforcer() );
return MouseUtil._instance;
}
else
{
throw new IllegalOperationError( "An instance of MouseUtil already exists" );
}
}
public function startMouseMove( target:DisplayObjectContainer ):void
{
// target would most likely be a reference to the stage
target.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMoveEvent );
}
private function onMouseMoveEvent( e:MouseEvent ):void
{
// --add functionality here to move the sprite object up and down--
//
// being so specific to the one sprite object, it would create a strong
// dependency here... this doesn't seem to be what I want, but I have to create
// the dependency somewhere... where is the best place?
}
// also would add more mouse functionality later, as well as ways to remove the listeners
//
// is all of this just me trying to reinvent the wheel?
}
}
class SingletonEnforcer {}
Am I just adding unneeded complexity by wrapping mouse functionality in its own utility class? What would you recommend?