blob: 42b2f8b25efbb37f2bb7d4039a4f4600f13f827c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
;;; Port of https://stackoverflow.com/questions/3984296/model-view-controller-design-pattern-code-example.
;;; Model and Controller classes.
(require "json")
(defpackage #:mc
(:use #:openlisp #:json)
(:export
#:mc-server)
)
(in-package #:mc)
(defclass <graph-gui-stub> () ((button :accessor button)
(model :accessor model)
(controller :writer controller)))
(defmethod initialize-object :after ((self <graph-gui-stub>) initargs)
(setf (button self) (create (class <button>) 'm "Click Me"))
(setf (model self) (create (class <graph-data>)))
(setf (controller self) (create (class <graph-controller>) 'm (model self) 'b (button self))))
(defclass <graph-data> () ((number :reader number :writer get-number)))
(defmethod initialize-object :after ((self <graph-data>) initargs)
(setf (number self) 10))
(defgeneric increase-number (data))
(defmethod increase-number ((data <graph-data>))
(setf (number data) (+ (get-number data) 10)))
(defclass <graph-controller> () ((model :initarg m :reader model)
(button :initarg b :reader button)))
(defmethod initialize-object :after ((self <graph-controller>) initargs)
(set-click-handler (button self) self #'on-button-clicked))
(defgeneric on-button-clicked (controller))
(defmethod on-button-clicked ((controller <graph-controller>))
(increase-number (model controller)))
(provide "mc")
|