Tuesday, July 6, 2010

Changing the Rules … in Appearance Only

Near the end of the previous chapter, I alluded to the fact that MATLAB gives us a way to tailor standard dot-reference syntax to suit the needs of our objects. In the eyes of our clients, dotreference tailoring makes an object look like a structure. This gives objects an enormous boost. If objects look like structures, using objects rather than structures is completely transparent. Transparency is a good thing because it gives us immediate access to a very powerful tool we can use to protect the integrity of our code, encapsulation.

The good news is that MATLAB allows dot-reference tailoring. In this chapter, we will develop a set of member functions that implement the tailoring in a way that allows objects to mimic structures. We will take advantage of a pair of standard, but relatively unknown, functions, subsref.m and subsasgn.m. The built-in versions operate on structures. Tailored versions, as long as MATLAB can find them, operate on objects. From Chapter 3, we know MATLAB will find them as long as they exist in the class directory as member functions. As tailored member functions, subsref.m and subsasgn.m are so critical to object-oriented programming that they share a place beside the constructor in the group of eight.

1. A SPECIAL ACCESSOR AND A SPECIAL MUTATOR
The reason these very important functions are not well-known is that outside the realm of object oriented programming, they are almost never called by name. MATLAB classifies these functions as operators in the same way it classifies symbols like +, -, /, and ~= as operators. There are many operators (see Table 4.1), but the distinguishing feature is syntax. When MATLAB encounters an operator, it orders the arguments and converts the operator’s symbol into a function call. Unless you understand what it means to be an operator, you might not realize what is going on behind the scenes. Shortly we will specifically examine subsref.m and subsasgn.m operators. First, let’s take a brief side trip to discuss operators and introduce a technique called operator overloading.

1.1 A SHORT SIDE TRIP TO EXAMINE OVERLOADING
Most of the symbols you can type from the keyboard have special meaning. The meanings behind +, -, /, and ~= are clear. These special symbols are called operators. When MATLAB interprets a line of code, it maps every operator to an m-file. Table 4.1 lists the mapping from symbol to mfile. From the command line, you can display a similar list using help ops. If you look at the list you see conversions like + => plus.m and <= => le.m. We can call these unctions by name, but we almost never do because operator syntax is a lot easier.

Stop for a minute and consider the implications. Every operator maps to an m-file, and the execution of every m-file is determined based on the search path. That means we can redefine the operation of any operator. All we have to do is create a new m-file with the same name as the operator and put it in a directory with a higher search-path priority. For objects, we simply put the tailored operator function in the class directory. Since MATLAB searches for member functions before it searches for built-in functions, the tailored function has higher priority. Clients use normal operator syntax, and MATLAB conveniently finds the appropriate function.



Object-oriented terminology calls this technique operator overloading, and every function in Table 1.1 can be overloaded. When MATLAB encounters operator syntax, it collects variables into argument lists and calls the operator’s function. Once you know the name of the operator’s function, you can display the function’s help information to get a description of the arguments. For example, the statement c = a + b; converts to the function equivalent c = plus(a, b);.

Operator overloading represents an important subset of general function overloading. General function overloading allows a class to customize the operation of virtually any function by including a tailored copy of the function in the class directory. Thus, any discussion of overloading involves two functions, the function doing the overloading and the function being overloaded. Let’s refer to the function doing the overloading as the tailored version and to the function being overloaded as the original version.
With very few exceptions, MATLAB allows a class to overload any function. The original function might exist as a built-in function or as a function on the path. If we jump ahead and consider inheritance, the original function might also exist as a parent-class function. In short, once a class overloads a function, the location of the original isn’t too important.

Given the ability to overload almost any function, you are also given the responsibility of doing it wisely. Unfortunately, this is an area where experience is the best guide. There are few hard-andfast rules, but there are some things to consider. Original functions are generally well understood, and overloading works best when the behavior of the tailored version can be inferred from the behavior of the original. The argument syntax of an original function is also well-known, and the tailored version should match the syntax very closely. This is particularly true for operator overloading because you can’t control the conversion from operator syntax to function call. The implementation examples and the group of eight represent a good resource for examining operator and function overloading. As we progress through the example code, you will be building experience.

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "Changing the Rules … in Appearance Only"

Post a Comment