CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Professional Learner

Refactoring pattern quiz number 1

Here is a pattern I'm sure we all see quite a bit. Nothing wrong with it really. Small, clean, simple. However, when you take into context the fact that the following code is part of an entire program used to manage carnival rides, how would you refactor the following code? No need to post code, an explanation will do. I'll post the answer and the code in a little while.  This is one of the more straight-foward and obvious patterns to recognize in the context given.  Those of you new to OOP, it may be harder to see.

Part of a carnival program

 

Public Class Foo

 

    Private baseTokenAmount As Int32 = 1

 

    Public Function GetNumberOfRequiredTokens(ByVal typeOfPerson As PersonType) As Int32

        Select Case typeOfPerson

            Case PersonType.Infant

                Throw New Exception("Too young to ride")

            Case PersonType.Child

                Return baseTokenAmount

            Case PersonType.Adolescent

                Return baseTokenAmount * 2

            Case PersonType.Adult

                Return baseTokenAmount * 3

            Case PersonType.Senior

                Throw New Exception("Too old to ride")

        End Select

 

    End Function

 

    Public Enum PersonType

        Infant

        Child

        Adolescent

        Adult

        Senior

    End Enum

 

End Class

 



Comments

breichelt said:

I would add an abstract method "GetNumberOfTokens" into a Person base class, and then override that method in each of the different Person subclasses, infant, child, adult, etc. to calculate the appropriate number of required tokens, or throw the proper exception.
# April 25, 2005 9:03 PM

Adrian Florea said:

# April 26, 2005 2:55 AM

George Lewellen said:

I would refactor this into a conditional logic database table. If needed, it could be two dimensional. If the expected results can be identified into a cause and effect action, this logic can be moved from the program and placed into a database, thus, making the program logic less dependent on change. Just a thought...
# April 26, 2005 5:43 AM

Raymond Lewallen said:

Here is how to refactor this example I posted earlier.  The class Foo can pretty much be implemented...
# April 26, 2005 11:17 AM

Patrick Steele's .NET Blog said:

# May 1, 2005 9:20 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!

Our Sponsors