The Delphi Language, Part 2 - Visibility Specifiers, Friend Classes and Class Helpers
(Page 10 of 14 )
Visibility Specifiers The Delphi language offers you further control over the behavior of your objects by enabling you to declare fields and methods with directives such as private, strict private, protected, strict protected, public, and published. The syntax for using these directives is as follows:
TSomeObject = class
private
APrivateVariable: Integer;
AnotherPrivateVariable: Boolean;
strict private
procedure AStrictPrivateMethod;
protected
procedure AProtectedProcedure;
function ProtectMe: Byte;
strict protected
procedure AStrictProtectedMethod;
public
constructor APublicContructor;
destructor Destroy; override; // a public destructor
published
property AProperty: Integer
read APrivateVariable write APrivateVariable;
end;
You can place as many fields or methods as you want under each directive. Style dictates that you should indent the specifier the same as you indent the classname. The meanings of these directives follow:
private—These parts of your object are accessible only to code in the same unit as your object's implementation. Use this directive to hide implementation details of your objects from users and to prevent users from directly modifying sensitive members of your object.
strict private—Members are accessible within the declaring class and not within the same unit. Used for more strict data encapsulation than private.
protected—Your object's protected members can be accessed by descendants of your object. This capability enables you to hide the implementation details of your object from users while still providing maximum flexibility to descendants of your object.
strict protected—Members are accessible only within the declaring class and ancestors and not within declaring units. Used for more strict data encapsulation than protected.
public—These fields and methods are accessible anywhere in your program. Object constructors and destructors always should be public.
published—Identical to public from a visibility standpoint. Published has the additional benefit of adding the [Browsable(true)] attribute to properties contained within, which causes them to be displayed in the Object Inspector when used in the form designer. Attributes are discussed later in this chapter.
Note - The meaning of published represents a fairly substantial departure from the Win32 implementation of the Delphi language. In Win32, Runtime Type Information (RTTI) was generated for published properties. The .NET equivalent of RTTI is Reflection; however, Reflection is possible on all class elements, regardless of visibility specifier.
Here, then, is code for the TMyObject class that was introduced earlier, with directives added to improve the integrity of the object:
TMyObject = class
private
SomeValue: Integer;
procedure SetSomeValue(AValue: Integer);
published
property Value: Integer read SomeValue write SetSomeValue;
end;
procedure TMyObject.SetSomeValue(AValue: Integer);
begin
if SomeValue <> AValue then
SomeValue := AValue;
end;
Now, users of your object will not be able to modify the value of SomeValue directly, and they will have to go through the interface provided by the property Value to modify the object's data.
"Friend" Classes The C++ language has a concept of friend classes (that is, classes that are allowed access to the private data and functions in other classes). This is accomplished in C++ using the friend keyword. .NET languages, such as Delphi and C#, have a similar concept, although the implementation is different. Non- strict, private, and protected members of a class are visible and accessible to other classes and code declared within the same unit namespace.
Class Helpers Class helpers provide a means to extend a class without modifying the actual class. Instead, a new class, the helper, is created and its methods effectively bolted on to the original class. This enables users of the original class to call methods of the helper class as if they were methods of the original class.
The following code example creates a simple class and class helper and demonstrates calling a method on the helper class:
program Helpers;
{$APPTYPE CONSOLE}
type
TFoo = class
procedure AProc;
end;
TFooHelper = class helper for TFoo
procedure AHelperProc;
end;
{ TFoo }
procedure TFoo.AProc;
begin
WriteLn('TFoo.AProc');
end;
{ TFooHelper }
procedure TFooHelper.AHelperProc;
begin
WriteLn('TFooHelper.AHelperProc');
AProc;
end;
var
Foo: TFoo;
begin
Foo := TFoo.Create;
Foo.AHelperProc;
end.
Caution - Class helpers are an interesting feature, but not a feature that generally lends itself to good software design. This feature is in the language mostly because Borland needed it to hide the differences between standard .NET classes and similar classes in Win32 Delphi. The occasion should be rare when a good design involves the use of class helpers.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Nested Types, Operator Overloading and Attributes >>
More .NET Articles
More By Xavier Pacheco