天天看點

《GOF設計模式》—組合(COMPOSITE)—Delphi源碼示例:繪圖編輯器

示例:繪圖編輯器

說明:

《GOF設計模式》—組合(COMPOSITE)—Delphi源碼示例:繪圖編輯器

代碼:

unit uGraphic;

interface

uses

    Windows,SysUtils,Classes,Graphics,Types;

type

    TGraphic = class

    protected

        FCanvas: TCanvas;

        function GetChilds(Index: integer): TGraphic; virtual;

        function GetCount: Integer; virtual;

    public

        constructor Create(ACanvas: TCanvas);

        //---

        procedure Draw; virtual; abstract;

        procedure Add(AGraphic: TGraphic); virtual;

        procedure Remove(AGraphic: TGraphic); virtual;

        //---

        property Childs[Index: integer]: TGraphic read GetChilds;

        property Count: Integer read GetCount;

    end;

    TPicture = class(TGraphic)

    private

        FList: TList;

        procedure Clear;

    protected

        function GetChilds(Index: integer): TGraphic; override;

        function GetCount: Integer; override;

    public

        constructor Create(ACanvas: TCanvas);

        destructor Destroy; override;

        //---

        procedure Draw; override;

        procedure Add(AGraphic: TGraphic); override;

        procedure Remove(AGraphic: TGraphic); override;

    end;

    TLine = class(TGraphic)

    public

        procedure Draw; override;

    end;

    TRectangle = class(TGraphic)

    public

        procedure Draw; override;

    end;

    TText = class(TGraphic)

    public

        procedure Draw; override;

    end;

implementation

constructor TGraphic.Create(ACanvas: TCanvas);

begin

    FCanvas := ACanvas;

end;

procedure TGraphic.Add(AGraphic: TGraphic);

begin

    raise Exception.Create('不支援該方法');

end;

procedure TGraphic.Remove(AGraphic: TGraphic);

begin

    raise Exception.Create('不支援該方法');

end;

function TGraphic.GetChilds(Index: integer): TGraphic;

begin

    Result := nil;

end;

function TGraphic.GetCount: Integer;

begin

    Result := 0;

end;

procedure TPicture.Clear;

var

    i: Integer;

begin

    with FList do

    begin

        for i := 0 to Count - 1 do

            TObject(Items[i]).Free;

        //---

        Clear;

    end;

end;

constructor TPicture.Create(ACanvas: TCanvas);

begin

    inherited;

    //---

    FList := TList.Create;

end;

destructor TPicture.Destroy;

begin

    self.Clear;

    FList.Free;

    //---

    inherited;

end;

procedure TPicture.Add(AGraphic: TGraphic);

begin

    FList.Add(AGraphic);

end;

function TPicture.GetChilds(Index: integer): TGraphic;

begin

    Result := FList[Index];

end;

function TPicture.GetCount: Integer;

begin

    Result := FList.Count;

end;

procedure TPicture.Remove(AGraphic: TGraphic);

begin

    FList.Remove(AGraphic)

end;

procedure TPicture.Draw;

var

    i: integer;

begin

    for i := 0 to self.Count - 1 do

        self.Childs[i].Draw;

end;

procedure TLine.Draw;

begin

    with FCanvas do

    begin

        with Pen do

        begin

            Color := clRed;

            Style := psSolid;

            Width := 2;

        end;

        //---

        MoveTo(20,20);

        LineTo(60,60);

    end;

end;

procedure TRectangle.Draw;

begin

    with FCanvas do

    begin

        with Pen do

        begin

            Color := clRed;

            Style := psSolid;

            Width := 2;

        end;

        //---

        Rectangle(20,20,80,80);

    end;

end;

procedure TText.Draw;

begin

    with FCanvas do

    begin

        Font.Color := clred;

        //---

        TextOut(20,30, '123');

    end;

end;

end.

procedure TForm1.Button1Click(Sender: TObject);

    //---

    function _GetGraphic1:TGraphic;

    begin

        Result := TLine.Create(self.Canvas);

    end;

    //---

    function _GetGraphic2:TGraphic;

    begin

        Result := TPicture.Create(self.Canvas);

        with Result do

        begin

            Add(TRectangle.Create(self.Canvas));

            Add(TText.Create(self.Canvas));

        end;

    end;

var

    AGraphic:TGraphic;

begin

    AGraphic := _GetGraphic2;

    try

        AGraphic.Draw;

    finally

        AGraphic.Free;

    end;

end;

繼續閱讀