練習用です。いろんなものがごちゃまぜです。
(メッセージはありません)
@@ -0,0 +1,131 @@ | ||
1 | +Namespace GUI | |
2 | + | |
3 | + <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ | |
4 | + Partial Class StateForm | |
5 | + Inherits System.Windows.Forms.Form | |
6 | + | |
7 | + Public Sub New() | |
8 | + MyBase.New() | |
9 | + | |
10 | + ' この呼び出しは Windows フォーム デザイナで必要です。 | |
11 | + InitializeComponent() | |
12 | + End Sub | |
13 | + | |
14 | + ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。 | |
15 | + Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) | |
16 | + If disposing Then | |
17 | + If Not (components Is Nothing) Then | |
18 | + components.Dispose() | |
19 | + End If | |
20 | + End If | |
21 | + MyBase.Dispose(disposing) | |
22 | + End Sub | |
23 | + | |
24 | + ' Windows フォーム デザイナで必要です。 | |
25 | + Private components As System.ComponentModel.IContainer | |
26 | + | |
27 | + ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 | |
28 | + ' Windows フォーム デザイナを使って変更してください。 | |
29 | + ' コード エディタは使用しないでください。 | |
30 | + Friend WithEvents _clockTimer As System.Windows.Forms.Timer | |
31 | + Friend WithEvents _clockTextBox As System.Windows.Forms.TextBox | |
32 | + Friend WithEvents _screenTextBox As System.Windows.Forms.TextBox | |
33 | + Friend WithEvents _useButton As System.Windows.Forms.Button | |
34 | + Friend WithEvents _alarmButton As System.Windows.Forms.Button | |
35 | + Friend WithEvents _phoneButton As System.Windows.Forms.Button | |
36 | + Friend WithEvents _exitButton As System.Windows.Forms.Button | |
37 | + <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() | |
38 | + Me.components = New System.ComponentModel.Container() | |
39 | + Me._clockTextBox = New System.Windows.Forms.TextBox() | |
40 | + Me._screenTextBox = New System.Windows.Forms.TextBox() | |
41 | + Me._useButton = New System.Windows.Forms.Button() | |
42 | + Me._alarmButton = New System.Windows.Forms.Button() | |
43 | + Me._phoneButton = New System.Windows.Forms.Button() | |
44 | + Me._exitButton = New System.Windows.Forms.Button() | |
45 | + Me._clockTimer = New System.Windows.Forms.Timer(Me.components) | |
46 | + Me.SuspendLayout() | |
47 | + ' | |
48 | + '_clockTextBox | |
49 | + ' | |
50 | + Me._clockTextBox.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ | |
51 | + Or System.Windows.Forms.AnchorStyles.Right) | |
52 | + Me._clockTextBox.Location = New System.Drawing.Point(8, 8) | |
53 | + Me._clockTextBox.Name = "_clockTextBox" | |
54 | + Me._clockTextBox.ReadOnly = True | |
55 | + Me._clockTextBox.Size = New System.Drawing.Size(344, 19) | |
56 | + Me._clockTextBox.TabIndex = 0 | |
57 | + Me._clockTextBox.Text = "" | |
58 | + ' | |
59 | + '_screenTextBox | |
60 | + ' | |
61 | + Me._screenTextBox.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ | |
62 | + Or System.Windows.Forms.AnchorStyles.Left) _ | |
63 | + Or System.Windows.Forms.AnchorStyles.Right) | |
64 | + Me._screenTextBox.Location = New System.Drawing.Point(8, 32) | |
65 | + Me._screenTextBox.Multiline = True | |
66 | + Me._screenTextBox.Name = "_screenTextBox" | |
67 | + Me._screenTextBox.ReadOnly = True | |
68 | + Me._screenTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical | |
69 | + Me._screenTextBox.Size = New System.Drawing.Size(344, 240) | |
70 | + Me._screenTextBox.TabIndex = 1 | |
71 | + Me._screenTextBox.Text = "" | |
72 | + ' | |
73 | + '_useButton | |
74 | + ' | |
75 | + Me._useButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) | |
76 | + Me._useButton.Location = New System.Drawing.Point(8, 280) | |
77 | + Me._useButton.Name = "_useButton" | |
78 | + Me._useButton.Size = New System.Drawing.Size(80, 24) | |
79 | + Me._useButton.TabIndex = 2 | |
80 | + Me._useButton.Text = "金庫使用" | |
81 | + ' | |
82 | + '_alarmButton | |
83 | + ' | |
84 | + Me._alarmButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) | |
85 | + Me._alarmButton.Location = New System.Drawing.Point(96, 280) | |
86 | + Me._alarmButton.Name = "_alarmButton" | |
87 | + Me._alarmButton.Size = New System.Drawing.Size(80, 24) | |
88 | + Me._alarmButton.TabIndex = 3 | |
89 | + Me._alarmButton.Text = "非常ベル" | |
90 | + ' | |
91 | + '_phoneButton | |
92 | + ' | |
93 | + Me._phoneButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) | |
94 | + Me._phoneButton.Location = New System.Drawing.Point(184, 280) | |
95 | + Me._phoneButton.Name = "_phoneButton" | |
96 | + Me._phoneButton.Size = New System.Drawing.Size(80, 24) | |
97 | + Me._phoneButton.TabIndex = 4 | |
98 | + Me._phoneButton.Text = "通常通話" | |
99 | + ' | |
100 | + '_exitButton | |
101 | + ' | |
102 | + Me._exitButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) | |
103 | + Me._exitButton.Location = New System.Drawing.Point(272, 280) | |
104 | + Me._exitButton.Name = "_exitButton" | |
105 | + Me._exitButton.Size = New System.Drawing.Size(80, 24) | |
106 | + Me._exitButton.TabIndex = 5 | |
107 | + Me._exitButton.Text = "終了" | |
108 | + ' | |
109 | + '_clockTimer | |
110 | + ' | |
111 | + Me._clockTimer.Interval = 1000 | |
112 | + ' | |
113 | + 'SafeForm | |
114 | + ' | |
115 | + Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12) | |
116 | + Me.ClientSize = New System.Drawing.Size(360, 310) | |
117 | + Me.Controls.AddRange(New System.Windows.Forms.Control() {Me._exitButton, _ | |
118 | + Me._phoneButton, _ | |
119 | + Me._alarmButton, _ | |
120 | + Me._useButton, _ | |
121 | + Me._screenTextBox, _ | |
122 | + Me._clockTextBox}) | |
123 | + Me.Name = "SafeForm" | |
124 | + Me.Text = "State Sample" | |
125 | + Me.ResumeLayout(False) | |
126 | + | |
127 | + End Sub | |
128 | + | |
129 | + End Class | |
130 | + | |
131 | +End Namespace | |
\ No newline at end of file |
@@ -0,0 +1,94 @@ | ||
1 | +Imports vbGofSample.State.Framework | |
2 | + | |
3 | +Namespace GUI | |
4 | + | |
5 | + Public Class StateForm | |
6 | + Implements IContext | |
7 | + | |
8 | + | |
9 | + Private _state As IState = DayState.GetInstance | |
10 | + Private _now As Integer = 0 | |
11 | + | |
12 | + | |
13 | + ' フォームロード時の処理を行います。 | |
14 | + Private Sub SafeForm_Load(ByVal sender As System.Object, _ | |
15 | + ByVal e As System.EventArgs) Handles MyBase.Load | |
16 | + _clockTimer.Enabled = True | |
17 | + End Sub | |
18 | + | |
19 | + ' フォームクローズ時の処理を行います。 | |
20 | + Private Sub SafeForm_Closed(ByVal sender As Object, _ | |
21 | + ByVal e As System.EventArgs) Handles MyBase.Closed | |
22 | + _clockTimer.Enabled = False | |
23 | + End Sub | |
24 | + | |
25 | + ' タイマーイベント時の処理を行います。 | |
26 | + Private Sub ClockTimer_Tick(ByVal sender As System.Object, _ | |
27 | + ByVal e As System.EventArgs) Handles _clockTimer.Tick | |
28 | + If (_now >= 23) Then _now = 0 | |
29 | + | |
30 | + SetClock(_now) | |
31 | + | |
32 | + _now += 1 | |
33 | + End Sub | |
34 | + | |
35 | + Private Sub _useButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _useButton.Click | |
36 | + _state.DoUse(Me) | |
37 | + End Sub | |
38 | + | |
39 | + Private Sub _alarmButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _alarmButton.Click | |
40 | + _state.DoAlarm(Me) | |
41 | + End Sub | |
42 | + | |
43 | + Private Sub _phoneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _phoneButton.Click | |
44 | + _state.DoPhone(Me) | |
45 | + End Sub | |
46 | + | |
47 | + Private Sub _exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _exitButton.Click | |
48 | + Close() | |
49 | + End Sub | |
50 | + | |
51 | + ' 時刻を設定します。 | |
52 | + Public Sub SetClock(ByVal hour As Integer) Implements IContext.SetClock | |
53 | + Dim clockString As String = "現在時刻は" | |
54 | + | |
55 | + If (hour < 10) Then | |
56 | + clockString &= String.Format("0{0}:00", hour) | |
57 | + Else | |
58 | + clockString &= String.Format("{0}:00", hour) | |
59 | + End If | |
60 | + | |
61 | + Console.WriteLine(clockString) | |
62 | + _clockTextBox.Text = clockString | |
63 | + | |
64 | + _state.DoClock(Me, hour) | |
65 | + End Sub | |
66 | + | |
67 | + ' 状態を変化させます。 | |
68 | + Public Sub ChageState(ByVal state As IState) Implements IContext.ChageState | |
69 | + Console.Write(_state) | |
70 | + Console.Write("から") | |
71 | + Console.Write(state) | |
72 | + Console.Write("へ状態が変化しました。") | |
73 | + Console.WriteLine() | |
74 | + | |
75 | + _state = state | |
76 | + End Sub | |
77 | + | |
78 | + ' 警備センターを呼び出します。 | |
79 | + Public Sub CallSecurityCenter(ByVal message As String) Implements IContext.CallSecurityCenter | |
80 | + _screenTextBox.AppendText(String.Format("call ! {0}", message) & vbCrLf) | |
81 | + End Sub | |
82 | + | |
83 | + ' 警備センターの記録です。 | |
84 | + Public Sub RecordLog(ByVal message As String) Implements IContext.RecordLog | |
85 | + _screenTextBox.AppendText(String.Format("record ... {0}", message) & vbCrLf) | |
86 | + End Sub | |
87 | + | |
88 | + | |
89 | + | |
90 | + | |
91 | + End Class | |
92 | + | |
93 | +End Namespace | |
94 | + |
@@ -0,0 +1,21 @@ | ||
1 | +Namespace Framework | |
2 | + | |
3 | + ' IStateインターフェイスです。 | |
4 | + Public Interface IState | |
5 | + | |
6 | + ' 時刻を設定します。 | |
7 | + Sub DoClock(ByVal context As IContext, ByVal hour As Integer) | |
8 | + | |
9 | + ' 金庫を使用します。 | |
10 | + Sub DoUse(ByVal context As IContext) | |
11 | + | |
12 | + ' 非常ベルを鳴らします。 | |
13 | + Sub DoAlarm(ByVal context As IContext) | |
14 | + | |
15 | + ' 通常の通話を行います。 | |
16 | + Sub DoPhone(ByVal context As IContext) | |
17 | + | |
18 | + End Interface | |
19 | + | |
20 | +End Namespace | |
21 | + |
@@ -0,0 +1,49 @@ | ||
1 | +Namespace Framework | |
2 | + | |
3 | + | |
4 | + ' NightStateクラスです。 | |
5 | + Public Class NightState | |
6 | + Implements IState | |
7 | + | |
8 | + Private Shared _singleton As NightState = New NightState() | |
9 | + | |
10 | + ' コンストラクタです。(外部からのアクセス不可) | |
11 | + Private Sub New() | |
12 | + | |
13 | + End Sub | |
14 | + | |
15 | + ' 唯一のインスタンスを取得します。 | |
16 | + Public Shared Function GetInstance() As IState | |
17 | + Return _singleton | |
18 | + End Function | |
19 | + | |
20 | + ' 時刻を設定します。 | |
21 | + Public Sub DoClock(ByVal context As IContext, ByVal hour As Integer) Implements IState.DoClock | |
22 | + If (9 <= hour AndAlso hour < 17) Then | |
23 | + context.ChageState(DayState.GetInstance) | |
24 | + End If | |
25 | + End Sub | |
26 | + | |
27 | + ' 金庫を使用します。 | |
28 | + Public Sub DoUse(ByVal context As IContext) Implements IState.DoUse | |
29 | + context.CallSecurityCenter("非常: 夜間の金庫使用!!!") | |
30 | + End Sub | |
31 | + | |
32 | + ' 非常ベルを鳴らします。 | |
33 | + Public Sub DoAlarm(ByVal context As IContext) Implements IState.DoAlarm | |
34 | + context.CallSecurityCenter("非常ベル(夜間)") | |
35 | + End Sub | |
36 | + | |
37 | + ' 通常の通話を行います。 | |
38 | + Public Sub DoPhone(ByVal context As IContext) Implements IState.DoPhone | |
39 | + context.RecordLog("夜間の通話録音") | |
40 | + End Sub | |
41 | + | |
42 | + ' NightStateの文字列表現を返します。 | |
43 | + Public Overrides Function ToString() As String | |
44 | + Return "[夜間]" | |
45 | + End Function | |
46 | + | |
47 | + End Class | |
48 | + | |
49 | +End Namespace | |
\ No newline at end of file |
@@ -0,0 +1,20 @@ | ||
1 | +Namespace Framework | |
2 | + | |
3 | + ' IContextインターフェイスです。 | |
4 | + Public Interface IContext | |
5 | + | |
6 | + ' 時刻を設定します。 | |
7 | + Sub SetClock(ByVal hour As Integer) | |
8 | + | |
9 | + ' 状態を変化させます。 | |
10 | + Sub ChageState(ByVal state As IState) | |
11 | + | |
12 | + ' 警備センターを呼び出します。 | |
13 | + Sub CallSecurityCenter(ByVal message As String) | |
14 | + | |
15 | + ' 警備センターの記録です。 | |
16 | + Sub RecordLog(ByVal message As String) | |
17 | + | |
18 | + End Interface | |
19 | + | |
20 | +End Namespace |
@@ -0,0 +1,48 @@ | ||
1 | +Namespace Framework | |
2 | + | |
3 | + | |
4 | + Public Class DayState | |
5 | + Implements IState | |
6 | + | |
7 | + Private Shared _singleton As DayState = New DayState() | |
8 | + | |
9 | + ' コンストラクタです。(外部からのアクセス不可) | |
10 | + Private Sub New() | |
11 | + | |
12 | + End Sub | |
13 | + | |
14 | + ' 唯一のインスタンスを取得します。 | |
15 | + Public Shared Function GetInstance() As IState | |
16 | + Return _singleton | |
17 | + End Function | |
18 | + | |
19 | + ' 時刻を設定します。 | |
20 | + Public Sub DoClock(ByVal context As IContext, ByVal hour As Integer) Implements IState.DoClock | |
21 | + If (hour < 9 OrElse 17 <= hour) Then | |
22 | + context.ChageState(NightState.GetInstance) | |
23 | + End If | |
24 | + End Sub | |
25 | + | |
26 | + ' 金庫を使用します。 | |
27 | + Public Sub DoUse(ByVal context As IContext) Implements IState.DoUse | |
28 | + context.RecordLog("金庫使用(昼間)") | |
29 | + End Sub | |
30 | + | |
31 | + ' 非常ベルを鳴らします。 | |
32 | + Public Sub DoAlarm(ByVal context As IContext) Implements IState.DoAlarm | |
33 | + context.CallSecurityCenter("非常ベル(昼間)") | |
34 | + End Sub | |
35 | + | |
36 | + ' 通常の通話を行います。 | |
37 | + Public Sub DoPhone(ByVal context As IContext) Implements IState.DoPhone | |
38 | + context.CallSecurityCenter("通常の通話(昼間)") | |
39 | + End Sub | |
40 | + | |
41 | + ' DayStateの文字列表現を返します。 | |
42 | + Public Overrides Function ToString() As String | |
43 | + Return "[昼間]" | |
44 | + End Function | |
45 | + | |
46 | + End Class | |
47 | + | |
48 | +End Namespace | |
\ No newline at end of file |