VCL类学习之(六) TCollectionItem

TCollectionItem represents an item in a collection.
Unit
Classes
Description
A TCollection holds a group of TCollectionItem objects. TCollectionItems are created and destroyed by TCollection抯 Add and Clear methods. Each TCollectionItem has a Collection property that points to the TCollection object to which the item belongs.
Objects descended from TCollection can contain objects descended from TCollectionItem. For example, a TDBGridColumns object contains TColumn objects; these two classes are used by TDBGrid to represent grid columns.
  1.  TCollectionItem = class(TPersistent)
  2.   private
  3.     FCollection: TCollection;
  4.     FID: Integer;
  5.     function GetIndex: Integer;
  6.   protected
  7.     procedure Changed(AllItems: Boolean);
  8.     function GetOwner: TPersistent; override;
  9.     function GetDisplayName: string; virtual;
  10.     procedure SetCollection(Value: TCollection); virtual;
  11.     procedure SetIndex(Value: Integer); virtual;
  12.     procedure SetDisplayName(const Value: string); virtual;
  13.   public
  14.     constructor Create(Collection: TCollection); virtual;
  15.     destructor Destroy; override;
  16.     function GetNamePath: string; override;
  17.     property Collection: TCollection read FCollection write SetCollection;
  18.     property ID: Integer read FID;
  19.     property Index: Integer read GetIndex write SetIndex;
  20.     property DisplayName: string read GetDisplayName write SetDisplayName;
  21.   end;
  22. { TCollectionItem }
  23. constructor TCollectionItem.Create(Collection: TCollection);
  24. begin
  25.   SetCollection(Collection);
  26. end;
  27. destructor TCollectionItem.Destroy;
  28. begin
  29.   SetCollection(nil);
  30.   inherited Destroy;
  31. end;
  32. procedure TCollectionItem.Changed(AllItems: Boolean);
  33. var
  34.   Item: TCollectionItem;
  35. begin
  36.   if (FCollection <> niland (FCollection.FUpdateCount = 0then
  37.   begin
  38.     if AllItems then Item := nil else Item := Self;
  39.     FCollection.Update(Item);
  40.   end;
  41. end;
  42. function TCollectionItem.GetIndex: Integer;
  43. begin
  44.   if FCollection <> nil then
  45.     Result := FCollection.FItems.IndexOf(Self) else
  46.     Result := -1;
  47. end;
  48. function TCollectionItem.GetDisplayName: string;
  49. begin
  50.   Result := ClassName;
  51. end;
  52. function TCollectionItem.GetNamePath: string;
  53. begin
  54.   if FCollection <> nil then
  55.     Result := Format('%s[%d]',[FCollection.GetNamePath, Index])
  56.   else
  57.     Result := ClassName;
  58. end;
  59. function TCollectionItem.GetOwner: TPersistent;
  60. begin
  61.   Result := FCollection;
  62. end;
  63. procedure TCollectionItem.SetCollection(Value: TCollection);
  64. begin
  65.   if FCollection <> Value then
  66.   begin
  67.     if FCollection <> nil then FCollection.RemoveItem(Self);
  68.     if Value <> nil then Value.InsertItem(Self);
  69.   end;
  70. end;
  71. procedure TCollectionItem.SetDisplayName(const Value: string);
  72. begin
  73.   Changed(False);
  74. end;
  75. procedure TCollectionItem.SetIndex(Value: Integer);
  76. var
  77.   CurIndex: Integer;
  78. begin
  79.   CurIndex := GetIndex;
  80.   if (CurIndex >= 0and (CurIndex <> Value) then
  81.   begin
  82.     FCollection.FItems.Move(CurIndex, Value);
  83.     Changed(True);
  84.   end;
  85. end;
Elite Container是DELPHI下的一个轻量级IoC对象容器(IoC:Inverse of Control,反转控制)。它是参考了Java中的Spring框架(主要是配置文件的写法),并结合DELPHI的特点来构建的。相比Spring的对象容器,它提供的功能更为精简常用(如对象延迟创建、对象属性自动注入等),降低了学习的难度,并且提供了很多扩展点,你只需简单地写一个插件实现,并在配置文件中进行简单配置,就可以让Elite Container拥有你的自定义功能! 借助Elite Container和Ioc思想,你可以更轻易地构建出具有松散耦合、重用度高的应用程序。它的核心思想就是拆分功能的接口和实现,上层只依赖于下层的接口,然后通过Elite Container的配置,把不同的实现注入到该接口中,达到更换功能,也就是复用已有代码的目的。设计人员可以真正地发挥好自己的面向对象思想和相关设计模式,来架构企业级的应用程序,而无需象以前那样,在Delphi中用起面向对象总有点捉襟见袖的感觉。 ps. 由于写程序,还有精心准备的26个例子,都花费了我很多的时间,所以象征性地收2个资源分,希望大家不会介意:) ps2. 说明一下,开发出来的程序在发布的时候,除了发布EliteCore.bpl、EliteContainer.bpl外,还需要发布rtl.bpl(EliteCore.bpl引用到了它)。由于一时疏忽,例子中的Bin目录里忘记带上它了,请大家注意,谢谢~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值