1 (No Transcript) 2 Modular OOP with Units and Mixins
Paper by Finder/Flatt
Presentation by Dan Licata
3 Scheme in Fixnum Minutes
gt 4
4
gt(define x 4)
gt x
4
4 Scheme in Fixnum Minutes
gt( 4 4)
8
gt ( ( 4 ( 4 4)) ( 4 4))
20
5 Scheme in Fixnum Minutes
gt 5
5
6 Scheme in Fixnum Minutes
gt (define add2 (lambda (x y) ( x y)))
gt (add2 4 5)
9
gt add2
ltprocedure...gt
7 Scheme in Fixnum Minutes
(define add2(lambda (x y) ( x y))
gt (add2 4 5)
9
(define mk-add(lambda (x) (lambda(y) ( x y)))) 8 Scheme in Fixnum Minutes
gt (mk-add 4)
ltproceduregt
gt (define add4 (mk-add 4))
gt (add4 5)
1
(define mk-add(lambda (x) (lambda(y) ( x y)))) 9 Reading a Paper
What problem are they solving?
How do they solve it?
Why should I care?
10 Reading a Paper
What problem are they solving?
How do they solve it?
Why should I care?
11 Extensibility!
Same story as last time
Functional programming gt easy to add a new operation
OOP gt easy to add a new datatype variant
What would the ideal be?
12 Extensibility Shape Clients 13 Add a New Variant Shape Existing clients should not need to be modified! Clients 14 Add a New Variant Shape Clients New Clients 15 Add a New Operation Shape Clients New Clients 16 Presumptions in the Picture
Add a new operation by subclassing
If the change gets used behind the original interface, unmodified clients should see it
In traditional OOP, do they?
17 Define Original Datatype
(define Shape (interface () draw)
(define Rectangle
(class null (Shape) (width height)
(public
draw (lambda (window x y) ))))
18 Define Original Datatype
(define Circle
(class null (Shape) (radius)
(public
draw (lambda (window x y) ...))))
19 Define Original Datatype
(define display-shape
(lambda (shape)
(let (window ...)
(send shape draw window 0 0))))
20 Create A Client
(display-shape (make-object Rectangle
50
100))
21 Add a New Variant
(define Union
(class null (Shape) (left right)
(public
draw (lambda (window x y) ...))))
22 Add a Client that Uses It
(display-shape
(make-object Union
(make-object Rectangle 10 30)
(make-object Circle 20)))
23 Add a New Operation
(define BB-Shape
(interface (shape) bounding-box)
(define BB-Rectangle
(class Rectangle (BB-Shape) (w h)
(public
bounding-box (lambda ()
(make-object BB
0 0 w h)))
...))
24 Add a New Operation
(define display-shape
(lambda (shape)
(let (bb (send shape bounding-box)
(let (window ... x ... y ...)
(send shape draw window x y)))))
25 Do the Old Clients Use It?
(display-shape (make-object Rectangle
50
100))
(display-shape
(make-object Union
(make-object Rectangle 10 30)
(make-object Circle 20)))
26 Do the Old Clients Use It?
(display-shape (make-object Rectangle
50
100))
(display-shape
(make-object Union
(make-object Rectangle 10 30)
(make-object Circle 20)))
27 Potential Solution
Always program using Abstract Factory
Downsides
Requires forethought
Contorts the code
How can we add language support?
28 Reading a Paper
What problem are they solving?
How do they solve it?
Why should I care?
29 Fortune-Cookie Wisdom
Every problem in CS can be solved by adding a layer of indirection.
Layers
Mixins
Units
30 Mixin
Class that is parameterized by its superclass
(define BB-Rect (class Rectangle ...)) (define BB-Rect (lambda (Rect) (class Rect ...))) 31 Unit
Module that is
Separately compilable
Black-box reusable (has an interface)
Multiply instantiable
Dynamically linkable
32 Define Original Datatype
(define Basic-Shapes
(unit (import)
(export Shape Rectangle Circle)
(define Shape (interface ...))
(define Rectangle (class ...))
(define Circle (class ...))))
33 Define Original Datatype
(define GUI
(unit (import Shape)
(export display-shape)
(define display-shape ...)))
34 Create a Client
(define Picture
(unit (import Shape Rectangle Circle
display-shape)
(export)
(display-shape
(make-object Rectangle 50 100))))
35 Link Them All Together
(define Basic-Program
(compound-unit
(import)
(link S (Basic-Shapes)
G (GUI (S Shape))
P (Picture (S Rectangle)
(S Circle)
(G display-shape)))
(export)))
36 And Run It
(invoke-unit Basic-Program)
37 Add a New Variant
(define Union-Shape
(unit (import Shape)
(export Union)
(define Union (class ...))))
38 Package It Up with the Others
(define BasicUnion-Shapes
(compound-unit (import)
(link S (Basic-Shapes)
US (Union-Shape (S Shape)))
(export (S Shape) (S Rectangle)
(S Circle) (US Union))))
39 Add a Client that Uses It
(define Union-Picture
(unit (import Rectangle Circle Union
display-shape)
(export)
(display-shape
(make-object Union
(make-object Rectangle 10 30)
(make-object Circle 20))))
40 Link Them All Together
(define Union-Program
(compound-unit
(import)
(link S (BasicUnion-Shapes)
G (GUI (S Shape))
P (Picture (S Rectangle)
(S Circle)
(G display-shape))
UP (Union-Picture (S Rectangle)
(S Circle) (S Union)
(G display-shape)))
(export)))
Picture is unchanged! 41 Add a New Operation
(define BB-Shapes
(unit (import Shape Rectangle Circle Union)
(export BB-Shape BB-Rectangle
BB-Circle BB-Union)
(define BB-Shape (interface ...))
(define BB-Rectangle (class Rectangle ...))
(define BB-Circle (class Circle ...)
(define BB-Union (class Union ...)
(define BB ...)))
Implicit mixin! 42 Package It Up with the Others
(define BasicUnionBB-Shapes
(compound-unit (import)
(link S (BasicUnion-Shapes)
BS (BB-Shapes (S Shape)
(S Rectangle) (S Circle) (S Union)))
(export (S Shape) (BS BB-Shape) (BS BB)
(BS (BB-Rectangle Rectangle))
...)))
Rename to preserve substitutability! 43 Use the New Functionality
(define BB-GUI
(unit (import BB-Shape BB)
(export display-shape)
(define display-shape ...)))
44 Link Them All Together Picture and UnionPicture are unchanged!
(define BB-Program
(compound-unit
(import)
(link S (BasicUnionBB-Shapes)
BG (BB-GUI (S BB-Shape) (S BB))
P (Picture (S Rectangle)
(S Circle)
(BG display-shape))
UP (Union-Picture (S Rectangle)
(S Circle) (S Union)
(BG display-shape)))
(export)))
45 Reading a Paper
What problem are they solving?
How do they solve it?
Why should I care?
46 Solves Extensibility Problem
You can add both new variants and new operations without modifying existing clients
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.