CS 335 Software Development Object-Oriented Versus Functional - - PowerPoint PPT Presentation

cs 335 software development
SMART_READER_LITE
LIVE PREVIEW

CS 335 Software Development Object-Oriented Versus Functional - - PowerPoint PPT Presentation

CS 335 Software Development Object-Oriented Versus Functional Programming; The Visitor Pattern April 1416, 2014 Functional vs OO interface Animal { String happyNoise(); String excitedNoise(); } class Dog implements Animal { String


slide-1
SLIDE 1

CS 335 — Software Development

Object-Oriented Versus Functional Programming; The Visitor Pattern April 14–16, 2014

slide-2
SLIDE 2

Functional vs OO

interface Animal { String happyNoise(); String excitedNoise(); } class Dog implements Animal { String happyNoise() { return "pant pant"; } String excitedNoise() { return "bark"; } } class Cat implements Animal { String happyNoise() { return "purrrrr"; } String excitedNoise() { return "meow"; } }

slide-3
SLIDE 3

Functional vs OO

class Chicken implements Animal { String happyNoise() { return "cluck cluck"; } String excitedNoise() { return "cockadoodledoo"; } }

slide-4
SLIDE 4

Functional vs OO

interface Animal { String happyNoise(); String excitedNoise(); String angryNoise(); } class Dog implements Animal { String happyNoise() { return "pant pant"; } String excitedNoise() { return "bark"; } String angryNoise() { return "grrrrr"; } } class Cat implements Animal { String happyNoise() { return "purrrrr"; } String excitedNoise() { return "meow"; } String angryNoise() { return "hissss"; } }

slide-5
SLIDE 5

Functional vs OO

datatype Animal = Dog | Cat ; fun happyNoise(Dog) = "pant pant" | happyNoise(Cat) = "purrrr" fun excitedNoise(Dog) = "bark" | excitedNoise(Cat) = "meow"

slide-6
SLIDE 6

Functional vs OO

fun angryNoise(Dog) = "grrrrr" | angryNoise(Cat) = "hisssss"

slide-7
SLIDE 7

Functional vs OO

datatype Animal = Dog | Cat | Chicken; fun happyNoise(Dog) = "pant pant" | happyNoise(Cat) = "purrrr" | happyNoise(Chicken) = "cluck cluck"; fun excitedNoise(Dog) = "bark" | excitedNoise(Cat) = "meow" | excitedNoise(Chicken) = "cockadoodledoo"; fun angryNoise(Dog) = "grrrrr" | angryNoise(Cat) = "hisssss" | angryNoise(Chicken) = "squaaaack";

slide-8
SLIDE 8

Functional vs OO

Dog Cat Chicken happyNoise pant pant purrrr cluck cluck excitedNoise bark meow cockadoodledoo angryNoise grrrrr hisssss squaaaaack

slide-9
SLIDE 9

MilliJava grammar

Program → public class ID { public static void main( String[] args ) { Declaration* Statement* } } Declaration → Type ID ; Type → int | boolean Statement → Assignment | Conditional | Loop | Block | Print | Nop Assignment → ID = Expression ; Conditional → if ( Expression ) Statement else Statement Loop → while ( Expression ) Statement Block → { Statement *} Print → System.out.println( Expression ) ; Nop → ;

slide-10
SLIDE 10

MilliJava grammar

Expression → BoolLiteral | IntLiteral | Variable | BinaryExpression | UnaryExpression Variable → Identifier BinaryExpression → ( Expression BinaryOperator Expression ) UnaryExpression → ( UnaryOperator Expression ) BinaryOperator → + | - | * | / | % | || | && | < | > | ==

slide-11
SLIDE 11

Interpreter/Composite Pattern

Statement

<<interface>>

Print

expr:Expression

Assignment

id:String expr:Expression

Conditional

then:Statement elze:Statement

Loop

test:Expression body:Statement guard:Expression stmts:AL<Statement>

Block Nop Expression

<<interface>>

IntLiteral BooleanLiteral

val:boolean val:int id:String

BinaryExpr UnaryExpr

expr1:Expression expr2:Expression expr:Expression

Identifier

2 2 n

slide-12
SLIDE 12

Structures and operations

Declaration Assignment Conditional . . . BinaryExpr IntLit . . . TypeCheck Compile Optimize PrettyPrint

slide-13
SLIDE 13

Our goal

Separate the structure/data from functionality so that we can add new functionality by writing one new “module” rather than modifying several.

slide-14
SLIDE 14

Visitor: General problem

N

<<interface>>

A B V

m(A) m(B)

slide-15
SLIDE 15

Visitor: Intent

Represent an operation to be performed on the elements

  • f an object structure. Visitor lets you define a new
  • peration without changing the classes of the elements
  • n which it operates.

DP, pg 331.

slide-16
SLIDE 16

Visitor: Structure

V N

<<interface>> accept(v) accept(v) m(N) m(A) m(B) accept(v)

A B

v.m(this); v.m(this); n.accept(this)

slide-17
SLIDE 17

Subtying the Visitors

V

m(N) m(A) m(B) n.accept(this)

V2 V1