views:

22

answers:

1

i'm creating NumericStepper and Slider component objects. both have very similar properties, except for a few. i'd like to refactor the code for creating them, but i'm not sure about the best way of doing so.

var numericStepper:NumericStepper = new NumericStepper();
numericStepper.width = 50;
numericStepper.minimum = 1;
numericStepper.maximum = 500;
numericStepper.value = 12;
numericStepper.move(20, 20);
numericStepper.addEventListener(Event.CHANGE, changeValueHandler);

var slider:Slider = new Slider();
slider.width = 200;
slider.minimum = 0.0;
slider.maximum = 1.0;
slider.value = 1.0;
slider.liveDragging = true;
slider.snapInterval = 0.01;
slider.move(60, 20);
slider.addEventListener(Event.CHANGE, changeValueHandler);

i'd like to create a function that returns the object, but i don't think i can return a *.

for example:

var numericStepper:NumericStepper = createValueChanger(50, 1, 500, 12, 20, 20);
var slider:Slider = createValueChanger(200, 0.0, 1.0, 1.0, 60, 20);

function createValueChanger(width:Number, min:Number, max:Number, val:Number, x:Number, y:Number):*
{
//create either a NumericStepper or a Slider
}

any ideas for the best approach here?

----------------------- UPDATE -----------------------

i've decided to use the following solution to remedy this problem:

var numericStepper:NumericStepper = new NumericStepper();
var slider:Slider = new Slider();
slider.liveDragging = true;
slider.snapInterval = 0.01;

assignComponentProperties(numericStepper, 50, 1, 500, 12, 20, 20);
assignComponentProperties(slider, 200, 0.0, 1.0, 1.0, 80, 20);

function assignComponentProperties(target:Object, width:Number, min:Number, max:Number, val:Number, x:Number, y:Number):void
    {
    target.width = width;
    target.minimum = min;
    target.maximum = max;
    target.value = 88;
    target.x = x;
    target.y = y;
    target.addEventListener(Event.CHANGE, changeValueHandler);
    }
+1  A: 

create an interface and a factory:

public interface IValueChanger extends IEventDispatcher {
   function set width(w : int) : void
   function set min(m : int) : void
   function set max(m : int) : void
   function set value(v : Number) : void
   function move(x : Number, y : Number) : void
}

public class Stepper implements IValueChanger {...}

public class Slider implements IValueChanger {...}


public class StepperFactory {
   public static const TYPE_SLIDER : String;
   public static const TYPE_STEPPER : String;

   public function create(type : String, width:Number, min:Number, max:Number, val : Number, x : Number, y : Number) : IValueChanger {  
       switch (type) { 
          case TYPE_SLIDER:
             var slider : Slider = new Slider();
                             slider.width = width;
                             ...
                             return slider;
          case TYPE_STEPPER:
             var stepper : Stepper = new Stepper();
                             stepper.width = width;
                             ...
                             return stepper;
          default:
              throw new Error("Unknown IValueChanger type: " + type);
        }
    }

   }
maxmc
interesting idea. seems a bit overkill, though. as well as double the lines of code. thanks for the idea though. i've gone with a solution that appears in an update of my question.
TheDarkInI1978