まさるblog

越後在住子持ちプログラマー奮闘記 - Author:まさる(高野 将、TAKANO Sho)

目次

Blog 利用状況

ニュース

著書

2010/7発売


Web掲載記事

@IT

.NET開発を始めるVB6プログラマーが知るべき9のこと

CodeZine

実例で学ぶASP.NET Webフォーム業務アプリケーション開発のポイント

第1回 3層データバインドを正しく活用しよう(前編)

ブログパーツ


書庫

日記カテゴリ

コミュニティ

デザインパターンを学ぶ~その10:ちょっとだけObserverパターン~

ちょっと電卓祭他で間が空きましたが、このシリーズ再開します。

 

さて、前回に引き続き今回はその4その5でやった「Observerパターン」のコードをVBに直してみました。

 

まずは「その4」でやった、普通にInterfaceを用いたObserverパターン。

 

VB Code
' Observerインタフェイス
Public Interface IObserver
    ' 通知
    Sub Notify(ByVal fromAddress As String _
        , ByVal toAddress As String, ByVal subject As String)
End Interface

' Subjectインタフェイス
Public Interface ISubject
    ' Observer登録
    Sub RegisterObserver(ByVal observer As IObserver)
    ' Observer削除
    Sub RemoveObserver(ByVal observer As IObserver)
    ' Observer通知
    Sub NotifyObserver()
End Interface

' メール管理クラス
Public Class MailManager
    Implements ISubject

    ' IObserverリスト
    Private _observerList As List(Of IObserver)

    ' プライベートメンバ
    Private _fromAddress As String
    Private _toAddress As String
    Private _subject As String

    ' コンストラクタ
    Public Sub New()
        _observerList = New List(Of IObserver)()
    End Sub

    ' Observer登録
    Public Sub RegisterObserver(ByVal observer As IObserver) _
        Implements ISubject.RegisterObserver
        _observerList.Add(observer)
    End Sub

    ' Observer削除
    Public Sub RemoveObserver(ByVal observer As IObserver) _
        Implements ISubject.RemoveObserver
        _observerList.Remove(observer)
    End Sub

    ' Observer通知
    Public Sub NotifyObserver() Implements ISubject.NotifyObserver
        For Each observer As IObserver In _observerList
            observer.Notify(_fromAddress, _toAddress, _subject)
        Next
    End Sub

    ' データ設定
    Public Sub SetData(ByVal fromAddress As String _
        , ByVal toAddress As String, ByVal subject As String)
        _fromAddress = fromAddress
        _toAddress = toAddress
        _subject = subject

        ' Observerに通知
        NotifyObserver()
    End Sub
End Class

' Faxクラス
Public Class Fax
    Implements IObserver

    ' 通知
    Public Sub Notify(ByVal fromAddress As String _
        , ByVal toAddress As String, ByVal subject As String) _
        Implements IObserver.Notify

        Console.WriteLine("Faxを送信します。")
        Console.WriteLine("From:{0}, To:{1}, Subject:{2}" _
            , fromAddress, toAddress, subject)
    End Sub
End Class

' Printerクラス
Public Class Printer
    Implements IObserver

    ' 通知
    Public Sub Notify(ByVal fromAddress As String _
        , ByVal toAddress As String, ByVal subject As String) _
        Implements IObserver.Notify

        Console.WriteLine("印刷します。")
        Console.WriteLine("From:{0}, To:{1}, Subject:{2}" _
            , fromAddress, toAddress, subject)
    End Sub
End Class

Public Shared Sub Main(ByVal args As String())
    ' MailManagerインスタンス生成
    Dim mm As New MailManager()

    ' Faxを登録
    Dim fax As New Fax()
    mm.RegisterObserver(fax)

    ' データ設定、通知
    Console.WriteLine("1回目")
    mm.SetData("わんくま同盟", "まさる", "勉強会の連絡")

    ' Printerを登録
    Dim printer As New Printer()
    mm.RegisterObserver(printer)

    ' データ設定、通知
    Console.WriteLine("2回目")
    mm.SetData("わんくま同盟", "まさる", "補足")

    ' Faxを削除
    mm.RemoveObserver(fax)

    ' データ設定、通知
    Console.WriteLine("3回目")
    mm.SetData("わんくま同盟", "まさる", "訂正")

    Console.ReadLine()

End Sub

実行結果

1回目
Faxを送信します。
From:わんくま同盟, To:まさる, Subject:勉強会の連絡
2回目
Faxを送信します。
From:わんくま同盟, To:まさる, Subject:補足
印刷します。
From:わんくま同盟, To:まさる, Subject:補足
3回目
印刷します。
From:わんくま同盟, To:まさる, Subject:訂正

 

次に「その5」でやった、イベントを使ったObserverパターン。

 

VB Code

' NewMailイベント引数クラス
Public Class NewMailEventArgs
    Inherits EventArgs
    Private ReadOnly _fromAddress As String
    Private ReadOnly _toAddress As String
    Private ReadOnly _subject As String

    ' コンストラクタ
    Public Sub New( _
       ByVal fromAddress As String _
     , ByVal toAddress As String _
     , ByVal subject As String _
    )
        Me._fromAddress = fromAddress
        Me._toAddress = toAddress
        Me._subject = subject
    End Sub

    Public ReadOnly Property FromAddress() As String
        Get
            Return Me._fromAddress
        End Get
    End Property

    Public ReadOnly Property ToAddress() As String
        Get
            Return Me._toAddress
        End Get
    End Property

    Public ReadOnly Property Subject() As String
        Get
            Return Me._subject
        End Get
    End Property

End Class

' メール管理クラス
Public Class MailManager

    ' イベント定義
    Public Event NewMail As EventHandler(Of NewMailEventArgs)

    ' 通知
    Private Sub Notify(ByVal e As NewMailEventArgs)
        ' イベント発生
        RaiseEvent NewMail(Me, e)
    End Sub

    ' データ設定
    Public Sub SetData( _
       ByVal fromAddress As String _
     , ByVal toAddress As String _
     , ByVal subject As String _
    )
        ' NewMailEventArgs生成
        Dim e As New NewMailEventArgs(fromAddress, toAddress, subject)

        ' 通知
        Notify(e)
    End Sub

End Class

' Faxクラス
Public Class Fax

    ' 通知
    Public Shared Sub Notify(ByVal sender As Object, ByVal e As NewMailEventArgs)
        Console.WriteLine("Faxを送信します。")
        Console.WriteLine("From:{0}, To:{1}, Subject:{2}" _
         , e.FromAddress, e.ToAddress, e.Subject)
    End Sub

End Class

' Printerクラス
Public Class Printer

    ' 通知
    Public Shared Sub Notify(ByVal sender As Object, ByVal e As NewMailEventArgs)
        Console.WriteLine("印刷します。")
        Console.WriteLine("From:{0}, To:{1}, Subject:{2}" _
         , e.FromAddress, e.ToAddress, e.Subject)
    End Sub

End Class

Public Shared Sub Main(ByVal args As String())

    ' MailManagerインスタンス生成
    Dim mm As New MailManager()

    ' Faxを登録
    AddHandler mm.NewMail, AddressOf Fax.Notify

    ' データ設定、通知
    Console.WriteLine("1回目")
    mm.SetData("わんくま同盟", "まさる", "勉強会の連絡")

    ' Printerを登録
    AddHandler mm.NewMail, AddressOf Printer.Notify

    ' データ設定、通知
    Console.WriteLine("2回目")
    mm.SetData("わんくま同盟", "まさる", "補足")

    ' Faxを削除
    RemoveHandler mm.NewMail, AddressOf Fax.Notify

    ' データ設定、通知
    Console.WriteLine("3回目")
    mm.SetData("わんくま同盟", "まさる", "訂正")

    Console.ReadLine()

End Sub

実行結果

1回目
1回目
Faxを送信します。
From:わんくま同盟, To:まさる, Subject:勉強会の連絡
2回目
Faxを送信します。
From:わんくま同盟, To:まさる, Subject:補足
印刷します。
From:わんくま同盟, To:まさる, Subject:補足
3回目
印刷します。
From:わんくま同盟, To:まさる, Subject:訂正

 

いかがでしょ?Interfaceを用いたほうはあんまりC#と変わりませんね。

イベントの方は、

  • MailManager.Notifyメソッド内で、C#はデリゲートをそのまま呼び出しているが、VBはRaiseEventでイベントを発生させている。
  • イベントの登録がC#は+=、-=演算子で行うが、VBはAddHandler、RemoveHandler、およびAddressOfキーワードを使って行う。

といったところがC#とはちょっと違いますね。

 

このように、C#とVBの違いを覚えておくと、つぶしが利くようになりますので、初心者の皆様は両方でコードを書いてみることをお勧めします。

みんなで(プログラミング言語くらいは)マルチリンガルになりましょう。

#ホントは英語も覚えてバイリンガルになりたいですけど、なかなかむずかしいっすorz

投稿日時 : 2007年8月2日 21:36

Feedback

# re: デザインパターンを学ぶ~その10:ちょっとだけObserverパターン~ 2007/08/03 15:38 凪瀬

マルチリンガルなプログラム解説書ってあまり見かけないですからね。
購入するターゲットが少ないだろうから本にし難いのかな。
サンプルプログラムの言語間の翻訳も結構大変だしなぁ。

# re: デザインパターンを学ぶ~その10:ちょっとだけObserverパターン~ 2007/08/03 16:37 まさる

>サンプルプログラムの言語間の翻訳も結構大変だしなぁ。
今回のC#→VBですらVBの構文思い出すのにちょっとだけ苦労しましたねぇ。
これがさらに他の言語となると、もっともっと違うところが出てくるでしょうし、大変でしょうね。

#そう考えるとじゃんぬさんのサイトはスゴイ!

# oxlGnZcxmXoU 2011/12/29 21:06 http://www.healthinter.org/health/page/clomid.php

I decided to help and sent a post to the social bookmarks. I hope to raise it in popularity!!...

# I'm not sure where you're getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for wonderful information I was looking for this information for my mission. 2019/05/12 7:28 I'm not sure where you're getting your info, but g

I'm not sure where you're getting your info, but great topic.
I needs to spend some time learning much more or understanding more.
Thanks for wonderful information I was looking for this information for
my mission.

# Thanks for finally talking about >デザインパターンを学ぶ~その10:ちょっとだけObserverパターン~ <Liked it! 2019/05/30 6:24 Thanks for finally talking about >デザインパターンを学ぶ~そ

Thanks for finally talking about >デザインパターンを学ぶ~その10:ちょっとだけObserverパターン~ <Liked it!

# Howdy just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Firefox. I'm not sure if this is a formatting issue or something to do with web browser compatibility but I thought I'd post to let you know. 2019/06/05 2:26 Howdy just wanted to give you a quick heads up. Th

Howdy just wanted to give you a quick heads up. The text in your article
seem to be running off the screen in Firefox.
I'm not sure if this is a formatting issue or something
to do with web browser compatibility but I thought I'd post to let you
know. The style and design look great though! Hope you get the issue
solved soon. Kudos

# There is certainly a great deal to know about this issue. I like all the points you made. 2019/06/07 3:38 There is certainly a great deal to know about this

There is certainly a great deal to know about this issue.
I like all the points you made.

# Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed. There was a hermit crab inside 2019/06/23 15:15 Today, I went to the beachfront with my kids. I fo

Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year
old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed.
There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

タイトル
名前
Url
コメント