Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: The Role of Enums  (Read 8156 times)
justSteve
Courseware Beta
Sr. Member
***
Posts: 55


View Profile Email
« on: September 09, 2007, 12:30:23 »

In attempting to adapt the Arch101 sample I come to the usage of enums in the model. I think I understand them to function as an entity used to enforce type checking.

Yes?

The 2 sample enum structures provided are both used to populate a dropdown in view components. The statement that creates the constants on which the enums are based will hardcode the items into the class.

public static const ADMIN:RoleEnum 
    = new RoleEnum( 'Administrator', 0);
public static const ACCT_PAY:RoleEnum
    = new RoleEnum( 'Accounts Payable', 1);
& so on.


But what if the contents of the list is itself, db-driven and not known at compile time.

Can those static constants be generated at runtime?

thx
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: September 09, 2007, 02:51:23 »

Steve,

The value of a constant can be set either when it is declared, or in the constructor of the class that declares it. Therefore, while you could affect the *value* of a constant at runtime (only once), you still have to declare it in code ahead of time. So you couldn't dynamically grow the list.

The Typesafe Enum pattern became all the rage in the Java world a few years ago, though it has been implemented in many languages. It has many supporters and detractors for various reasons that I'm sure an evening's Googling should leave you weary from.

I used it in a low-key way the Architecture 101 courseware in order to demonstrate integration of other patterns into a PureMVC-based application.

My fast and loose Enum implementation was meant to associate a human-readable word with a code. And to provide a way of getting just the enumerated values as a list or that list with a '--None Selected--' entry at the top for populating a combo box in the view.

It allows code to act upon the enumerated values in a typesafe way. The view component that has a combo box populated by one of these Enums can test for the combo's selectedItem property being equal to the enum's NONE_SELECTED constant if a field is required, for instance. Or only make available, say, an hourly rate field if the combo's selectedItem is CONTRACTOR.

In short, the Typesafe Enum pattern is not really applicable if you are dynamically changing a list at runtime. It's really for use when you know the types ahead of time, but you don't want to hardcode the codes and their labels throughout the app, and if you want to have code that acts on specific type codes in a typesafe manner.

In the enterprise, middleware is often used to generate Enums for both client and server sides just like the VOs (Value Objects or Transfer Objects) that represent the Model. Database tables are defined containing all types, their labels, codes, and other info such as the order for display. A DBA may make a new entry in a type table, and the build script will create Java and Actionscript enum classes based upon the current types.

Hope this helps,
-=Cliff>
Logged
justSteve
Courseware Beta
Sr. Member
***
Posts: 55


View Profile Email
« Reply #2 on: September 09, 2007, 07:32:47 »

Thx Cliff...makes sense to show other patterns in action. And I can understand seeing enum in a static list application. It looks like in this case at least, the enum is responsible for feeding the proxy...or at least being sure the proxy doesn't get ingestion.

Yes? and which pattern tends a proxy where dynamic (server-side) data feeds the view?

mny thx
--steve...
Logged
Pages: [1]
Print