• No results found

5. Advanced Object-Oriented Programming Language-Oriented Programming

N/A
N/A
Protected

Academic year: 2021

Share "5. Advanced Object-Oriented Programming Language-Oriented Programming"

Copied!
19
0
0

Loading.... (view fulltext now)

Full text

(1)

5. Advanced

Object-Oriented Programming

Language-Oriented Programming

Prof. Dr. Bernhard Humm

Faculty of Computer Science

(2)

Retrospective Functional Programming

• What is a higher-order function?

• What is a lambda function / closure?

• Are closures just „syntactic sugar“ or are they more powerful than

named functions? Is there behaviour that you can express with

closures that you cannot express with named functions?

• Explain sort, detect, select, collect, and inject

• What additional documentation features are implemented in the

language functional.1? Do they form a language extension?

• What are pre- and post-conditions / design by contract?

• Which design-by-contract features are implemented in the language

functional.1? Do they form a language extension?

(3)

This lecture in the context of the entire

course

1. Introduction

2. Lisp Crash Course

3. Functional programming

4. Advanced object-oriented programming

5. Business information systems

6. Database queries

7. Logic programming

8. Workflows

(4)

Agenda

• Classes

• Instances

• Methods

(5)

Defining classes

• Schema for defining classes:

• Example:

(define-class name (direct-superclass-name*)

(slot-specifier*))

(

define-class

bank-account ()

(customer-name

balance))

Extension of cl:defclass

with convenience features

Class name

Slots a.k.a. instance variables /

attributes

bank-account

+customer-name +balance

(6)

Inheritance

• Lisp provides multiple inheritance

• Examples:

(define-class checking-account (

bank-account

)

(credit-limit))

(define-class savings-account (

bank-account

)

(interest-rate))

(define-class money-market-account

(

checking-account savings-account

)

())

Slots additional to inherited ones from

superclass

Multiple inheritance: slots from all

superclasses (bank-account, savings-account,

and checking-account) are inherited

bank-account +customer-name +balance checking-account +credit-limit savings-account +interest-rate money-market-account

(7)

Slot and class options

• Lisp provides useful slot and class options via keyword parameters

• Examples:

(define-class bank-account ()

((customer-name

:type

'string

:documentation

"Surname of the customer")

(balance

:initform

0)

(all-accounts

:allocation

:class))

(

:documentation

"General bank account“))

Initializes the slot with a value

Specifies the slot data type (quoted!)

(validation platform-specific)

Documents the slot

Specifies slot as class slot (static

variable in Java). Default: instance slot

(8)

Agenda

• Classes

• Instances

• Methods

(9)

Instantiation

• Instantiation of classes via make-instance

(

make-instance

'bank-account)

#<bank-account @ #x21135952>

(

make-instance

'checking-account

:customer-name "Seibel")

#<checking-account @ #x211382ca>

(

make-instance

'savings-account

:customer-name "Steele"

:interest-rate 0.03)

#<savings-account

@ #x2113b352>

Class name (quoted!)

Keyword parameters

according to slot names

(convenience feature of

define-class)

(10)

Slot access

• Convenience feature of create-class:

automatic generation of getters and setters

• The examples make use of imperative programming in Lisp:

- Local variables

- Assignments

- Sequential execution

(let

(account)

(setf account(make-instance 'bank-account))

(

set-balance

42 account)

(print (

get-balance

account)))

42

Definition of local variables that can be used

within the scope of let;

sequential execution of following forms

Assignment of a value to a variable

Write access of slot balance

(11)

Agenda

• Classes

• Instances

• Methods

(12)

Methods = functions

• Methods are defined as functions outside the class definition (unlike

Java, C++, Smalltalk)

• Allows for multi-methods – introduced later

(define-function withdraw ((account

:type

bank-account)

amount)

(set-balance (- (get-balance account) amount) account))

Class specified via :type keyword;

parameter name and type declaration

enclosed in list

Method body: implementation of

functionality

(13)

Method invocation

• Methods are invoked like funtions

(unlike message passing in Java, C++, Smalltalk)

(let

(account)

(setf account(make-instance 'bank-account :balance 40))

(

withdraw account 15

)

(print (get-balance account)))

25

(14)

Polymorphism

• Methods are inherited and can be overwritten by subclasses

• The implementation is selected according to the type of the actual

parameter passed

(define-function withdraw ((account :type savings-account)

amount)

(if (> amount (get-balance account))

(error "Account overdrawn")

(call-next-method)))

(let

(account)

(setf account(make-instance 'savings-account :balance 40))

(withdraw account 50))

Error

Method definition for savings-account

Invocation of withdraw of class bank-account

(similar to

super

in Java)

(15)

Agenda

• Classes

• Instances

• Methods

(16)

Multimethods:

Polymorphism over several parameters

• Powerful concept: Lisp methods may behave polymorphic over

several parameters

• Avoids if/else cascades or stragegy pattern

• Example:

(define-function transfer

((from :type bank-account) (to :type bank-account) amount)

;;; general behaviour

(withdraw from amount)

(deposit to amount))

(define-function transfer

((from :type checking-account) (to :type savings-account) amount)

;;; specific behaviour ...

)

Classic polymorphism: method applies to all

subclasses of bank-account

Advanced polymorphism: method applies to the combination

(17)

Method definitions specific

to literal values

• Methods may be defined for concrete numbers or (quoted) symbols

if their treatment is special

• Example:

• Avoids if-cascades for special cases

(define-function withdraw ((account :type bank-account)

42

)

(print "The answer to all questions, universe, and everything")

(call-next-method))

Is invoked if and only if

amount = 42

(18)

:before :after and :around Methods

• Concept to elegantly factor out tests and functionality to be evaluated before

and after method invocation

• Example:

• Result: as in previous examples but better separated from default business

logic

(define-function withdraw :before

((account :type savings-account) amount)

(if (> amount (get-balance account))

(error "Account overdrawn")))

(define-function withdraw :after

((account :type bank-account) 42)

(print "The answer to all questions, universe, everything"))

Declares a method to be invoked

before primary withdraw methods

to be invoked after primary

withdraw methods

(19)

:before :after and :around Methods

(cont’d)

• Invocation order:

1. All

:before

methods (here

withdraw

for

savings-account

)

2. All primary methods, i.e., without

:before

, :

after

, or :

around

specifier (here:

withdraw

for

bank-account

)

3. All :

after

methods

References

Related documents

GAA - Georgia Alternate Assessment GaDOE - Georgia Department of Education GHSGT - Georgia High School Graduation Tests GHSWT - Georgia High School Writing Test..

This Account Center (Banking) Agreement (the &#34;Agreement&#34;) includes the terms and conditions for your use of the Discover Bank Account Center – Banking (the “Account

Q12: In order of importance (1= most significant opportunity), please identify the top opportunities that you see for the future development of Ireland's international

 Emergency core cooling system via the steam generators – cools down the reactor core upon loss of power and loss of coolant in the primary or secondary circuit. In the event

This method returns a reference to the thread in which it is called. Once you have a reference to the main thread, you can control it just like any other thread. More than one

As the responsibility for the manufacturing of autoparts, modules and systems proceeds from Original Equipment Manufacturers (OEM’s) to the actors below in the supply chain,

Ordinary methods - functions that act on one or more objects (plus additional data) and return a new object or some computed value Constructor methods - special function that

In this review, the research carried out using various ion-exchange resin-like adsorbents including modified clays, lignocellulosic biomasses, chitosan and its derivatives, microbial