SLIDE 1
CS 335 Software Development Object-Oriented Versus Functional - - PowerPoint PPT Presentation
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 2
SLIDE 3
Functional vs OO
class Chicken implements Animal { String happyNoise() { return "cluck cluck"; } String excitedNoise() { return "cockadoodledoo"; } }
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
Functional vs OO
datatype Animal = Dog | Cat ; fun happyNoise(Dog) = "pant pant" | happyNoise(Cat) = "purrrr" fun excitedNoise(Dog) = "bark" | excitedNoise(Cat) = "meow"
SLIDE 6
Functional vs OO
fun angryNoise(Dog) = "grrrrr" | angryNoise(Cat) = "hisssss"
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
Functional vs OO
Dog Cat Chicken happyNoise pant pant purrrr cluck cluck excitedNoise bark meow cockadoodledoo angryNoise grrrrr hisssss squaaaaack
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
MilliJava grammar
Expression → BoolLiteral | IntLiteral | Variable | BinaryExpression | UnaryExpression Variable → Identifier BinaryExpression → ( Expression BinaryOperator Expression ) UnaryExpression → ( UnaryOperator Expression ) BinaryOperator → + | - | * | / | % | || | && | < | > | ==
SLIDE 11 Print
Interpreter/Composite Pattern
Statement
<<interface>>
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
Structures and operations
Declaration Assignment Conditional . . . BinaryExpr IntLit . . . TypeCheck Compile Optimize PrettyPrint
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
Visitor: General problem
N
<<interface>>
A B V
m(A) m(B)
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
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