source: trunk/j/examples/abcl/interface_implementation_in_lisp/interface_implementation.lisp @ 11382

Last change on this file since 11382 was 11382, checked in by vvoutilainen, 13 years ago

Examples for using abcl, initial commit.

File size: 1.9 KB
Line 
1; first we define a class hierarchy. No slots defined,
2; we don't need them in the example.
3(defclass base ())
4
5(defclass derived1 (base))
6
7(defclass derived2 (base))
8
9; then a couple of generic methods
10(defgeneric invoke (param) (:documentation "Sample generic function"))
11
12(defgeneric invoke2 (param) (:documentation "Sample generic function"))
13
14; and their methods, for different classes
15(defmethod invoke ((param derived1))
16  (format t "in derived1 invoke~%"))
17
18(defmethod invoke ((param derived2))
19  (format t "in derived2 invoke~%"))
20
21(defmethod invoke2 ((param derived1))
22  (format t "in derived1 invoke2~%"))
23
24(defmethod invoke2 ((param derived2))
25  (format t "in derived2 invoke2~%"))
26
27; closure for interface implementation, closes
28; over a provided object and calls the invoke
29; method with the object. Thus the firstFunction()
30; in MyInterface will call the invoke method.
31(defun make-first-function (object)
32  (lambda () (invoke object)))
33
34; closure for interface implementation, closes
35; over a provided object and invokes the invoke2
36; method with the object. Thus the secondFunction()
37; in MyInterface will call the invoke2 method.
38(defun make-second-function (object)
39  (lambda () (invoke2 object)))
40
41; gets an interface implementation, uses an instance of
42; class derived1
43(defun get-interface ()
44  (let ((firstobject (make-instance 'derived1)))
45    (jinterface-implementation "MyInterface"
46             "firstFunction" 
47             (make-first-function firstobject)
48             "secondFunction"
49             (make-second-function firstobject))))
50
51; gets an interface implementation, uses an instance of
52; class derived2
53(defun get-another-interface ()
54  (let ((secondobject (make-instance 'derived2)))
55    (jinterface-implementation "MyInterface"
56             "firstFunction" 
57             (make-first-function secondobject)
58             "secondFunction"
59             (make-second-function secondobject))))
60           
Note: See TracBrowser for help on using the repository browser.