Skip to content

Commit f406906

Browse files
rickywoabishekaditya
authored andcommitted
Bridge Pattern (#10)
* Bridge Pattern * Update README for Bridge Pattern
1 parent 07dd2f3 commit f406906

File tree

10 files changed

+177
-0
lines changed

10 files changed

+177
-0
lines changed

BridgePattern/BridgePattern.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

BridgePattern/FlyingEnchantment.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace BridgePattern
4+
{
5+
public class FlyingEnchantment:IEnchantment
6+
{
7+
public void OnActivate()
8+
{
9+
Console.WriteLine("The item begins to glow faintly.");
10+
}
11+
12+
public void Apply()
13+
{
14+
Console.WriteLine("The item flies and strikes the enemies finally returning to owner's hand.");
15+
}
16+
17+
public void OnDeactivate()
18+
{
19+
Console.WriteLine("The item's glow fades.");
20+
}
21+
}
22+
}

BridgePattern/Hammer.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace BridgePattern
4+
{
5+
public class Hammer:IWeapon
6+
{
7+
private readonly IEnchantment _enchantment;
8+
public Hammer(IEnchantment enchantment)
9+
{
10+
_enchantment = enchantment;
11+
}
12+
13+
public void Wield()
14+
{
15+
Console.WriteLine("The hammer is wielded.");
16+
_enchantment.OnActivate();
17+
}
18+
19+
public void Swing()
20+
{
21+
Console.WriteLine("The hammer is swinged.");
22+
_enchantment.Apply();
23+
}
24+
25+
public void Unwield()
26+
{
27+
Console.WriteLine("The hammer is unwielded.");
28+
_enchantment.OnDeactivate();
29+
}
30+
31+
public IEnchantment GetEnchantment()
32+
{
33+
return _enchantment;
34+
}
35+
}
36+
}

BridgePattern/IEnchantment.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BridgePattern
2+
{
3+
public interface IEnchantment
4+
{
5+
void OnActivate();
6+
void Apply();
7+
void OnDeactivate();
8+
}
9+
}

BridgePattern/IWeapon.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace BridgePattern
2+
{
3+
public interface IWeapon
4+
{
5+
void Wield();
6+
void Swing();
7+
void Unwield();
8+
IEnchantment GetEnchantment();
9+
}
10+
}

BridgePattern/Program.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace BridgePattern
2+
{
3+
internal static class Program
4+
{
5+
private static void Main()
6+
{
7+
IWeapon sword = new Sword(new FlyingEnchantment());
8+
sword.Wield();
9+
sword.Swing();
10+
sword.Unwield();
11+
12+
IWeapon hammer = new Hammer(new SoulEatingEnchantment());
13+
hammer.Wield();
14+
hammer.Swing();
15+
hammer.Unwield();
16+
}
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace BridgePattern
4+
{
5+
public class SoulEatingEnchantment:IEnchantment
6+
{
7+
public void OnActivate()
8+
{
9+
Console.WriteLine("The item spreads bloodlust.");
10+
}
11+
12+
public void Apply()
13+
{
14+
Console.WriteLine("The item eats the soul of enemies.");
15+
}
16+
17+
public void OnDeactivate()
18+
{
19+
Console.WriteLine("Bloodlust slowly disappears.");
20+
}
21+
}
22+
}

BridgePattern/Sword.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace BridgePattern
4+
{
5+
public class Sword:IWeapon
6+
{
7+
private readonly IEnchantment _enchantment;
8+
9+
public Sword(IEnchantment enchantment)
10+
{
11+
_enchantment = enchantment;
12+
}
13+
14+
public void Wield()
15+
{
16+
Console.WriteLine("The sword is wielded.");
17+
_enchantment.OnActivate();
18+
}
19+
20+
public void Swing()
21+
{
22+
Console.WriteLine("The sword is swinged.");
23+
_enchantment.Apply();
24+
}
25+
26+
public void Unwield()
27+
{
28+
Console.WriteLine("The sword is unwielded.");
29+
_enchantment.OnDeactivate();
30+
}
31+
32+
public IEnchantment GetEnchantment()
33+
{
34+
return _enchantment;
35+
}
36+
}
37+
}

DesignPatternsDotNetCore.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyPattern", "Strategy
2525
EndProject
2626
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplatePattern", "TemplatePattern\TemplatePattern.csproj", "{928CD7B2-BB6E-4E07-834E-67B20CA6D698}"
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BridgePattern", "BridgePattern\BridgePattern.csproj", "{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -170,5 +172,17 @@ Global
170172
{928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|x64.Build.0 = Release|x64
171173
{928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|x86.ActiveCfg = Release|x86
172174
{928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|x86.Build.0 = Release|x86
175+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
176+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
177+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x64.ActiveCfg = Debug|Any CPU
178+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x64.Build.0 = Debug|Any CPU
179+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x86.ActiveCfg = Debug|Any CPU
180+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x86.Build.0 = Debug|Any CPU
181+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
182+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|Any CPU.Build.0 = Release|Any CPU
183+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x64.ActiveCfg = Release|Any CPU
184+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x64.Build.0 = Release|Any CPU
185+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x86.ActiveCfg = Release|Any CPU
186+
{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x86.Build.0 = Release|Any CPU
173187
EndGlobalSection
174188
EndGlobal

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ There are three kinds of Design Patterns
1717
-----------------------------------------
1818

1919
* [Adapter](/AdapterPattern)
20+
* [Bridge](/BridgePattern)
2021
* [Command](/CommandPattern)
2122
* [Composite](/CompositePattern)
2223
* [Decorator](/DecoratorPattern)

0 commit comments

Comments
 (0)