yamasushi(2013/04/21 03:06:41 UTC)instance poolについてまとめてみます。
;; 'Instance pool' class is a class that keeps the list of instances ;; of itself and its subclasses. ;; A class that inherits <instance-pool-mixin> directly becomes a 'root' ;; class of the pool. Instances of the subclass of the root class will ;; be added to the pool. ;; ;; An application can have multiple root classes. If a class inherits ;; from two or more pooled classes, its instances will be added to ;; all the pools. ;; ;; The actual implementation of how to manage pools can be customizable ;; by subclassing <instance-pool-meta> and overloading methods.;; Pool management protocol ;; ;; instance-pool-create-pool (class <instance-pool-meta>) ;; Called on the class that directly inherits <instance-pool-mixin>, ;; and has to return a pool object. A pool object can be any object ;; that responds the following methods: ;; ;; (instance-pool:add pool instance) - add instance to pool. ;; (instance-pool:find pool pred) ;; (instance-pool:remove! pool pred) ;; (instance-pool:->list pool) ;; ;; instance-pool-compute-pools (class <instance-pool-meta>)
動機
あるクラス(およびその派生クラス)の全てのインスタンスのセットを保持しておきたい、 ということがよくある。例えば定義ファイルをパーズしてゆく最中にどんどん定義を 表すインスタンスを作っていって、後からまとめて処理したり検索したりするとか。 インスタンスを作るたびに明示的にレジストレーションしていってもいいんだが、 クラススロットを使って基底クラスのinitializeでレジストレーションするように すれば派生クラスでは何も考えなくてよい。