2b)機能の実現:ConcreteImplementor

Bridge::ConcreteImplementor では、Bridge::Implementor で規定されたプロトコルに従って、任意のサービスを実現します。

## --------------------             Bridge::ConcreteImplementor
class ItemList(XList, ListImplementor):
    def append(self, item):
        self.view.Items.Add(item)

プロパティー Items に対して、項目 Item を追加 Add します。

    def colorName(self, item):
        return item

引数 item に指定された色の名前 item をリターン値とします。

## --------------------             Bridge::ConcreteImplementor
class GridList(XList, ListImplementor):
    def __init__(self, view, client):
        XList.__init__(self, view, client)
        self.view.ItemsSource = ObservableCollection[object]()

データバインディングを導入するには、各項目ごとに依存性を管理できる ObservableCollection を利用します。すると、各リスト項目は、データソースの情報を反映します。

    def append(self, item):
        self.view.ItemsSource.Add(ColorItem(item))

プロパティー ItemsSource に対して、項目 ColorItem(item) を追加 Add します。

    def colorName(self, item):
        return item.name

引数 item に指定された色情報をもとに、色の名前 item.name をリターン値とします。


Previous|2/3|Next

2a)機能の実現:Implementor

任意のサービスを実現するときには、Bridge::Implementor で規定されたプロトコルに従います。

## --------------------             Bridge::Implementor
class ListImplementor:
    def append(self, item):
        raise NotImplementedError("append")

リスト項目を追加するときのプロトコル append を規定します。引数 items には、追加したい単一の項目を指定します。このとき、各項目をどのように表示するか(what)は、子孫クラスに委ねます。

## --------------------               
class XList:
    def __init__(self, view, client):
        self.view = view
        self.view.SelectionChanged += self.selectionChanged
        self.client = client

    def selectionChanged(self, sender, e):
        item = sender.SelectedItem
        print "%s: %s"%(self.__class__.__name__, item)
        color = getattr(Brushes, self.colorName(item))
        self.client.paint(color)

リスト項目を選択すると、イベント SelectionChanged が発生します。これに呼応してイベントハンドラー selectionChanged では、self.client に対して、指定した色 color で背景を描画 paint するように依頼します。
XList の子孫クラスでは、選択された項目 item を頼りに、具体的な色の名前を獲得 colorName します。