Overriding in Owner Class

From Andrey

(Difference between revisions)
Revision as of 05:48, 3 February 2006
Andrey (Talk | contribs)

← Previous diff
Revision as of 05:49, 3 February 2006
Andrey (Talk | contribs)

Next diff →
Line 1: Line 1:
-Title: Overriding in Owner Class.+'''''An extension of polymorphism allowing for better integration of objects for common work.'''''
-Author: Andrey Potekhin+
-Subject areas: Object-oriented design techniques; C++ language design; theoretical foundations.+
- +
-''An extension of polymorphism allowing for better integration of objects for common work.''+
Andrey Potekhin Andrey Potekhin
-''Abstract''+Subject areas: Object-oriented design techniques; C++ language design; theoretical foundations.
 + 
 += Abstract =
Classes in C++ are devoid of an important quality: the class is not aware of operations performed on its member objects by code outside of the class. A member of a class may serve as an argument of some algorithm, it may be a widget exposed to the end user, or can be manipulated by other members of the class. The changes happening to the member may require owner class attention. To enable the owner to see these changes, usually either the code of the member's class is modified to send notifications, or a special wrapper for the member is created. This article introduces an alternative way of providing control over members: an extension of polymorphism that allows one to override a virtual function of member's class in the owner class. This can help objects to work together; the designs may benefit by minimizing class and run-time dependencies, resolving the update issues and improving reusability of code. Classes in C++ are devoid of an important quality: the class is not aware of operations performed on its member objects by code outside of the class. A member of a class may serve as an argument of some algorithm, it may be a widget exposed to the end user, or can be manipulated by other members of the class. The changes happening to the member may require owner class attention. To enable the owner to see these changes, usually either the code of the member's class is modified to send notifications, or a special wrapper for the member is created. This article introduces an alternative way of providing control over members: an extension of polymorphism that allows one to override a virtual function of member's class in the owner class. This can help objects to work together; the designs may benefit by minimizing class and run-time dependencies, resolving the update issues and improving reusability of code.
Line 21: Line 19:
This approach will be described in the section called “Definition”; its applications to some serious programming problems in “Applications”; some analysis will be done in “Comparison with the Alternatives” and “Questions” sections; an implementation for C++ in “Inside the Overriding Mechanism”; and then I will conclude with some summarizing thoughts. The use of C++ in this article should not imply that the ideas cannot be used in other object-oriented programming languages. This approach will be described in the section called “Definition”; its applications to some serious programming problems in “Applications”; some analysis will be done in “Comparison with the Alternatives” and “Questions” sections; an implementation for C++ in “Inside the Overriding Mechanism”; and then I will conclude with some summarizing thoughts. The use of C++ in this article should not imply that the ideas cannot be used in other object-oriented programming languages.
-[[Definition of Overriding in Owner Class]]+Next: [[Definition of Overriding in Owner Class]]

Revision as of 05:49, 3 February 2006

An extension of polymorphism allowing for better integration of objects for common work.

Andrey Potekhin

Subject areas: Object-oriented design techniques; C++ language design; theoretical foundations.

Abstract

Classes in C++ are devoid of an important quality: the class is not aware of operations performed on its member objects by code outside of the class. A member of a class may serve as an argument of some algorithm, it may be a widget exposed to the end user, or can be manipulated by other members of the class. The changes happening to the member may require owner class attention. To enable the owner to see these changes, usually either the code of the member's class is modified to send notifications, or a special wrapper for the member is created. This article introduces an alternative way of providing control over members: an extension of polymorphism that allows one to override a virtual function of member's class in the owner class. This can help objects to work together; the designs may benefit by minimizing class and run-time dependencies, resolving the update issues and improving reusability of code.

Introduction

Consider a spell check algorithm making changes to objects representing text. The class representing a text document passes its member text for correction. Because it must reflect the changes to the text, it must know what the spell checker has done to the text. In general, it is often desirable for a class to control the changes that happen to its member objects. There are several ways to make owner class aware of changes to a member. You can hide the member and embed its interface in the interface of the owner class. The drawback to this is the necessity to rewrite the code working with member class to start working with the owner class. Alternatively, you can declare a pointer to the owner object inside the member class, and pass notifications about changes to the owner. The drawback to this approach is the tying together of the otherwise independent classes of owner and member through a pointer and notifications; forcing the member class to know about the owner class, which reduces the member class's reusability. You can also change the member to belong to a class derived from the original member class and implementing the code necessary for notifying the owner class. The drawbacks are: creating another class in the system, giving this class access rights to the member class's internals, coupling the new class with the owner class, and possibly giving the new class knowledge of the owner class 's internals. You can also derive the owner class from the member class. This makes the owner an instance of the member class (is-a), instead of intended usage of the member (has-a). This is also is not applicable if the owner class wants to control more than one member of the same class. In an attempt to find a better solution, I decided to re-formulate the problem:

Given a class and its members, it is desirable that when a call is made to a member function of a member, some function of the owner class is called.

Following the analogy with the usual overriding of a method in the derived class, I propose to allow the possibility to override a method in the enclosing class. This approach will be described in the section called “Definition”; its applications to some serious programming problems in “Applications”; some analysis will be done in “Comparison with the Alternatives” and “Questions” sections; an implementation for C++ in “Inside the Overriding Mechanism”; and then I will conclude with some summarizing thoughts. The use of C++ in this article should not imply that the ideas cannot be used in other object-oriented programming languages.

Next: Definition of Overriding in Owner Class