Overriding in Owner Class
From Andrey
Revision as of 04:19, 4 February 2006 Andrey (Talk | contribs) ← Previous diff |
Revision as of 04:20, 4 February 2006 Andrey (Talk | contribs) Next diff → |
||
Line 7: | Line 7: | ||
= Abstract = | = 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 paper 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. These pages introduce 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 = | = Introduction = |
Revision as of 04:20, 4 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. These pages introduce 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. It may be necessary 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. This approach ties together the otherwise independent classes of owner and member through a pointer and notifications; forcing the member class to know about the owner class. 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. This will create a new class, giving it access rights to the member class's internals, coupling it with the owner class, and possibly giving it knowledge of the owner class 's internals. You could also derive the owner class from the member class. However, this would make the owner an instance of the member class (is-a), instead of intended usage of the member (has-a). This may become complicated if the owner class wants to control more than one member belonging to 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 well-known programming problems in “Applications”; some analysis will be done in “Comparison with the Alternatives” and “Questions”; an implementation for C++ in “Inside the Overriding Mechanism”. And then I will conclude with some summarizing thoughts. By the way, the use of C++ in this text should not imply that the ideas cannot be used in other object-oriented programming languages.