Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
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



Loremo - the 1.5 liter car coming in 2009




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 (2)


Simple example for the COMPOSITE Design PatternFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
OO-related
Skill Level:
Scoring:
Last Update:
03/19/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet CopyDirectory Copy File Files Directory Directories Composite Design Pattern
Times Scored:
11
Visits:
3797
Uploader: Jochen Fromm
Company:
Reference: N/A
 
Question/Problem/Abstract:
What is the Composite Design Pattern ? How do you copy a directory with all subdirectories in a simple way ?
Answer:




The COMPOSITE Design Pattern lets clients treat individual
objects and compositions of objects uniformly
(according to Gamma, Helm, Johnson and Vlissides
"Design Patterns", Addision-Wesley, 1995. Allthough
this book now is nearly 8 years old, it is even in
the new DOT NET World still important.)

The idea is to define a Composite Class

aComponentClass = class
   ..
   procedure DoSomething; virtual;
end;  
  

aCompositeClass = class(aComponentClass)
    aList: tList;  // List of "aComponentClass" Objects
    procedure DoSomething; override;
   ...
end;

Now you can call a single class method

aComponentClass.DoSomething;

in any case, whether the class is a composite class or not.
The method "DoSomething" of the CompositeClass for example
calls "DoSomething" for every item of the list.

This allows you to ignore the difference between compositions
of objects and individual objects.

A simple example for a composition is a directory : a
directory is a composition of files. A simple implementation
of a class that copies a file or a complete directory with
all subdirectories looks like this :


uses Classes,SysUtils,..;

  tFile = class
  public
    fName : string;
  public
    constructor Create(Name : string);
    procedure Copy(DstDir : string); virtual;

    property Name : string read fName;
  end;

  tDirectory = class(tFile)  
  private
    FileList : tList;
  public
    constructor Create(Name : string);
    destructor Destroy;
    procedure Copy(DstDir : string); override;

    property Name;
  end;



{ tFile }

constructor tFile.Create(Name: string);
begin
  fName:=Name;
end;

procedure tFile.Copy(DstDir: string);
var SrcFilename,DstFilename : string;
begin
  SrcFilename:=fName;
  DstFilename:=IncludeTrailingPathDelimiter(DstDir)+
               ExtractFilename(fName);
  if FileExists(SrcFilename) then
    Windows.CopyFile(PChar(SrcFilename),PChar(DstFilename),false);
end;

{ tDirectory }

procedure tDirectory.Copy(DstDir: string);
var i : integer;
    RelPath : string;
begin
  if not DirectoryExists(DstDir) then
    ForceDirectories(DstDir);

  for i:=0 to FileList.Count-1 do
   if tFile(FileList[i]) is tDirectory then
   begin
     RelPath:=ExtractRelativePath(IncludeTrailingPathDelimiter(Name),
                                  tDirectory(FileList[i]).Name);
     tDirectory(FileList[i]).Copy(DstDir+'\'+RelPath)
   end else
     tFile(FileList[i]).Copy(DstDir)
end;

constructor tDirectory.Create(Name: string);
var Root,s : string;
    sr : tSearchRec;
begin
  inherited Create(Name);

  FileList := tList.Create;

  Root:=IncludeTrailingPathDelimiter(Name);
  s:=Root+'*.*';
  if FindFirst(s, faAnyFile , sr) = 0 then
  begin
    repeat
      if (sr.Name = '.') or (sr.Name = '..') then continue;

      if ((sr.Attr and faDirectory) <> 0)
       then FileList.Add(tDirectory.Create(Root+sr.Name))
       else FileList.Add(tFile.Create(Root+sr.Name));
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

destructor tDirectory.Destroy;
var i : integer;
begin
  for i:=0 to FileList.Count-1 do
    tFile(FileList[i]).Destroy;
  FileList.Free;
end;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Patterns around the World
    Max Kleiner (Mar 20 2003 10:20AM)

As such it captures design experience of experienced programmers. A designer who is familiar with such patterns can apply them immediately to design problems (and code problems too, like code patterns) without having to rediscover them.
If you agree, I would like to publish your solution in my new book.

Respond

RE: Patterns around the World
Jochen Fromm (Mar 20 2003 10:56AM)

Yes, of course I agree.
Good luck with your new book.

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







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