Skip to content

Commit a0a61a0

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

14 files changed

+276
-16
lines changed

Command/Calculator.cs

+1-3
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

+15
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 Observer/IInverstor.cs

+3-5
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-
class Program
9+
interface IInvestor
1010
{
11-
static void Main(string[] args)
12-
{
13-
}
11+
void Update(Stock stock);
1412
}
1513
}

Observer/Inverstor.cs

+36
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

+8-1
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

+19
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

+62
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

+33
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

+38
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+
}

Template Method/Products.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 Products : DataAccessObject
11+
{
12+
public override void Select()
13+
{
14+
string sql = "select ProductName from Products";
15+
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, connectionString);
16+
17+
dataSet = new DataSet();
18+
dataAdapter.Fill(dataSet, "Products");
19+
}
20+
21+
public override void Process()
22+
{
23+
Console.WriteLine("Products ---- ");
24+
DataTable dataTable = dataSet.Tables["Products"];
25+
foreach (DataRow row in dataTable.Rows)
26+
{
27+
Console.WriteLine(row["ProductName"]);
28+
}
29+
Console.WriteLine();
30+
}
31+
}
32+
}

Template Method/Program.cs

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

7-
namespace Template_Method
3+
/// <summary>
4+
/// Definition:
5+
/// Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
6+
/// Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
7+
/// </summary>
8+
namespace DoFactory.GangOfFour.Template.RealWorld
89
{
9-
class Program
10+
/// <summary>
11+
/// MainApp startup class for Real-World
12+
/// Template Design Pattern.
13+
/// </summary>
14+
class MainApp
1015
{
11-
static void Main(string[] args)
16+
/// <summary>
17+
/// Entry point into console application.
18+
/// </summary>
19+
static void Main()
1220
{
21+
DataAccessObject daoCategories = new Categories();
22+
daoCategories.Run();
23+
24+
DataAccessObject daoProducts = new Products();
25+
daoProducts.Run();
26+
27+
// Wait for user
28+
Console.ReadKey();
1329
}
1430
}
1531
}

Template Method/Template Method.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45+
<Compile Include="Categories.cs" />
46+
<Compile Include="DataAccessObject.cs" />
47+
<Compile Include="Products.cs" />
4548
<Compile Include="Program.cs" />
4649
<Compile Include="Properties\AssemblyInfo.cs" />
4750
</ItemGroup>
4851
<ItemGroup>
4952
<None Include="App.config" />
5053
</ItemGroup>
54+
<ItemGroup>
55+
<Content Include="template.gif" />
56+
</ItemGroup>
5157
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5258
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5359
Other similar extension points exist, see Microsoft.Common.targets.

Template Method/template.gif

5.5 KB
Loading

0 commit comments

Comments
 (0)