《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:组合接口

示例:组合接口

说明:

1)、定义

将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。

2)、结构

 

组合:

Component:为组合中的对象声明接口。在适当的情况下,实现所有类共有接口的缺省行为。声明一个接口用于访问和管理Component的子组件。在递归结构中定义一个接口,用于访问一个父部件,并在合适的情况下实现它(可选)

Leaf:在组合中表示叶节点对象,叶节点没有子节点。在组合中定义图元对象的行为。

Composite:定义有子部件的那些部件的行为。存储子部件。在Component接口中实现与子部件有关的操作。

客户端:

Client:通过Component接口操纵组合部件的对象。

协作:

用户使用Component类接口与组合结构中的对象进行交互。如果接收者是一个叶节点,则直接处理请求。如果接收者是Composite,它通常将请求发送给它的子部件,在转发请求之前与/或之后可能执行一些辅助操作。

 

代码:

 

unit uComposite;

 

interface

 

uses

    Dialogs;

 

type

    TComponent = class

    public

        procedure Operation; virtual; abstract;

        procedure Add(AComponent: TComponent); virtual;

        procedure Remove(AComponent: TComponent); virtual;

        function GetChild(const AIndex: integer): TComponent; virtual;

    end;

    TLeaf = class(TComponent)

    public

        procedure Operation; override;

    end;

    TComposite = class(TComponent)

    private

        FChilds: array of TComponent;

    public

        constructor Create;

        destructor Destroy; override;

        //---

        procedure Operation; override;

        procedure Add(AComponent: TComponent); override;

        procedure Remove(AComponent: TComponent); override;

        function GetChild(const AIndex: integer): TComponent; override;

    end;

 

implementation

 

procedure TComponent.Add(AComponent: TComponent);

begin

end;

 

procedure TComponent.Remove(AComponent: TComponent);

begin

end;

 

function TComponent.GetChild(const AIndex: integer): TComponent;

begin

    Result := nil;

end;

 

procedure TLeaf.Operation;

begin

    ShowMessage('Leaf');

end;

 

constructor TComposite.Create;

begin

    SetLength(FChilds,0);

end;

 

destructor TComposite.Destroy;

    //---

    procedure _Clear;

    var

        i: integer;

    begin

        for i := low(FChilds) to high(FChilds) do

            FChilds[i].Free;

        SetLength(FChilds,0);

    end;

begin

    _Clear;

    //---

    inherited;

end;

 

procedure TComposite.Add(AComponent: TComponent);

var

    ACount: integer;

begin

    ACount := Length(FChilds);

    SetLength(FChilds,ACount + 1);

    FChilds[ACount] := AComponent;

end;

 

procedure TComposite.Remove(AComponent: TComponent);

    //---

    function _IndexOf: integer;

    var

        i: integer;

    begin

        for i := low(FChilds) to high(FChilds) do

        begin

            if FChilds[i] = AComponent then

            begin

                Result := i;

                exit;

            end;

        end;

        //---

        Result := -1;

    end;

    //---

    procedure _Delete(AIndex: integer);

    var

        ACount: integer;

    begin

        ACount := Length(FChilds);

        if AIndex < ACount - 1 then

            Move(FChilds[AIndex + 1],FChilds[AIndex],(ACount - AIndex - 1) * 4);

        SetLength(FChilds,ACount - 1);

    end;

var

    AIndex: integer;

begin

    AIndex := _IndexOf;

    if AIndex >= 0 then

        _Delete(AIndex);

end;

 

function TComposite.GetChild(const AIndex: integer): TComponent;

begin

    Result := FChilds[AIndex];

end;

 

procedure TComposite.Operation;

var

    i: integer;

begin

    ShowMessage('Composite');

    //---

    for i := low(FChilds) to high(FChilds) do

        FChilds[i].Operation;

end;

 

end.

 

procedure TForm1.Button1Click(Sender: TObject);

var

    AComposite:TComponent;

begin

    AComposite := TComposite.Create;

    try

        AComposite.Add(TLeaf.Create);

        AComposite.Add(TLeaf.Create);

        AComposite.Add(TComposite.Create);

        //---

        AComposite.Operation;

    finally

        AComposite.Free;

    end;

end;

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TVideoGrabber Video SDK v10.8 for C#, VB.NET, VC++, Delphi and C++Builder (all versions), and ActiveX compatible tools (PowerBuilder, LabView,...) ©2018 Datastead Software Home page: http://www.datastead.com/products/tvideograbber.html Description CAPTURE AND PREVIEW OF REALTIME VIDEO SOURCES The TVideoGrabber SDK supports most of the video capture devices: - IP cameras in RTSP / ONVIF mode with PTZ support (*) - capture of live streaming URLs through RTSP, RTMP, UDP, RTP, SDP, HTTP, MMS (*) - USB webcams, - PCIe capture/rendering cards (e.g. BlackMagic Decklink) - capture cards including a H264 or MPEG hardware encoder, e.g. AverMedia Live Gamer Portable, - analog composite USB capture devices (e.g. Easycap) - PCIe analog composite capture cards (e.g. OsPrey 210, OsPrey 440) - GigE cameras (e.g. PointGrey) (*) requires our optional Datastead RTSP/RTMP/HTTP/ONVIF DirectShow Source Filter VIDEO RECORDING - audio/video recording to various formats after installing our optional Datastead Multipurpose DirectShow Encoder filter, Most of the recording formats include MP4, MOV, FLV, AVI, Ogg/Theora, WebM, etc... - audio/video recording with Legacy DirectShow compressors - AVI recording, - WMV/ASF recording, - MKV recording after installing the Haali MKV muxer, - MP4/FLV recording (may require a third-party H264 or AAC DirectShow encoder) Timer-based recording (delayed start, timered stop, or create new file periodically) Compression of the audio and video streams, "on-the-fly" or after recording, Pause/resume during recording, with possibility to create a new clip for each pause/resume, Preview while recording.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值