BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Delphi Design Pattern Clarification

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
daniel
Guest





PostPosted: Mon Apr 03, 2006 2:03 pm    Post subject: Delphi Design Pattern Clarification Reply with quote



Hi,
I have been reading about design patterns in Delphi and looked at
examples on Jim's and Joanna's site and am a little confused now.

Do I need to use interfaces in Delphi < version 8, I can not see a
massive advantage in using interfaces over abstract classes apart from

1. Not being able to accidentally instantiate an interface like an
abstract class.

2. you can inherit from multiple interfaces, though I recall reading
somewhere that classes should only do one job and do it well, so not
sure if this is an advantage or not.

3 not needing to remember to release interface references as they are
reference counted.

Further into my reading of head first design patterns it seems to
digress from programming using interfaces to programming using abstract
classes, but Java has a garbage collector so no need to clean up objects
in Java but not so in not .Net versions of Delphi.

Are there any advantages to using interfaces over abstract classes in
Delphi < 8, and if so can anyone offer some compelling reasons to do so?

cheers
Back to top
Jim Cooper
Guest





PostPosted: Mon Apr 03, 2006 3:03 pm    Post subject: Re: Delphi Design Pattern Clarification Reply with quote



Quote:
Do I need to use interfaces in Delphi < version 8

You never absolutely need to. The GoF didn't, for example. However, sometimes
the solution is neater. So Observer (and similar occasions where many classes
are referenced by one other class) often benefit from interfaces as the solution
is neater. If you find yourself with code like this :

procedure TMyClass.DoSomethingWithObject(AnObject : TObject)
begin
if AnObject is TMyClass1 then begin
TMyClass1(AnObject).DoSomething;
end else if AnObject is TMyClass2 then begin
TMyClass2(AnObject).DoSomething;
...

end;

then you definitely have a candidate for interface use.

Quote:
2. you can inherit from multiple interfaces, though I recall reading
somewhere that classes should only do one job and do it well, so not
sure if this is an advantage or not.

This is my opinion too. You can have multiple interfaces on a class, but it is a
smell, I reckon.

Quote:
3 not needing to remember to release interface references as they are
reference counted.

The downside there is that object and interface references are difficult to
handle when mixed.

Quote:
can anyone offer some compelling reasons to do so?

If interfaces get you an more elegant solution then fine. Almost all the time
they do not offer any clear advantage. IMO you should then refrain from using
them either altogether, or defer their use until your design has been
sufficiently refactored. Refactoring with interfaces involved is more difficult
than without, most of the time.



Cheers,
Jim Cooper

_____________________________________________

Jim Cooper jcooper (AT) tabdee (DOT) ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi to your Palm
_____________________________________________
Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Mon Apr 03, 2006 4:03 pm    Post subject: Re: Delphi Design Pattern Clarification Reply with quote



daniel wrote:
Quote:

2. you can inherit from multiple interfaces, though I recall reading
somewhere that classes should only do one job and do it well, so not
sure if this is an advantage or not.

There was a very recent discussion here on just that. Like any feature, it
can be badly used, but my opinion is being able to aggregate functionality
via multiple interfaces can be extremely advantageous at times. It does not
detract from the idea of a class doing one thing well, in fact when used
correctly, it *enhances* that - the "one" thing a class does still typically
involves multiple steps, and each of those steps could potentially have
multiple ways of being performed depending on some criteria. Using
interfaces allows that class to use other classes interchangeably to perform
those things.

In other words, if you design a class that directly implements, say, 5
interfaces that can all be used independently from each other - as though
this one class were actually 5 different classes, then *that is definitely a
bad smell*. If the functionality of the class is a logically cohesive
merging of that separate functionality, then that is fine.

However, that to me is not the main use of multiple interfaces. The more
important use is to allow that one class to delegate some of its
functionality to other classes, but without caring about the type of that
class as long as it implements the expected functionality. The difference
here from using abstract classes is that usable descendants are tied to a
specific class type, interfaces aren't. In Delphi, using an interface means
one can transparently call into another Delphi class, or a COM object
written in another language, and your class doesn't know or care.

Quote:
3 not needing to remember to release interface references as they are
reference counted.

Trivial advantage, not enough on its own. It also requires correct use - not
mixing object and interface use. It also only works if you descend from
TInterfaceObject or implement your own IInterface methods.

Personally, I use interfaces "sparingly". There are times when the situation
seems to stand out as a place where I should use an interface, and in those
cases they usually pay off.

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"The only reason some people get lost in thought is because it's
unfamiliar territory." - Paul Fix
Back to top
sanmaotuo_33@yahoo.com
Guest





PostPosted: Thu Apr 06, 2006 10:03 am    Post subject: Re: Delphi Design Pattern Clarification Reply with quote

I Like Interface:)
The More You Use Interface ,the More Advantage of Interface Will Be.
I Define Each BO Using Interface In My Application.

My Thought is From Joanna Cater's MVP Articles.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.