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

Last change on this file since 11383 was 11383, checked in by vvoutilainen, 14 years ago

Add copyright/license headers.

File size: 2.7 KB
Line 
1;;; interface_implementation.lisp
2;;;
3;;; Copyright (C) 2008 Ville Voutilainen
4;;;
5;;; This program is free software; you can redistribute it and/or
6;;; modify it under the terms of the GNU General Public License
7;;; as published by the Free Software Foundation; either version 2
8;;; of the License, or (at your option) any later version.
9;;;
10;;; This program is distributed in the hope that it will be useful,
11;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13;;; GNU General Public License for more details.
14;;;
15;;; You should have received a copy of the GNU General Public License
16;;; along with this program; if not, write to the Free Software
17;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19; first we define a class hierarchy. No slots defined,
20; we don't need them in the example.
21(defclass base ())
22
23(defclass derived1 (base))
24
25(defclass derived2 (base))
26
27; then a couple of generic methods
28(defgeneric invoke (param) (:documentation "Sample generic function"))
29
30(defgeneric invoke2 (param) (:documentation "Sample generic function"))
31
32; and their methods, for different classes
33(defmethod invoke ((param derived1))
34  (format t "in derived1 invoke~%"))
35
36(defmethod invoke ((param derived2))
37  (format t "in derived2 invoke~%"))
38
39(defmethod invoke2 ((param derived1))
40  (format t "in derived1 invoke2~%"))
41
42(defmethod invoke2 ((param derived2))
43  (format t "in derived2 invoke2~%"))
44
45; closure for interface implementation, closes
46; over a provided object and calls the invoke
47; method with the object. Thus the firstFunction()
48; in MyInterface will call the invoke method.
49(defun make-first-function (object)
50  (lambda () (invoke object)))
51
52; closure for interface implementation, closes
53; over a provided object and invokes the invoke2
54; method with the object. Thus the secondFunction()
55; in MyInterface will call the invoke2 method.
56(defun make-second-function (object)
57  (lambda () (invoke2 object)))
58
59; gets an interface implementation, uses an instance of
60; class derived1
61(defun get-interface ()
62  (let ((firstobject (make-instance 'derived1)))
63    (jinterface-implementation "MyInterface"
64             "firstFunction" 
65             (make-first-function firstobject)
66             "secondFunction"
67             (make-second-function firstobject))))
68
69; gets an interface implementation, uses an instance of
70; class derived2
71(defun get-another-interface ()
72  (let ((secondobject (make-instance 'derived2)))
73    (jinterface-implementation "MyInterface"
74             "firstFunction" 
75             (make-first-function secondobject)
76             "secondFunction"
77             (make-second-function secondobject))))
78           
Note: See TracBrowser for help on using the repository browser.