Le 06/02/2021 à 17:35, Pascal Bourguignon a écrit :
Now, where would defmacro enter the scene? We could define a few macros
and supporting functions to be able to define factories with their set
of concrete classes; the macro would expand to the boilerplate code,
such as the generic and methods, according to the implementation choice
(alist, factory class, factory symbol, etc).
(define-abstract-factory gui-factory
(window pane button textfield))
(define-factory x11-factory gui-factory
(x11-window x11-pane x11-button x11-textfield))
(define-factory x11-factory gui-factory
(macos-window macos-pane macos-button macos-textfield))
(defparameter *gui-factory* (make-factory 'x11-factory))
[...]
In effect, the abstract factory meta design pattern is thus implemented
in lisp as a define-abstract-factory macro, and the concrete abstract
factory design pattern as a define-factory macro.
I should add that while defining such macros may seem useless given the
small about of boilerplate they would cover, they offer the advantage of
providing an abstraction, that would let you change easily the
implementation at a later time (either to add features, or for
optimization reasons, etc).
--
__Pascal Bourguignon__