CSC/ECE 517 Fall 2009/wiki2 4 va: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 49: Line 49:
end
end
</pre>
</pre>
One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles grows further. For instance, let's imagine we decided to add two new vehicle types, Motorcycle and Bicycle. In order to support this addition, we would need to add two new classes, as well as to modify the IF statement (or, better to replace it with a more appropriate conditional, such as case.)


== Use of polymorphism ==
== Use of polymorphism ==

Revision as of 15:16, 9 October 2009

Introduction

The IF statement, a conditional language structure, has been available to programmers for quite some time. It was first introduced in FORTRAN in a form of an arithmetic IF statement back in 1957. The control of the program would be redirected to one of the three labels, depending on the value of the analyzed arithmetic expression (which could be negative, positive, or zero). Over the years, this language element had become obsolete and was replaced by a logical IF statement (if-then-else), the one that is still widely used to this day.

The logical IF statement is very familiar to most programmers as it is one of the first language structures they learn. Despite that, it is a cause of many code errors. Being more suitable for structured programming, most today's object-oriented languages have more sophisticated mechanisms to replace the IF statement. One of such approaches is a use of polymorphism.

This paper will discuss ways in which a polymorphism technique can be applied to replace the IF control structures, pros and cons of the both, and will demonstrate some examples written in Ruby language.

Use of IF statements

Let's consider a simple code that would illustrate a use of IF statement in an object-oriented code. First, we define a parent class, Vehicle, and two child classes - Car and Truck. We would then ask a user to choose which of the two classes they would like to initialize. Depending on their selection, the code would look up and suggest a type of fuel that would work with the selected vehicle.

Below is a Ruby code that implements the above utilizing the IF statement:

class Vehicle
  def initialize
    puts "I am a vehicle"
  end
end

class Car < Vehicle
  def initialize
    super
    puts "I am a passenger car"
  end
end

class Truck < Vehicle
  def initialize
    super
    puts "I am a commercial truck"
  end 
end

class Driver
  puts "Do you have a Car or a Truck?" 
  #Expects Car or Truck, case sensitive
  type = gets             
  my_car = eval(type).new

  if my_car.class == Car
    puts "I like 87 octane gasoline"
  elsif my_car.class == Truck
    puts "I like diesel"
  end
end

One apparent downside of this solution is that the code is not easily maintainable if the selection of vehicles grows further. For instance, let's imagine we decided to add two new vehicle types, Motorcycle and Bicycle. In order to support this addition, we would need to add two new classes, as well as to modify the IF statement (or, better to replace it with a more appropriate conditional, such as case.)

Use of polymorphism

class Vehicle
  def initialize
    puts "I am a vehicle"
  end
end

class Car < Vehicle
  def initialize
    super
    puts "I am a passenger car"
  end
  
  def get_type_of_fuel
    "I like 87 octane gasoline"
  end
end

class Truck < Vehicle
  def initialize
    super
    puts "I am a truck"
  end
  
  def get_type_of_fuel
    "I like diesel"
  end
end

class Driver
  puts "Do you have a Car or a Truck?" 
  #Expects Car or Truck, case sensitive
  type = gets
  my_car = eval(type).new
  puts my_car.get_type_of_fuel  
end

Conclusion

xxxxx

Links

[1] http://en.wikipedia.org/wiki/Arithmetic_IF