Pluggable Adapters

Download Report

Transcript Pluggable Adapters

Pluggable Adapters
A short introduction with examples from Smalltalk applied on
the Converter example
Karlstads universitet
Datavetenskap
DAV D11
Object Oriented Methods
Adapters for TextField and Scrollbar
• Individual adapters need to be tailored for each application
area
Scrollbar
ScrollAdapter
1
1
+ se tT ex t( te x t: St ri ng ) :v oi d
+ se tV al ue (v a lu e: in t) : vo id
+ ge tT ex t( ): S tr in g
+ ge tV al ue () : in t
+ up da te () :v o id
ConversionModel
1
1
TextAdapter
1
+ se tT ex t( te x t: St ri ng ) :v oi d
+ ge tT ex t( ): S tr in g
TextField
1
+ se tV al ue (v a lu e: in t) : vo
+ ge tV al ue () : in t
+ up da te () :v o id
Karlstads universitet
Datavetenskap
Pluggable Adapters 2
DAV D11
Object Oriented Methods
Reusable PluggableAdapter
• A pluggable adapter can be reused for different classes
Object
ConversionModel
1
PluggableAdapter
0 .. *
- ge tB lo ck : in t
a da pt ee
- se tB lo ck : in t
- up da te Bl o ck :i nt
+ se tV al ue ( va lu e: in t ): vo id
+ ge tV al ue ( ): in t
TextField
+ up da te () : vo id
+ ge tT ex t( ) :S tr in g
Scrollbar
+ ge tV al ue ( ): in t
+ se tT ex t( t ex t: St ri n g) :v+oi
sedtV al ue ( va lu e: in t ): vo
Karlstads universitet
Datavetenskap
Pluggable Adapters 3
DAV D11
Object Oriented Methods
To set up a PluggableAdapter
• The operations for setting and reading values and for being
updated are set at run time instead of at compile time
– Lagras som kod i lokala variabler
– getBlock, setBlock, updateBlock
• Example code from Smalltalk
– a is formal parameter for the adaptee
– v is formal parameter for the new value
| tempA |
tempA := PluggableAdapter on: Scrollbar new
tempA getBlock: [:a | a getValue]
setBlock: [:a :v | a setValue: v]
Karlstads universitet
Datavetenskap
Pluggable Adapters 4
DAV D11
Object Oriented Methods
How a PluggableAdapter works
• The code in a pluggable adapter always does the same
thing
– getValue calls the code in the getBlock with the adaptee as a
parameter
getValue
^getBlock eval: adaptee
– setValue calls the code in the setBlock with the adaptee and the
new value as parameters
setValue: newValue
^setBlock eval: adaptee eval: newValue
Karlstads universitet
Datavetenskap
Pluggable Adapters 5
DAV D11
Object Oriented Methods
How can this work for the text field as well?
• The adaptation is done in the creation and setup of the
adapter
– This adapter needs to convert the values to and from text
format
• Example code from Smalltalk
| tempA |
tempA := PluggableAdapter on: TextField new
tempA getBlock: [:a | s getText intValue]
setBlock: [:a :v | s setText: v toString]
Karlstads universitet
Datavetenskap
Pluggable Adapters 6
DAV D11
Object Oriented Methods