Monday, May 22, 2006

The concept of Delegate (2)

Delegate is callback mechanism in essence. You can utilize all advantage of callback programming by Delegate. For the possibility of the exchange, class inheritance is useful as well as Delegate in XOOPS Cube. Therefore, separating class inheritance and Delegate is very important. For that purpose, you need to understand the difference of class inheritance and Delegate.

For example... See the following class:



  1. This class that is called "PMMonitor" lists up users that have unread PM for a week, and sends the mail to them.
  2. The code that uses this class is included to the block.
  3. That function becomes active by putting the block to the site.

In other words, the Program Counter goes through (executes) the code of this class.

Well, you think that if this class can send CC to administrators, it becomes more useful for you. And, you define the sub-class and override the base class to send CC.



But, it doesn't work. Your sub class isn't called from anybody, because other code doesn't know the name of your sub-class that you created as new class. In other words, your sub-class is never gone through by Program Counter. Until you replace the old code that creates the instance of the base class by the new code that creates the instance of the sub class, your sub class doesn't work.

Class inheritance brings the possibility of exchanging functions to developer in the program. But it needs the change of source code. If PMMonitor has Delegate, you can use it for exchanging function or adding functions. In this instance, you don't need to define the sub class and change the source code of other modules.

Given that the class that has Delegate is "PMMonitorDash". PMMonitorDash substitutes Delegate for the fixed function. Therefore, if you register your function to the delegate of PMMonitorDash, your code will work in PMMonitorDash.



You can replace the basic class of XOOPS Cube for customizing. It is the most drastic ameliorative measures but heavy work. Users may call it a last resort. In that regard, Delegate will become the easiest ameliorative measures in XOOPS Cube.

No comments: