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