A Global Singleton is a class of which only one instance exists within a program.

Any attempt to use non-static members of the class involves performing operations on this one instance.




(defgeneric concat (a b)
  (:documentation "Concatenate two phrases."))
 
(defclass nonempty-phrase ()
  ((text :initarg :text :reader text)))
 
(defmethod concat ((a nonempty-phrase) (b nonempty-phrase))
  (make-instance 'nonempty-phrase :text (concatenate 'string (text a) " " (text b))))
 
(defmethod concat ((a (eql 'the-empty-phrase)) b)
  b)
 
(defmethod concat (a (b (eql 'the-empty-phrase)))
  a)
 
(defun example ()
  (let ((before (make-instance 'nonempty-phrase :text "Jack"))
        (mid (make-instance 'nonempty-phrase :text "went"))
        (after (make-instance 'nonempty-phrase :text "to fetch a pail of water")))
    (dolist (p (list 'the-empty-phrase
                     (make-instance 'nonempty-phrase :text "and Jill")))
      (dolist (q (list 'the-empty-phrase
                       (make-instance 'nonempty-phrase :text "up the hill")))
        (write-line (text (reduce #'concat (list before p mid q after))))))))