VisualWorks Smalltalk の“アプリケーションモデル”で BMI checker

アプリケーションモデル

これは、Fowler氏がVisualWorks Smalltalkのやり方を念頭において説明しているパターンだ。残念なことに私はSmalltalkに詳しくないので、私の理解が正しいか確かめる術がない(助けてsumimさん!!)。

http://blog.inomata.lolipop.jp/?eid=920323#p1


Smalltalk で書かれたコードがあったところでさしたる助けにはならないとは思いましたが、私自身の VisualWorksGUIフレームワークに対する勉強も兼ねて BMI値チェッカー(計算機)のサンプルを書いてみました。ファウラーの指摘にもあるように、テキストフィールドの背景の色を変えるのはたいへんそうなので、未実装です。

BMIChecker open

で起動できます。


http://squab.no-ip.com/collab/uploads/61/bmichecker.png

Smalltalk defineClass: #BMIChecker
    superclass: #{UI.ApplicationModel}
    indexedType: #none
    private: false
    instanceVariableNames: 'height weight bmi '
    classInstanceVariableNames: ''
    imports: ''
    category: '(none)'

BMIChecker >> initialize
    super initialize.
    height := 1.704 asValue.
    height onChangeSend: #bmi to: self.
    weight := 60.1 asValue.
    weight onChangeSend: #bmi to: self.
    bmi := ValueHolder new.

BMIChecker >> bmi
    ^bmi value: weight value / (height value ** 2)

BMIChecker >> height
    ^height

BMIChecker >> weight
    ^weight

BMIChecker class >> windowSpec
    "Tools.UIPainter new openOnClass: self andSelector: #windowSpec"

    <resource: #canvas>
    ^#(#{UI.FullSpec}
        #window:
        #(#{UI.WindowSpec}
            #label: 'BMI checker'
            #bounds: #(#{Graphics.Rectangle} 640 400 840 520 ) )
        #component:
        #(#{UI.SpecCollection}
            #collection: #(
                #(#{UI.LabelSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 15 0 15 0 46 18 )
                    #label: 'Height:' )
                #(#{UI.LabelSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 15 0 50 0 46 18 )
                    #label: 'Weight:' )
                #(#{UI.LabelSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 15 0 85 0 29 18 )
                    #label: 'BMI:' )
                #(#{UI.InputFieldSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 70 0 13 0 100 23 )
                    #model: #height
                    #type: #number )
                #(#{UI.InputFieldSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 70 0 48 0 100 23 )
                    #model: #weight
                    #type: #number )
                #(#{UI.InputFieldSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 70 0 83 0 100 23 )
                    #model: #bmi
                    #type: #number )
                #(#{UI.LabelSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 175 0 15 0 10 18 )
                    #label: 'm' )
                #(#{UI.LabelSpec}
                    #layout: #(#{Graphics.LayoutSizedOrigin} 175 0 50 0 16 18 )
                    #label: 'kg' ) ) ) )


ファイルイン用のファイルは こちら においておきました。


関連: