Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0a61a0

Browse files
Add Oberser & Template Method pattern.
1 parent 81728a9 commit a0a61a0

File tree

14 files changed

+276
-16
lines changed

14 files changed

+276
-16
lines changed

‎Command/Calculator.cs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ public void Operation(char @operator, int operand)
1818
case '*': _curr *= operand; break;
1919
case '/': _curr /= operand; break;
2020
}
21-
Console.WriteLine(
22-
"Current value = {0,3} (following {1} {2})",
23-
_curr, @operator, operand);
21+
Console.WriteLine("Current value = {0,3} (following {1} {2})", _curr, @operator, operand);
2422
}
2523
}
2624
}

‎Observer/IBM.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Observer.RealWorld
8+
{
9+
class IBM : Stock
10+
{
11+
public IBM(string symbol, double price) : base(symbol, price)
12+
{
13+
}
14+
}
15+
}

‎Observer/Program.cs‎ renamed to ‎Observer/IInverstor.cs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
using System.Text;
55
using System.Threading.Tasks;
66

7-
namespace Observer
7+
namespace DoFactory.GangOfFour.Observer.RealWorld
88
{
9-
classProgram
9+
interfaceIInvestor
1010
{
11-
static void Main(string[] args)
12-
{
13-
}
11+
void Update(Stock stock);
1412
}
1513
}

‎Observer/Inverstor.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Observer.RealWorld
8+
{
9+
class Investor : IInvestor
10+
{
11+
private string _name;
12+
private Stock _stock;
13+
14+
public Investor(string name)
15+
{
16+
_name = name;
17+
}
18+
19+
public void Update(Stock stock)
20+
{
21+
Console.WriteLine("Notified {0} of {1}'s " + "change to {2:C}", _name, stock.Symbol, stock.Price);
22+
}
23+
24+
public Stock Stock
25+
{
26+
get
27+
{
28+
return _stock;
29+
}
30+
set
31+
{
32+
_stock = value;
33+
}
34+
}
35+
}
36+
}

‎Observer/Observer.csproj‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,19 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Program.cs" />
45+
<Compile Include="IBM.cs" />
46+
<Compile Include="IInverstor.cs" />
47+
<Compile Include="Inverstor.cs" />
48+
<Compile Include="Program.RealWorldCode.cs" />
4649
<Compile Include="Properties\AssemblyInfo.cs" />
50+
<Compile Include="Stock.cs" />
4751
</ItemGroup>
4852
<ItemGroup>
4953
<None Include="App.config" />
5054
</ItemGroup>
55+
<ItemGroup>
56+
<Content Include="observer.gif" />
57+
</ItemGroup>
5158
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5259
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5360
Other similar extension points exist, see Microsoft.Common.targets.

‎Observer/Program.RealWorldCode.cs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
namespace DoFactory.GangOfFour.Observer.RealWorld
4+
{
5+
class MainApp
6+
{
7+
static void Main()
8+
{
9+
IBM ibm = new IBM("IBM", 120.00);
10+
ibm.Attach(new Investor("Sorros"));
11+
ibm.Attach(new Investor("Berkshire"));
12+
ibm.Price = 120.10;
13+
ibm.Price = 121.00;
14+
ibm.Price = 120.50;
15+
ibm.Price = 120.75;
16+
Console.ReadKey();
17+
}
18+
}
19+
}

‎Observer/Stock.cs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Observer.RealWorld
8+
{
9+
abstract class Stock
10+
{
11+
private string _symbol; private double _price;
12+
private List<IInvestor> _investors = new List<IInvestor>();
13+
14+
public Stock(string symbol, double price)
15+
{
16+
_symbol = symbol; _price = price;
17+
}
18+
19+
public void Attach(IInvestor investor)
20+
{
21+
_investors.Add(investor);
22+
}
23+
24+
public void Detach(IInvestor investor)
25+
{
26+
_investors.Remove(investor);
27+
}
28+
29+
public void Notify()
30+
{
31+
foreach (IInvestor investor in _investors)
32+
{
33+
investor.Update(this);
34+
}
35+
Console.WriteLine("");
36+
}
37+
38+
public double Price
39+
{
40+
get
41+
{
42+
return _price;
43+
}
44+
set
45+
{
46+
if (_price != value)
47+
{
48+
_price = value;
49+
Notify();
50+
}
51+
}
52+
}
53+
54+
public string Symbol
55+
{
56+
get
57+
{
58+
return _symbol;
59+
}
60+
}
61+
}
62+
}

‎Observer/observer.gif‎

8.8 KB
Loading[フレーム]

‎Template Method/Categories.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.OleDb;
4+
5+
namespace DoFactory.GangOfFour.Template.RealWorld
6+
{
7+
/// <summary>
8+
/// A 'ConcreteClass' class
9+
/// </summary>
10+
class Categories : DataAccessObject
11+
{
12+
public override void Select()
13+
{
14+
string sql = "select CategoryName from Categories";
15+
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, connectionString);
16+
17+
dataSet = new DataSet();
18+
dataAdapter.Fill(dataSet, "Categories");
19+
}
20+
21+
public override void Process()
22+
{
23+
Console.WriteLine("Categories ---- ");
24+
25+
DataTable dataTable = dataSet.Tables["Categories"];
26+
foreach (DataRow row in dataTable.Rows)
27+
{
28+
Console.WriteLine(row["CategoryName"]);
29+
}
30+
Console.WriteLine();
31+
}
32+
}
33+
}

‎Template Method/DataAccessObject.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Data;
2+
3+
namespace DoFactory.GangOfFour.Template.RealWorld
4+
{
5+
/// <summary>
6+
/// The 'AbstractClass' abstract class
7+
/// </summary>
8+
abstract class DataAccessObject
9+
{
10+
protected string connectionString;
11+
protected DataSet dataSet;
12+
13+
public virtual void Connect()
14+
{
15+
// Make sure mdb is available to app
16+
connectionString =
17+
"provider=Microsoft.JET.OLEDB.4.0; " +
18+
"data source=..\\..\\..\\db1.mdb";
19+
}
20+
21+
public abstract void Select();
22+
public abstract void Process();
23+
24+
public virtual void Disconnect()
25+
{
26+
connectionString = "";
27+
}
28+
29+
// The 'Template Method'
30+
public void Run()
31+
{
32+
Connect();
33+
Select();
34+
Process();
35+
Disconnect();
36+
}
37+
}
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /