5. Advanced Object-Oriented Programming Language-Oriented Programming



Similar documents
Chapter 13 - Inheritance

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

How To Understand And Understand Common Lisp

D06 PROGRAMMING with JAVA

Chapter 1 Fundamentals of Java Programming

The Common Lisp Object System: An Overview

Java: overview by example

CS193j, Stanford Handout #10 OOP 3

ICOM 4015: Advanced Programming

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

Java Application Developer Certificate Program Competencies

Java Programming Language

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Moving from CS 61A Scheme to CS 61B Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Inheritance, overloading and overriding

Glossary of Object Oriented Terms

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Java EE Web Development Course Program

Design by Contract beyond class modelling

Lecture 1: Introduction

The Smalltalk Programming Language. Beatrice Åkerblom

Fundamentals of Java Programming

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

Formal Engineering for Industrial Software Development

The CLIPS environment. The CLIPS programming language. CLIPS production rules language - facts. Notes

How To Design Software

History OOP languages Year Language 1967 Simula Smalltalk

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Outline of this lecture G52CON: Concepts of Concurrency

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

TypeScript for C# developers. Making JavaScript manageable

Java SE 8 Programming

Common Lisp ObjectiveC Interface

Introduction to Object-Oriented Programming

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

11 November

Computer Programming I

Description of Class Mutation Mutation Operators for Java

Specialized Programme on Web Application Development using Open Source Tools

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

AP Computer Science Java Subset

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Structural Modeling and Analysis

#820 Computer Programming 1A

Objective-C and Cocoa User Guide and Reference Manual. Version 5.0

The Needle Programming Language

Specialized Programme on Web Application Development using Open Source Tools

Web Application Development

Introduction to Object-Oriented Programming

The C Programming Language course syllabus associate level

Subjective-C 2.0. Multithreaded Context-Oriented Programming with Objective-C

Computing Concepts with Java Essentials

Agile Software Development

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

61A Lecture 16. Friday, October 11

Object-Oriented Programming in C# (VS 2010)

Android Application Development Course Program

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

XPoints: Extension Interfaces for Multilayered Applications

Lecture 15: Inheritance

Introduction to Java Lecture Notes. Ryan Dougherty

Exercise 8: SRS - Student Registration System

UML basics. Part III: The class diagram. by Donald Bell IBM Global Services

El Dorado Union High School District Educational Services

Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

GContracts Programming by Contract with Groovy. Andre Steingress

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

Smalltalk in Enterprise Applications. ESUG Conference 2010 Barcelona

Functional Programming

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Computer Programming I & II*

Part I. Multiple Choice Questions (2 points each):

VB.NET Programming Fundamentals

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

CS 111 Classes I 1. Software Organization View to this point:

ios Dev Crib Sheet In the Shadow of C

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Hoare-Style Monitors for Java

Object-Oriented Programming Lecture 2: Classes and Objects

DIPLOMADO DE JAVA - OCA

Object-Oriented Programming: Polymorphism

Génie Logiciel et Gestion de Projets. Object-Oriented Programming An introduction to Java

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

Transcription:

5. Advanced Object-Oriented Programming Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science Hochschule Darmstadt University of Applied Sciences 1

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? 2

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 9. Implementing a DSL: Macros 3

Agenda Classes Instances Methods Advanced concepts 4

Defining classes Schema for defining classes: Extension of cl:defclass with convenience features (define-class name (direct-superclass-name*) (slot-specifier*)) Example: Class name (define-class bank-account () (customer-name balance)) Slots a.k.a. instance variables / attributes bank-account +customer-name +balance 5

bank-account +customer-name +balance Inheritance Lisp provides multiple inheritance Examples: checking-account savings-account +credit-limit +interest-rate money-market-account (define-class checking-account (bank-account) (credit-limit)) Slots additional to inherited ones from superclass (define-class savings-account (bank-account) (interest-rate)) (define-class money-market-account (checking-account savings-account) ()) Multiple inheritance: slots from all superclasses (bank-account, savings-account, and checking-account) are inherited 6

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 )) Specifies the slot data type (quoted!) (validation platform-specific) Documents the slot Initializes the slot with a value Specifies slot as class slot (static variable in Java). Default: instance slot Documents the class 7

Agenda Classes Instances Methods Advanced concepts 8

Instantiation Instantiation of classes via make-instance Class name (quoted!) (make-instance 'bank-account) #<bank-account @ #x21135952> (make-instance 'checking-account :customer-name "Seibel") #<checking-account @ #x211382ca> Keyword parameters according to slot names (convenience feature of define-class) (make-instance 'savings-account :customer-name "Steele" :interest-rate 0.03) #<savings-account @ #x2113b352> 9

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) 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 (setf account(make-instance 'bank-account)) (set-balance 42 account) Write access of slot balance (print (get-balance account))) 42 Read access of slot balance 10

Agenda Classes Instances Methods Advanced concepts 11

Methods = functions Methods are defined as functions outside the class definition (unlike Java, C++, Smalltalk) Allows for multi-methods introduced later Class specified via :type keyword; parameter name and type declaration enclosed in list (define-function withdraw ((account :type bank-account) amount) (set-balance (- (get-balance account) amount) account)) Method body: implementation of functionality 12

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))) Method invocation 25 13

Polymorphism Methods are inherited and can be overwritten by subclasses The implementation is selected according to the type of the actual parameter passed Method definition for savings-account (define-function withdraw ((account :type savings-account) amount) (if (> amount (get-balance account)) (error "Account overdrawn") (call-next-method))) Invocation of withdraw of class bank-account (similar to super in Java) 14 (let (account) (setf account(make-instance 'savings-account :balance 40)) (withdraw account 50)) Error "Account overdrawn" Invocation of withdraw for savings-account according to type of object account

Agenda Classes Instances Methods Advanced concepts 15

Multimethods: Polymorphism over several parameters Powerful concept: Lisp methods may behave polymorphic over several parameters Avoids if/else cascades or stragegy pattern Example: Classic polymorphism: method applies to all subclasses of bank-account (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... ) Advanced polymorphism: method applies to the combination checking-account / savings-account and its respective subclasses 16

Method definitions specific to literal values Methods may be defined for concrete numbers or (quoted) symbols if their treatment is special Example: Is invoked if and only if amount = 42 (define-function withdraw ((account :type bank-account) 42) (print "The answer to all questions, universe, and everything") (call-next-method)) Invokes standard withdraw method Avoids if-cascades for special cases 17

:before :after and :around Methods Concept to elegantly factor out tests and functionality to be evaluated before and after method invocation Declares a method to be invoked Example: before primary withdraw methods (define-function withdraw :before ((account :type savings-account) amount) (if (> amount (get-balance account)) (error "Account overdrawn"))) to be invoked after primary withdraw methods (define-function withdraw :after ((account :type bank-account) 42) (print "The answer to all questions, universe, everything")) Result: as in previous examples but better separated from default business logic 18

: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 :around-methods are wrapped around all invocations 19