Announcement Announcement Module
Collapse
No announcement yet.
Type-safe Enums and DI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Type-safe Enums and DI

    First, I'm still on Java 1.4.

    The type-safe enum is a very handy pattern. In my experience, some type-safe enums grow to be more than just "simple constants" to have actual behavior like "good object'. Unfortunately, that behavior often requires additional objects and sometimes those objects even vary by which value in the Enum set is used.

    So, what I *want* is type-safe enums that come out of Spring.

    In my mind, this is a real chicken-and-egg problem. The question I have is if anyone has discovered a patttern that works well for this combination.

    Thanks.

  • #2
    I'm quite sure what you are looking for from Spring here. Any chance you can add some more of your ideas on this one.
    Last edited by karldmoore; Aug 30th, 2007, 07:32 AM.

    Comment


    • #3
      I'm looking for a design pattern that marries Spring with Joshua Blochs type-safe enum pattern.

      That is, I want to be able to say "Handler.FOO" and "Handler.BAR" in my code, but I want Foo and Bar to ultimately come out of my spring context, so they can be constructed with other spring beans, etc.

      Any clearer?

      Comment


      • #4
        Can't this be done currently? Of course, you couldn't say something such as:

        Code:
        <property name="handler" value="Handler.FOO" />
        But you should be able to do:

        Code:
        <property name="handler">
             <bean class="Handler.FOO" />
        </property>
        This could then be simplified with a namespace handler, perhaps even one that allows you to register new typesafe enums.

        -Lucas

        Comment


        • #5
          By the sounds of it, you want Spring to be creating the type-safe enum class and it's associated types. Out of interest, why?
          Last edited by karldmoore; Aug 30th, 2007, 07:32 AM.

          Comment


          • #6
            Type-safe enums tend to be used as a substitute for "type codes". Now, in an OO system, type-codes are a smell -- polymorphism is much better. I frequently end up evolving type-safe enums to have polymorphic behavior. Often, that behavior is dependent on various remote services that I would like to have injected in the polymorphic enum, or one of several similar implementations based on context. One solution sometimes available is to get completely away from enums. I find this difficult when the enum is a member of a persistent object - "type codes" in databases are not nearly as much of a smell as in OO.

            Comment


            • #7
              Lucas, I think you were looking for

              Code:
              <property name="handler">
                   <util:constant static-field="Handler.FOO" />
              </property>
              Or you can use a PropertyEditor

              Code:
              <bean class=“org.springframework.beans.factory.config.
                                    CustomEditorConfigurer”>
                  <property name=“customEditors”>
                      <map>
                          <entry key=“...Handler”>
                              <bean class=“...HandlerEditor” />
                          </entry>
                      </map>
                  </property>
              </bean>
              with

              Code:
              <property name="handler" value="FOO"/>

              Comment

              Working...
              X