Go Back

Refactoring part 3: Move Method

 

In the previous example, the amountFor(Rental rental) {} method was inside the customer. Since the method doesn’t use any data from customer and only uses data from a rental Fowler alerts us that we should move this method to where it has the least viscosity for change.. and that is over in the Rental Object.

 

So to be backwards compat inside of Customer you have this:

 

        private static double AmountFor(Rental rental)

        {

            return rental.Charge;

        }

 

And then in Rental you’d have this new property:

        public double Charge

        {

            get

            {

 

                double result = 0;

                switch (Movie.PriceCode)

                {

                    case Movie.REGULAR:

                        result += 2;

                        if (DaysRented > 2)

                        {

                            result += (DaysRented - 2) * 1.5;

                        }

                        break;

                    case Movie.NEW_RELEASE:

                        result += DaysRented * 3;

                        break;

                    case Movie.CHILDRENS:

                        result += 1.5;

                        if (DaysRented > 3)

                        {

                            result += (DaysRented - 3) * 1.5;

                        }

                        break;

                }

                return result;

            }

        }

 

 

IN the book, they're using Java and java doesn't have properties that i'm aware of. they use setters and getters. So i've extrapolated that anywhere fowler uses set or get i'm going to try to convert it to a property where easily applicable.

 

This particular property may be sort of big, but that will change soon :)

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Post a comment!
  1. Formatting options