![]() |
[1999 年 4 月号] |
[Happy Squeaking!!]
5.Squeak演習: インターセッション
5.5 インターセッション応用 つづき
Interfaceのメタモデルの追加
まずはInterfaceをSmalltalkの中に定義しなければなりません。これはClassでもMetaclassでもない、全く独立したモデル要素と考えられます。よって、ClassDescriptionの下にサブクラスとして新たに作成します。
Interfaceは名前("IPrintable"など)、操作名の集合(printString、getBalanceなど)をもつので、そのための属性をそれぞれname、operationListとして定義します。
ClassDescription subclass: #Interface
instanceVariableNames: 'name operationList '
classVariableNames: ''
poolDictionaries: ''
category: 'Metamodel-Interface'
nameは、書き換えられても困るのでシンボル文字列を使うことにします。operaiotnListは、重複する操作名が入っては都合が悪いので、操作名を示すシンボル文字列を要素としてもつSetのインスタンスとします。
これより属性アクセス用のメソッドは以下のようになります。
(Interface >> accessingカテゴリ)
name
name isNil ifTrue:[ name := 'a nameless interface'].
^name
name: newName
name := newName
operationList
operationList isNil ifTrue:[ operationList := Set new].
^operationList
operationList: aListOfOperaionSymbol
operationList := aListOfOperaionSymbol
次は、Interfaceを便利に生成できるようなクラスメソッドを定義することにしましょう。
クラスメソッドの名前はnewでなければならないということはありません。Interfaceの場合は、名前と操作リストが必ず必要なのですから、生成時にそれらの情報が引数として与えられるようにしたほうが便利でしょう。
そこで以下のようなメソッドをブラウザの"class"側に定義します。
(Interface class >> instance creationカテゴリ)
name: aNameString operationList: aSetOfOperationSymbol
^self new name: aNameString;
operationList: aSetOfOperationSymbol asSet

Interfaceの生成用クラスメソッドのaccept
これでInterface name: #IPrintable operationList: #(#printString) という形式でInterfaceのインスタンスを生成できるようになりました。
SetのインスタンスをoperationList: の後で渡さなくとも、配列で渡せば、クラスメソッドの内部で変換されるようにしています。(aSetOfOperationSymbol asSetの部分)。
配列オブジェクトに対してasSetでSetのインスタンスに変換しています。この処理がないと
| se |
se := Set new. "Setを生成"
se add: #printString. "要素 #printStringを追加"
interface := Interface name: #IPrintable operationList: se.
"新たなInterfaceのインスタンス生成"
としなければなりません。
| SmallTip: 配列をSetのインスタンスに変換するときにはasSetを用いる |
さて、ここまでのソースです。
FileIn: Metamodel-Interface.st
(<=Click)
更につづく
| © 1999-2001 OGIS-RI Co., Ltd. |
|