`
cooldesigner
  • 浏览: 67447 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

Reference Count

阅读更多

起源

Reference Count模式是一种相对简单但实用性很强比如COM中的引用记数就是COM生命周期控制的基础同样也是Observer模式(详细请见Observer模式)的生命周期的基础。

目的<o:p></o:p>

利用Reference Count的模式来控制对象的生命周期。对于引用记数中对象将是可利用的对象,否则对象将析构自身。<o:p></o:p>

动机

Reference Count模式的基础与Lock的基础很相类似。唯一不同的是Lock预先定义、固定的。它的具体的类的应用时也与Lock模式类似。Reference Count模式默认一种行为方法:SetReferenced,同样当具体使用些模式时,你可以创建自己习惯的方法。如

 <o:p></o:p>

Reference Count模式在singleton模式中会有比较好的应用。<o:p></o:p>

应用

unit ReferenceCount;<o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

  TRefCountSample = class (TObject)<o:p></o:p>

  private<o:p></o:p>

    FReferenceCnt: Integer;<o:p></o:p>

  protected<o:p></o:p>

    function Referenced: Boolean;<o:p></o:p>

    procedure SetReferenced(IsReferenced: Boolean);<o:p></o:p>

  public<o:p></o:p>

    procedure AddReference;<o:p></o:p>

    procedure RemoveReference;<o:p></o:p>

  end;<o:p></o:p>

 <o:p></o:p>

{<o:p></o:p>

*******************************TRefCountSample*******************************<o:p></o:p>

}<o:p></o:p>

procedure TRefCountSample.AddReference;<o:p></o:p>

begin<o:p></o:p>

  Inc(FReferenceCnt);<o:p></o:p>

  if FReferenceCnt = 1 then SetReferenced(True);<o:p></o:p>

end;<o:p></o:p>

 <o:p></o:p>

function TRefCountSample.Referenced: Boolean;<o:p></o:p>

begin<o:p></o:p>

  Result := (FReferenceCnt <> 0);<o:p></o:p>

end;<o:p></o:p>

 <o:p></o:p>

procedure TRefCountSample.RemoveReference;<o:p></o:p>

begin<o:p></o:p>

  Dec(FReferenceCnt);<o:p></o:p>

  if FReferenceCnt = 0 then SetReferenced(False);<o:p></o:p>

end;<o:p></o:p>

 <o:p></o:p>

procedure TRefCountSample.SetReferenced(IsReferenced: Boolean);<o:p></o:p>

begin<o:p></o:p>

  if not Referenced then Free;<o:p></o:p>

end;<o:p></o:p>

Delphi实例IUnknown

IUnknown = interface<o:p></o:p>

    ['{00000000-0000-0000-C000-000000000046}']<o:p></o:p>

    …<o:p></o:p>

function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;<o:p></o:p>

    function _AddRef: Integer; stdcall;<o:p></o:p>

    function _Release: Integer; stdcall;<o:p></o:p>

  end;<o:p></o:p>

分享到:
评论

相关推荐

    GObject Reference Manual

    Reference count Weak References Reference counts and cycles Object properties Accessing multiple properties at once The GObject messaging system Closures C Closures non-C closures (for the ...

    msm_gpiomux.rar_The Count

    标题“msm_gpiomux.rar_The Count”暗示了我们正在处理与高通(Qualcomm)MSM(Mobile Station Modem)平台相关的GPIO复用(GPIO muxing)技术,特别是关于GPIO引用计数(reference counting)的部分。描述中提到的...

    vm_anon.rar_The Count

    Decrement the reference count of an anon page. If reference count goes to zero, free it and its associated page (if any).

    GObject reference count debugger-开源

    GObject参考计数调试器(以下简称Refdbg)是一款专为解决GObject、GTK+以及Gnome应用程序中的引用计数错误而设计的开源工具。在理解Refdbg之前,我们需要先了解什么是GObject,以及引用计数在面向对象编程中的重要性...

    memory.txt

    当类中有指针成员时,一般有两种方式...智能指针(smart pointer)的一种通用实现技术是使用引用计数(reference count)。智能指针类将一个计数器与类指向的对象相关联,引用计数跟踪该类有多少个对象的指针指向同一对象。

    C++智能指针实现

    它的一种通用实现技术是使用引用计数(reference count)。智能指针类将一个计数器与类指向的对象相关联,引用计数跟踪该类有多少个对象共享同一指针。每次创建类的新对象时,初始化指针并将引用计数置为1;当对象作为...

    SmartPtr_Reference_Count

    在C++编程中,"SmartPtr_Reference_Count"这个主题涉及到智能指针(Smart Pointer)的实现,特别是关于引用计数(Reference Counting)的技术。在《More Effective C++》这本书的条款29中,作者Scott Meyers探讨了...

    华南理工大学《操作系统》期末复习资料(超全).pdf

    文件还讨论了文件系统,如文件引用计数(file-reference count)的用途,这是用来计算指向文件的链接数以及有多少进程在访问该文件。Unix系统中,文件属性是存储在索引节点(i-node)中,而不是目录或目录项中。 最后,...

    kobject结构体1

    一个引用计数(reference count),用于跟踪对这个对象的引用数量,确保在所有使用它的代码都释放引用后,才能安全地删除该对象。此外,kobject还有一个指向其父对象的指针,这样可以构建起一个层级结构,如设备树;...

    java memory intro

    4. **引用计数算法**(Reference Count GC Algorithm):通过记录每个对象的引用数量来判断对象是否可回收。 #### JVM内存热点与代际垃圾回收 1. **热点**:频繁执行的代码片段,通常用于优化编译器的优化目标。 2...

    list文档报告1

    Rust 的 `Arc`(Atomic Reference Count) 类型提供了一种解决方案,它可以安全地跨多个线程共享可变数据。`Arc` 使用引用计数来跟踪有多少个引用指向同一个对象,只要还有引用存在,对象就不会被释放。然而,使用 `...

    learning-netty

    `ReferenceCount`是Netty中用于管理缓冲区引用计数的机制,确保资源在不再使用时能够被及时释放。 #### 接口ReferenceCounted 定义了对象引用计数的行为。 #### 类AbstractReferenceCountedByteBuf 这是一个实现了...

    MM_designpatterns_中文SN2002-9-28_

    文档提到ModelMaker6.2支持八种设计模式,分别是Visitor、Observer、Wrapper、Singleton、Mediator、Decorator、ReferenceCount和Lock。这些模式都是基于经典的《Design Patterns: Elements of Reusable Object-...

    windbg實戰

    devobj命令并加上设备对象的地址参数,调试器会输出该设备对象当前的状态信息,包括正在处理的IRP(I/O 请求包)、引用计数(reference count)、设备扩展(device extension)等。 !drvobj和!devobj只是众多Windbg...

    boost 智能指针

    std::cout &lt;&lt; "Reference count: " &lt;&lt; ptr.use_count() ; return 0; } ``` 在这个例子中,`ptr`和`ptr2`都指向同一个`MyClass`对象。当最后一个`shared_ptr`实例离开作用域时,`MyClass`对象会被释放。 #### ...

    PHP内核介绍及扩展开发指南

    /* reference count */ zend_uchar type; /* active type */ zend_uchar is_ref; /* is it a reference? */ } zval; ``` 其中`type`字段用于标识`value`中的具体类型,而`value`字段则包含了具体的值。例如,...

Global site tag (gtag.js) - Google Analytics