delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


How do we implement the Choice PatternGo to Max Kleiner's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Working with Interfaces
Product:
Delphi 3.x (or higher)
Category:
OO-related
Skill Level:
Scoring:
Last Update:
03/29/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Choice-Pattern Design-Patterns Interfaces Association
Times Scored:
5
Visits:
3370
Uploader: Max Kleiner
Company: kleiner kommunikation
Reference: N/A
 
Question/Problem/Abstract:
The Choice Design Pattern is new and relies on interfaces and runtime associations. No aggregation or composition between classes are needed.
Answer:



The Choice Pattern needs one interface, n-classes which supports the interface and a worker-class to provide the choice of an algorithm at runtime. The Choice Pattern is like the Strategy Pattern, but smaller and more runtime in his behaviour.

With interfaces we don't have to concern about memory management.
Interface references are managed through reference-counting, which depends on the _AddRef and _Release methods inherited from IUnknown. When an object is referenced only through interfaces, there is no need to destroy it manually; the object is automatically destroyed when the last reference
to it goes out of scope.

The following restrictions apply.
- The member List can include only methods and properties.
- Fields are not allowed in interfaces.
- Interfaces have no constructors or destructors. They cannot be instantiated,     except through classes that implement their methods.

Methods cannot be declared as virtual, dynamic, abstract, or override. Since interfaces do not implement their own methods, these bindings have no meaning. So let's practice the Choice Pattern in 5 steps:


1.  We need an Interface in order to be type-compatible.

IChoicePattern = Interface
    procedure doPatternSearch;
  end;


2. We declare 2 or n classes.
A class from an Interface can support/implement multiple interfaces. TInterfacedObject implements the methods of IUnknown, so TInterfacedObject automatically handles reference counting and memory management of interfaced objects.
One of the concepts behind the design of interfaces is ensuring the lifetime management of the objects that implement them. The AddRef and Release methods of IUnknown provide a way of implementing this functionality.


  TCheckFormatA = class(TInterfacedObject, IChoicePattern)
  public
    procedure doPatternSearch;
  end;

  TCheckFormatB = class(TInterfacedObject, IChoicePattern)
  public
    procedure doPatternSearch;
  end;


3. We need a worker-class which calls the runtime Interface-methods:

  TDirWorker = class
    procedure callCheck(myInst: IChoicePattern);
  end;



4. Now we implement the Interface Classes and the Worker Class too:

procedure TCheckFormatA.doPatternSearch;
var ldbPath: string;
begin
if FileExists(extractFileDir(application.exeName)+'\'+ DBNAME) then
   ldbPath:=extractFileDir(application.exeName)+'\'+DBNAME;
   messageDlg('formatASearch doing', mtInformation,[mbok],0);
end;


procedure TCheckFormatB.doPatternSearch;
var ldbPath: string;
begin
   if OpenDialog1.Execute then
   ldbPath:=openDialog1.FileName;
   messageDlg('formatBSearch doing', mtInformation,[mbok],0);
end;


procedure TDirWorker.callCheck(myInst: IChoicePattern);
begin
   myInst.doPatternSearch;
   messageDlg('do_some_Work', mtInformation,[mbok],0);
end;

Here we can see, no myInst.Free is needed. Each object from TCheckFormatA or TCheckFormatB is automatically destroyed. Interfaces track the lifetime of an object by incrementing the reference count on the object when an interface reference is passed, and will destroy the object when that reference count is zero.  


5. At least the client calls the Choice Pattern and every object is local at runtime:

procedure TMainFrm.Button1Click(Sender: TObject);
begin
  with(TDirWorker.Create) do begin
    callCheck(TCheckFormatA.create);
    callCheck(TCheckFormatB.create);
   Free;
  end;
end;

Have fun and choice with OP (Delphi, Kylix and FreePascal)





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)