# Type Switch Alternative

**URL:** https://forum.kirupa.com/t/type-switch-alternative/229827
**Category:** flash
**Created:** [June 20, 2007, 2:18pm UTC](https://forum.kirupa.com/t/type-switch-alternative/229827 "2007-06-20T14:18:22Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![tecknoize](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@tecknoize](https://forum.kirupa.com/u/tecknoize)
#### Post date: [June 20, 2007, 2:18pm UTC](https://forum.kirupa.com/t/type-switch-alternative/229827/1 "2007-06-20T14:18:22Z")

</div>

Hi guys,  
I have a very simple and generic problem I would like to solve but I can’t seem to find a good solution.

Basically, I have a class with a method that takes a string as parameter. This string define the type of the object created by the method. Somewhat like so:

```auto
 
public static const TYPE_A:String = "type_a";
public static const TYPE_B:String = "type_b";
 
private var _type:IType;
 
public function setType(type:String):void
{
    switch(type){
        case TYPE_A :
             _type = new TypeA();
            break;
        case TYPE_B :
            _type = new TypeB();
           break;
       default :
           throw new ArgumentError("Invalid Type");
 
   }
}

```

As you can see, It not really scalable because each time I add a new type, I must modify this class’s code. Is there any other ways to do this kind of thing?

Thanks!
