Skip to content

Commit 057a165

Browse files
committed
观察者模式
1 parent 7748944 commit 057a165

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ObserverPattern
8+
{
9+
/// <summary>
10+
/// 抽象的观察者
11+
/// </summary>
12+
abstract class Observer
13+
{
14+
public abstract void Update();
15+
}
16+
17+
class ConcreteObserver : Observer
18+
{
19+
private string observerName;
20+
private string State;
21+
private Subject subject;
22+
23+
24+
public Subject GetSubject()
25+
{
26+
return this.subject;
27+
}
28+
29+
public void SetSubject(Subject subject)
30+
{
31+
this.subject = subject;
32+
}
33+
34+
public ConcreteObserver(Subject subject, string observerName)
35+
{
36+
this.subject = subject;
37+
this.observerName = observerName;
38+
}
39+
40+
public override void Update()
41+
{
42+
throw new NotImplementedException();
43+
}
44+
}
45+
}

DesignPatterns/ObserverPattern/ObserverPattern/ObserverPattern.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@
4343
</ItemGroup>
4444
<ItemGroup>
4545
<Compile Include="Country.cs" />
46+
<Compile Include="Observer.cs" />
4647
<Compile Include="Program.cs" />
4748
<Compile Include="Properties\AssemblyInfo.cs" />
4849
<Compile Include="Spy.cs" />
50+
<Compile Include="Subject.cs" />
4951
</ItemGroup>
5052
<ItemGroup>
5153
<None Include="App.config" />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ObserverPattern
8+
{
9+
abstract class Subject
10+
{
11+
private List<Observer>
12+
}
13+
}

0 commit comments

Comments
 (0)