こちらには初めて投稿します。ひらぽん@エース設計と申します。
現在、結城さんの本と Gofと さらにブリシデスの書いた
「パターンハッチング」を並べて、デザパタの勉強中の身です。
ただし読んでてどうにも苦しいのが・・・
普段 Delphi を使ってるだけに、言語仕様の違いで、
JAVA やC++ の例をどうDelphiに実装するか・・・
頭を痛めるケースが多々あったりします。
で、どうせなら各サンプルをDelphiで実装しなおしたものを
後学の為にも、このMLおよび、Delphi-MLで公開したいと考えてるのですが
果たしてそれって許されるのでしょうか。(^^;
ちなみに『 Iterator 』パターンのサンプルをDelphiで実装したら
こうなりました。(^^;
クラス・インターフェイス・フィールドの名前はDelphiでの慣例に従い
クラス名には「T」、インターフェイスには「I」、
フィールドには「F」を先頭に付けてます。
ソースは突っ込み大歓迎です。(^^;
// Main.dpr ========================================================
program Main;
{$APPTYPE CONSOLE}
uses
Iterator in 'Iterator.pas';
var
bookShelf: TBookShelf;
book: TBook;
it: IIterator;
begin
bookShelf := TBookShelf.Create;
bookShelf.appendBook( TBook.Create( '徳川家康' ) );
bookShelf.appendBook( TBook.Create( '燃えよ剣' ) );
bookShelf.appendBook( TBook.Create( '坂の上の雲' ) );
bookShelf.appendBook( TBook.Create( '写真 太平洋戦争' ) );
bookShelf.appendBook( TBook.Create( '小説吉田学校' ) );
it := bookShelf.iterator;
while ( it.hasNext() ) do
begin
book := TBook( it.next() );
Writeln( book.getName );
book.Free;
end;
bookShelf.Free;
Readln;
end.
// Iterator.pas ====================================================
unit Iterator;
interface
uses
classes;
{ Aggreagte インターフェイス }
type
IAggreagte = interface
['{A3F20B7C-B48C-4D44-927D-840A6C0DBFC8}']
function Iterator: IIterator;
end;
{ Iterator インターフェイス }
type
IIterator = interface
['{26892D0A-E2F3-4767-8E39-C28FF20F55C2}']
function hasNext: Boolean;
function next: TObject;
end;
{ Book クラス }
type
TBook = class( TObject )
private
Fname: string;
public
constructor Create( name: string );
function getName: string;
end;
{ BookShelf クラス }
type
TBookShelf = class( TInterfacedObject, IAggreagte )
private
Fbooks: TStringList;
public
constructor Create;
destructor Destroy; override;
function getBookAt( index: integer ): TBook;
procedure appendBook( book: TBook );
function getLength: integer;
function iterator: IIterator;
end;
{ BookShelfIterator クラス }
type
TBookShelfIterator = class( TInterfacedObject, IIterator )
private
FbookShelf: TBookShelf;
Findex: integer;
public
constructor Create( const bookshelf: TBookShelf );
function hasNext: Boolean;
function next: TObject;
end;
implementation
{ TBook }
constructor TBook.Create( name: string );
begin
Fname := name;
end;
function TBook.getName: string;
begin
Result := Fname;
end;
{ TBookShelf }
procedure TBookShelf.appendBook( book: TBook );
begin
Fbooks.AddObject( '', book );
end;
constructor TBookShelf.Create;
begin
Fbooks := TStringList.Create;
end;
destructor TBookShelf.Destroy;
begin
Fbooks.Free;
inherited;
end;
function TBookShelf.getBookAt(index: integer): TBook;
begin
Result := TBook( Fbooks.Objects[ index ] );
end;
function TBookShelf.getLength: integer;
begin
Result := Fbooks.Count;
end;
function TBookShelf.iterator: IIterator;
begin
Result := TBookShelfIterator.Create( Self );
end;
{ TBookShelfIterator }
constructor TBookShelfIterator.Create( const bookshelf: TBookShelf );
begin
Fbookshelf := bookshelf;
Findex := 0;
end;
function TBookShelfIterator.hasNext: Boolean;
begin
if ( Findex < FbookShelf.getLength() ) then
Result := True
else
Result := False;
end;
function TBookShelfIterator.next: TObject;
begin
Result := FbookShelf.getBookAt( Findex );
Inc( Findex );
end;
end.
////////////////////////////////////////////////////////////
有限会社 エース設計
Mail ace@…
Home Page http://www.acesekkei.com/
////////////////////////////////////////////////////////////