cs 335 software development
play

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


  1. CS 335 — Software Development Object-Oriented Versus Functional Programming; The Visitor Pattern April 14–16, 2014

  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"; } }

  3. Functional vs OO class Chicken implements Animal { String happyNoise() { return "cluck cluck"; } String excitedNoise() { return "cockadoodledoo"; } }

  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"; } }

  5. Functional vs OO datatype Animal = Dog | Cat ; fun happyNoise(Dog) = "pant pant" | happyNoise(Cat) = "purrrr" fun excitedNoise(Dog) = "bark" | excitedNoise(Cat) = "meow"

  6. Functional vs OO fun angryNoise(Dog) = "grrrrr" | angryNoise(Cat) = "hisssss"

  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";

  8. Functional vs OO Dog Cat Chicken happyNoise pant pant purrrr cluck cluck excitedNoise bark meow cockadoodledoo angryNoise grrrrr hisssss squaaaaack

  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 → ;

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

  11. Interpreter/Composite Pattern Statement Expression <<interface>> <<interface>> Nop Print BooleanLiteral IntLiteral Identifier expr:Expression val:boolean val:int id:String Block n stmts:AL<Statement> 2 BinaryExpr UnaryExpr Loop expr1:Expression expr:Expression guard:Expression expr2:Expression body:Statement Conditional 2 test:Expression then:Statement elze:Statement Assignment id:String expr:Expression

  12. Structures and operations Declaration Assignment Conditional . . . BinaryExpr IntLit . . . TypeCheck Compile Optimize PrettyPrint

  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.

  14. Visitor: General problem N <<interface>> V m(A) m(B) A B

  15. Visitor: Intent Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. DP, pg 331.

  16. Visitor: Structure N <<interface>> accept(v) V m(N) n.accept(this) m(A) m(B) A B accept(v) accept(v) v.m(this); v.m(this);

  17. Subtying the Visitors V m(N) n.accept(this) m(A) m(B) V2 V1

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend