CSC/ECE 517 Fall 2009/wiki 2 If SJ

From Expertiza_Wiki
Jump to navigation Jump to search

Refactoring If Statement with Polymorphism.

Design Consideration: Reuse and Change

One of the most important consideration of Object-Orientation Software Design is 'Reuse' and 'Change' [2][4]. In terms of reuse, we mean to build the new software with the building block from the old software. The importance of 'change' is from the fact that the software is always changing to keep alive. So that how to effectively make change to the source code is critical for the software design.

The Problem of If Statement

Under the design consideration we have discussed, the problem of if statement is quite obvious: 1. the conditional relationship in function level prevent us to reuse each of the function separately. We can only reuse the whole application as a unit. 2. when we want to change the functionality of a certain function or add a new function we have to trace to every implementation code. Such change is expensive and violate the OO principle 'code to interface instead of implementation'.

Example of If statement

public class Operator
{
    string operation;

    public int Execute(int x, int y)
    {
         switch(operation)
         {
             case "Add":
                 return x + y;
             case "Subtract":
                 return x - y;
             case "Multiply":
                 return x * y;
             case "Divide":
                 return x / y;
             default:
                 throw new InvalidOperationException("Unsupported operation");
         }
    }
}

In such example of conditional statement on operator, we see that: if we want to reuse function "add" operator in the other code segment, we have to reuse whole Class operator with the value "Add". And if we want to add a new operation "Power", we need to code in the implementation of Class Operator.

Idea of Polymorphism

Facing the problem of the conditional statement, people[2] come up with refactoring method called 'Polymorphism'. The idea of Polymorphism is to replace the highly functional relationship with the subclass override procedure under abstract class. So subclass is created to encapsulate each function in the conditional statement and the conditional relation is replaced by override a common abstract class. The advantage of such idea is to separate the functionality.

Definition of Polymorphism

According to [1], Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability of one type, A, to appear as and be used like another type, B.

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run time.

Example of Refactoring Conditional Statement with Polymorphism

  • Before refactoring
public class Operator
{
    string operation;

    public int Execute(int x, int y)
    {
         switch(operation)
         {
             case "Add":
                 return x + y;
             case "Subtract":
                 return x - y;
             case "Multiply":
                 return x * y;
             case "Divide":
                 return x / y;
             default:
                 throw new InvalidOperationException("Unsupported operation");
         }
    }
}
  • After refactoring
public abstract class Operator
{
    public abstract int Execute(int x, int y)
}

public class Add : Operator
{
    public override int Execute(int x, int y)
    {
        return x + y;
    }
}
public class Subtract : Operator
{
    public override int Execute(int x, int y)
    {
        return x - y;
    }
}
public class Multiply: Operator
{
    public override int Execute(int x, int y)
    {
        return x * y;
    }
}
public class Divide: Operator
{
    public override int Execute(int x, int y)
    {
        return x / y;
    }
}

References

[1] Polymorphism in object-oriented programming, http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

[2] Rene Santaolaya, Olivia G. Fragoso, Joaquín Pérez, Lorenzo Zambrano, Restructuring Conditional Code Structures Using Object Oriented Design Patterns, Lecture Notes in Computer Science.

[3] Refactoring conditional statement, http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism

[4] Object Orientation Design, http://en.wikipedia.org/wiki/Object-oriented_design