0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Common LispでSingletonなクラスを作る(CLOS)

Last updated at Posted at 2017-08-15

Common LispでSingletonなクラスをCLOSで書くとどうなるのか?

考えてみた結果以下のような感じになった。

singleton.lisp
(defpackage foo
  (:use cl cl-user)
  (:export foo
           get-instance))
(in-package :foo)

;; fooクラスを定義する。
(defclass foo () ())

;; +instance+にインスタンスを格納。(この時点ではコンストラクタは呼び出し可能)
(defvar +instance+ (make-instance 'foo))

;; 新しいインスタンスを作れなくするためにコンストラクタを呼び出せないようにする
(defmethod initialize-instance :around ((this foo) &key) 
  (error "can't create instance."))

;; インスタンスを取得するための関数を作る。(defunで定義された関数をstaticなメソッドのように使う)
(defun get-instance ()
  +instance+)

(in-package :cl-user)

なんだかすっきりしない形になった。
もっといい方法があれば教えてください。

追記(2017/8/19)

以下コメントで紹介していただいたものたち。
便利なものがありますね。

Common Lisp

cl-singleton-mixin
singleton-classes

どちらもQuickLispから利用できる。

Gauche

singleton.scm

Gaucheは使ったことないのでよくわからないけどスレッドセーフなSingletonが作れそう。

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?