Skip to content

Commit 3cfac47

Browse files
committed
update BridgePattern
1 parent d8e703f commit 3cfac47

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

StructurePattern/BridgePattern/Program.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ public class Program
66
{
77
public static void Main(string[] args)
88
{
9+
#region Prototype
10+
11+
Abstraction ab = new RefinedAbstraction();
12+
13+
ab.SetImplementor(new ConcreteImplementorA());
14+
ab.Operation();
15+
16+
ab.SetImplementor(new ConcreteImplementorB());
17+
ab.Operation();
18+
19+
Console.WriteLine();
20+
21+
#endregion Prototype
22+
923
HandsetBrand handsetBrand = new HandsetBrandM();
1024
handsetBrand.SetHandsetSoft(new HandsetMp3());
1125
handsetBrand.Run();

StructurePattern/BridgePattern/Prototype.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ internal class ConcreteImplementorA : Implementor
1111
{
1212
public override void Operation()
1313
{
14-
Console.WriteLine("ImplementorA Opearation");
14+
Console.WriteLine("ImplementorA Operation");
1515
}
1616
}
1717

1818
internal class ConcreteImplementorB : Implementor
1919
{
2020
public override void Operation()
2121
{
22-
Console.WriteLine("ImplementorB Opearation");
22+
Console.WriteLine("ImplementorB Operation");
2323
}
2424
}
2525

@@ -35,12 +35,12 @@ public void SetImplementor(Implementor implementor)
3535
public abstract void Operation();
3636
}
3737

38-
internal class RefindAbstraction : Abstraction
38+
internal class RefinedAbstraction : Abstraction
3939
{
4040
public override void Operation()
4141
{
4242
Implementor.Operation();
43-
Console.WriteLine("Invoke in RefindAbstraction");
43+
Console.WriteLine("Invoke in RefinedAbstraction");
4444
}
4545
}
4646
}

StructurePattern/BridgePattern/README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,64 @@
22

33
## Intro
44

5-
桥接模式
5+
> 桥接模式,将抽象部分与它的实现部分分离,使得它们都可以独立地变化。
6+
>
7+
> 这里说到抽象与它的实现分离指的是抽象类和它的派生类用来实现自己的对象
8+
9+
## Sample
10+
11+
``` csharp
12+
internal abstract class Implementor
13+
{
14+
public abstract void Operation();
15+
}
16+
17+
internal class ConcreteImplementorA : Implementor
18+
{
19+
public override void Operation()
20+
{
21+
Console.WriteLine("ImplementorA Operation");
22+
}
23+
}
24+
25+
internal class ConcreteImplementorB : Implementor
26+
{
27+
public override void Operation()
28+
{
29+
Console.WriteLine("ImplementorB Operation");
30+
}
31+
}
32+
33+
internal abstract class Abstraction
34+
{
35+
protected Implementor Implementor;
36+
37+
public void SetImplementor(Implementor implementor)
38+
{
39+
Implementor = implementor;
40+
}
41+
42+
public abstract void Operation();
43+
}
44+
45+
internal class RefinedAbstraction : Abstraction
46+
{
47+
public override void Operation()
48+
{
49+
Implementor.Operation();
50+
Console.WriteLine("Invoke in RefinedAbstraction");
51+
}
52+
}
53+
54+
55+
Abstraction ab = new RefinedAbstraction();
56+
57+
ab.SetImplementor(new ConcreteImplementorA());
58+
ab.Operation();
59+
60+
ab.SetImplementor(new ConcreteImplementorB());
61+
ab.Operation();
62+
```
663

764
## More
865

0 commit comments

Comments
 (0)