I hate this pattern and it’s very popular these days with as3 API’s:
package
{
public class AllMightyClass
{
public var _value0:
String;
public var _value1:
Array;
public var _value2:
int;
public var _value3:
Object;
public function AllMightyClass
(params:
Object)
{
_value0 = params.
value0;
_value1 = params.
value1;
_value2 = params.
value2;
_value3 = params.
value3;
}
}
}
// FROM OUTSIDE THIS WOULD LOOK LIKE:
var amclass:AllMightyClass = new AllMightyClass(
//editor hints: AllMightyClass(params:Object)
Is this something that people use with some other language or something they learn at school?
It’s just wrong. The end user has no idea what params it wants or what’s their type.
Why not use:
package
{
public class Test
{
public var value0:String;
public var value1:Array;
public var value2:int;
public var value3:Object;
public function Test(value0:String, value1:Array, value2:int, value3:Object)
{
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
}
}
// FROM OUTSIDE THIS WOULD LOOK LIKE:
var amclass:AllMightyClass = new AllMightyClass(
//editor hints: AllMightyClass(value0:String, value1:Array, value2:int, value3:Object)
You may still pass stuff to baseclass in object-format if you like and you may still check if some values are null and then turn them into default. With the first pattern API’s user need to open the class to see what parameters it swallows or even worse browse the documentation.
But there is even better way to do this:
// FIRST CREATE A VALUEOBJECT CLASS
package
{
public class AllMightyClassVO
{
public var value0:
String;
public var value1:
Array;
public var value2:
int;
public var value3:
Object;
public function AllMightyClassVO
(value0:
String, value1:
Array, value2:
int, value3:
Object)
{
this.
value0 = value0;
this.
value1 = value1;
this.
value2 = value2;
this.
value3 = value3;
}
}
}
// USE THAT TO PASS YOUR PARAMETERS
package
{
public class AllMightyClass
{
public var value0:String;
public var value1:Array;
public var value2:int;
public var value3:Object;
public function AllMightyClass(valueObject:AllMightyClassVO)
{
this.value0 = valueObject.value0;
this.value1 = valueObject.value1;
this.value2 = valueObject.value2;
this.value3 = valueObject.value3;
}
}
}
// FROM OUTSIDE THIS WOULD LOOK LIKE:
var amclass:AllMightyClass = new AllMightyClass(
//editor hints: AllMightyClass(valueObject:AllMightyClassVO)
// OK IT EATS AN OBJECT CALLED AllMightyClassVO (let’s write some more)
var amclass:AllMightyClass = new AllMightyClass(new AllMightyClassVO(
//editor hints: AllMightyClassVO(value0:String, value1:Array, value2:int, value3:Object)
You can allways extend your VO class with some abstract when you want to pass it down to base classes. This way you make it look intuitive for end user.