Board index » delphi » Accessing a subset of class functionality

Accessing a subset of class functionality


2008-05-13 04:04:23 AM
delphi276
Hi,
Suppose, there are two distinct classes TClassA and TClassB. TClassB
needs access to the functionality provided by TClassA. The requirement
is to use only one instance of TClassA during the whole lifetime of
instance of TClassB, therefore TClassB has a constructor that takes a
reference to TClassA instance as a parameter:
constructor TClassB.Create(AClassA: TClassA);
begin
...
FClassA := AClassA;
...
end;
procedure TClassB.Foo;
var
I: Integer;
begin
...
FClassA.SomeMethod;
I := FClassA.SomeIntegerProperty;
...
end;
This apparently works, but...TClassB does not need to access *entire*
functionality of TClassA, but rather some specific *subset* of it.
Furthermore TClassB should not be able to access other
methods/properties that do not belong to the above mentioned subset.
The more I think about it, the more I tend to replace the parameter of
TClassB constructor with another type of object reference, that
provides only the specific subset of TClassA's functionality that
TClassB needs to access.
Are my concerns legitimate or would you classify them rather as a
nitpicking? If the former is the case, what technique do you use in
order to achieve the above goal?
Thank you in advance for your comments. If you need a more concrete
example of what I am talking about, just please let me know and I'll
provide you with one.
--
Ivo Bauer
 
 

Re:Accessing a subset of class functionality

On Mon, 12 May 2008 22:04:23 +0200, Ivo Bauer writes:
Quote
what technique do you use in
order to achieve the above goal?
There are basically two ways to achieve this an abstract base class or
an interface. The latter is probably better but is more difficult to
implement in Win32.
Another way would be to provide some sort of helper methods in TClassB
to access only the methods you want in TClassA and then hide the
reference to TClassA (Either by storing it in a field of type TObject
or hiding it as a private field in a base class).
Quote
Are my concerns legitimate or would you classify them rather as a
nitpicking?
I think it depends on the size of your project, who is going to
maintain it and if the TClassB is a class maintained by a third party.
You would have to take all of these into account and balance them with
the added complexity of whatever you implement.
I probably wouldn't be concerned for most of my projects and just
trust developers to be disciplined enough. Many of the popular dynamic
languages like Ruby have much more relaxed rules for object access and
yet they are still being used in complex projects.
--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
 

Re:Accessing a subset of class functionality

Option 1: ClassA should implement an interface which is passed to ClassB's
constructor.
Option 2: Create an intermediate class that accepts an instance of ClassA
and delegates the work to it. Then you can pass this intermediate instance
to ClassB instead and it will have access only to the subset exposed.
Pete
 

Re:Accessing a subset of class functionality

Ivo Bauer writes:
<sniP>
Quote
Are my concerns legitimate or would you classify them rather as a
nitpicking? If the former is the case, what technique do you use in
order to achieve the above goal?
Another technique I didn't see mentioned was this:
ObjectB.OnGetItem := ObjectA.GetItem;
ObjectB.OnsetItem := ObjectA.SetItem;
etc.