The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Refactoring pattern quiz number 1 answered

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Raymond Lewallen

Posts: 312
Nickname: rlewallen
Registered: Apr, 2005

Raymond Lewallen is a .Net developer and Sql Server DBA
Refactoring pattern quiz number 1 answered Posted: Apr 26, 2005 1:09 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Raymond Lewallen.
Original Post: Refactoring pattern quiz number 1 answered
Feed Title: Raymond Lewallen
Feed URL: /error.htm?aspxerrorpath=/blogs/raymond.lewallen/rss.aspx
Feed Description: Patterns and Practices, OOP, .Net and Sql
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Raymond Lewallen
Latest Posts From Raymond Lewallen

Advertisement

Here is how to refactor (or at least how I refactor, there are other ways) this example I posted earlier.  The class Foo can pretty much be implemented however you want.  In my case, I wanted to get the number of required tokens for a specific person using an interface.  You can see that I created in interface IPerson that contains GetNumberOfRequiredTokens.  This way I can just pass in an IPerson object type and use the polymorphed, overriden function in the child class, or adult class (each of which inherit from the Person class that implements the IPerson interface).  I implemented a property in the base class that contains the default number of tokens, and then for each derived class, I use that along with custom calculations the derived class to come up with the appropriate number of tokens required.

The refactoring pattern we are targeting here is called “conditional to polymorphism”, along with a few other names.  The goal was to refactor our initial “Select Case” into a base class with derived classes.  This increases usability and scalability greatly over our initial code we started with.

Don’t forget, and I did not demonstrate this, you will ALWAYS want to write a unit test for your initial code (Foo.GetNumberOfRequiredTokens) and make sure it passes all scenarios BEFORE beginning your refactoring process and DURING and AFTER your refactoring process.

To test this, create a new object of type Adult.  Then call Foo.GetNumberOfRequiredTokens(adultObject) and it will return 3.

Refactored: conditional refactored to polymorphism

 

Public Class Foo

 

    Public Shared Function GetNumberOfRequiredTokens(ByVal person As IPerson) As Int32

        Return person.GetNumberOfRequiredTokens()

    End Function

 

End Class

 

Public Interface IPerson

    Function GetNumberOfRequiredTokens() As Int32

End Interface

 

Public MustInherit Class Person : Implements IPerson

    Private baseTokenAmount As Int32 = 1

    Protected ReadOnly Property Tokens() As Int32

        Get

            Return baseTokenAmount

        End Get

    End Property

 

    Public MustOverride Function GetNumberOfRequiredTokens() As Int32 Implements IPerson.GetNumberOfRequiredTokens

End Class

 

Public Class Child : Inherits Person

    Public Overrides Function GetNumberOfRequiredTokens() As Int32

        Return MyBase.Tokens()

    End Function

End Class

 

Public Class Adult : Inherits Person

    Public Overrides Function GetNumberOfRequiredTokens() As Int32

        Return MyBase.Tokens * 3

    End Function

End Class

 

Public Class Infant : Inherits Person

    Public Overrides Function GetNumberOfRequiredTokens() As Int32

        Throw New TooYoungException

    End Function

End Class

 

Public Class TooYoungException : Inherits Exception

 

End Class

Read: Refactoring pattern quiz number 1 answered

Topic: goto DumpTheSucker Previous Topic   Next Topic Topic: Because the parent does not exist

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use