3. Outline
• State-centered and behavior-centered OOP
• The beauty of multiple dispatch
• Changes in v0.6
• Design patterns in Julia
4. Linguisticrelativity
• or, the Sapir-Whorf hypothesis
• The language determines or constrains cognition.
• The tools you use affect your thought. (frame effect)
你的語言如何影響了你的「思考」?
5. Whatcanwe do with/toa thing?
• OOP with class
buy
top up
pay fare
loss
buy
top up
pay fare
loss
buy
pay fare
loss
methods objects
16. The beautyof multipledispatch
abstract type Shape end
struct Rock <: Shape end
struct Paper <: Shape end
struct Scissors <: Shape end
play(::Type{Paper}, ::Type{Rock}) = 1
play(::Type{Scissors}, ::Type{Paper}) = 1
play(::Type{Rock}, ::Type{Scissors}) = 1
play(::Type{T}, ::Type{T}) where {T <: Shape} = 0
play(a::Type{<:Shape}, b::Type{<:Shape}) = - play(b, a) # Commutativity
https://giordano.github.io/blog/2017-11-03-rock-paper-scissors/
17. Definethe Shape
abstract type Shape end
struct Rock <: Shape end
struct Paper <: Shape end
struct Scissors <: Shape end
https://giordano.github.io/blog/2017-11-03-rock-paper-scissors/
27. Compositepattern
struct Line <: Graphic
length::Float64
End
draw(l::Line) = println("Draw a $(l.length) cm line.")
struct Text <: Graphic
str::String
end
draw(t::Text) = println(t.str)
30. Decoratorpattern
• Attach additional responsibilities to an object dynamically.
• Client-specified embellishment of a core object by recursively wrapping it.
• Wrapping a gift, putting it in a box, and wrapping the box.
34. Decoratorpattern
abstract type Decorator <: LCD end
mutable struct Border <: Decorator
component::LCD
size::Float64
end
function draw(b::Border)
println("Draw a $(b.size) width border")
draw(b.component)
# do something
end
35. Decoratorpattern
mutable struct VerticalScrollBar <: Decorator
component::LCD
end
function draw(vsb::VerticalScrollBar)
# do something
draw(vsb.component)
println("Draw vertical scroll bar.")
end
scroll(vsb::VerticalScrollBar, direction::Symbol) =
println("Vertical scroll bar scrolls $(direction)")
36. Decoratorpattern
mutable struct HorizontalScrollBar <: Decorator
component::LCD
end
function draw(hsb::HorizontalScrollBar)
# do something
draw(hsb.component)
println("Draw horizontal scroll bar.")
end
scroll(hsb::HorizontalScrollBar, direction::Symbol) =
println("Horizontal scroll bar scrolls $(direction)")
38. Observerpattern
• Define a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updated automatically.
40. Observerpattern
abstract type Newspaper end
abstract type Subscriber end
function subscribe(::Subscriber, ::Newspaper) end
function unsubscribe(::Subscriber, ::Newspaper) end
function notify(::Newspaper) end
41. Observerpattern
struct SubscriberA <: Subscriber
name::String
end
update(a::SubscriberA, s::String) = println("$(a.name) is notified
by $(s).")
struct SubscriberB <: Subscriber
name::String
end
update(b::SubscriberB, s::String) = println("$(b.name) is notified
by $(s).")
44. Observerpattern
a = AppleNews()
suba = SubscriberA("Joe")
subb = SubscriberB("Kay")
subscribe(suba, a)
subscribe(subb, a)
notify(a)
Joe is notified by AppleNews.
Kay is notified by AppleNews.
47. Chainof responsibilitypattern
abstract type Account end
can_pay(acc::Account, amount) = acc.balance >= amount
function pay(acc::Account, amount)
if can_pay(acc, amount)
acc.balance -= amount
else
pay(acc.successor, amount)
end
end