Object Pooling(对象池)实现 (2)

Student 类用作测试

public class Student : IDisposable { public string Name { get; set; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private bool _disposed; protected virtual void Dispose(bool disposing) { if (_disposed) return; if (disposing) { Name = null; //Free any other managed objects here. } _disposed = true; } } public void TestPool() { Func<Student> func = NewStudent; var pool = new Pool<Student>(AccessModel.FIFO, 2, func); for (var i = 0; i < 3; i++) { Student temp = pool.Rent(); //todo:Some operations pool.Return(temp); } Student temp1 = pool.Rent(); pool.Return(temp1); pool.Dispose(); } public Student NewStudent() { return new Student(); }

总结:至此,一个完整的对象池建立完毕。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdpwy.html