Skip to content

Commit

Permalink
strategy pattern first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Moreira committed Sep 22, 2017
1 parent d63d6b5 commit 1d84ee3
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 3 deletions.
11 changes: 9 additions & 2 deletions DesignPatternsByExample.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
VisualStudioVersion = 15.0.26730.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Creational", "Creational", "{D54E4C11-92D5-4B35-AD36-E5E7E6045588}"
EndProject
Expand Down Expand Up @@ -32,7 +32,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VisitorPattern", "src\Visit
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObserverPattern", "src\ObserverPattern\ObserverPattern.csproj", "{CF07133A-644E-43D1-9F0B-2658AF028EBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfReponsibilityPattern", "src\ChainOfReponsibilityPattern\ChainOfReponsibilityPattern.csproj", "{6CD853E1-8058-47CB-BD7C-07F5140F0D74}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfReponsibilityPattern", "src\ChainOfReponsibilityPattern\ChainOfReponsibilityPattern.csproj", "{6CD853E1-8058-47CB-BD7C-07F5140F0D74}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyPattern", "src\StrategyPattern\StrategyPattern.csproj", "{46D321BC-CB3A-474E-B653-F37439A4B137}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -76,6 +78,10 @@ Global
{6CD853E1-8058-47CB-BD7C-07F5140F0D74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CD853E1-8058-47CB-BD7C-07F5140F0D74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CD853E1-8058-47CB-BD7C-07F5140F0D74}.Release|Any CPU.Build.0 = Release|Any CPU
{46D321BC-CB3A-474E-B653-F37439A4B137}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{46D321BC-CB3A-474E-B653-F37439A4B137}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46D321BC-CB3A-474E-B653-F37439A4B137}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46D321BC-CB3A-474E-B653-F37439A4B137}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -90,6 +96,7 @@ Global
{9E0BE05A-EFC8-4BC8-AA5D-847514A7698C} = {4B157FFD-B85F-4627-8EB0-E403330E7696}
{CF07133A-644E-43D1-9F0B-2658AF028EBA} = {4B157FFD-B85F-4627-8EB0-E403330E7696}
{6CD853E1-8058-47CB-BD7C-07F5140F0D74} = {4B157FFD-B85F-4627-8EB0-E403330E7696}
{46D321BC-CB3A-474E-B653-F37439A4B137} = {4B157FFD-B85F-4627-8EB0-E403330E7696}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2F76BEA8-697A-427D-8C40-96572FDC3AD9}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Each pattern has an example implementation in order to allow a better approach t
- [ ] Memento
- [x] [Observer](https://github.com/pedromsmoreira/design-patterns-by-example/tree/master/src/ObserverPattern)
- [ ] State
- [ ] Strategy
- [x] [Strategy](https://github.com/pedromsmoreira/design-patterns-by-example/tree/master/src/StrategyPattern)
- [ ] Template Method
- [x] [Visitor](https://github.com/pedromsmoreira/design-patterns-by-example/tree/master/src/VisitorPattern)

Expand Down
15 changes: 15 additions & 0 deletions src/StrategyPattern/Barney.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace StrategyPattern
{
public class Barney : Character
{
public Barney()
: base((string)nameof(Barney))
{
}

public override void Fight()
{
this.Weapon.UseWeapon();
}
}
}
23 changes: 23 additions & 0 deletions src/StrategyPattern/Character.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace StrategyPattern
{
using System;

public abstract class Character
{
protected Character(string name)
{
this.Name = name;
}

public string Name { get; }

public IWeaponBehavior Weapon { get; private set; }

public void SetWeapon(IWeaponBehavior weapon)
{
this.Weapon = weapon;
}

public abstract void Fight();
}
}
7 changes: 7 additions & 0 deletions src/StrategyPattern/IWeaponBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace StrategyPattern
{
public interface IWeaponBehavior
{
void UseWeapon();
}
}
17 changes: 17 additions & 0 deletions src/StrategyPattern/Marshall.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace StrategyPattern
{
using System;

public class Marshall : Character
{
public Marshall()
: base((string) nameof(Marshall))
{
}

public override void Fight()
{
this.Weapon.UseWeapon();
}
}
}
39 changes: 39 additions & 0 deletions src/StrategyPattern/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

namespace StrategyPattern
{
internal class Program
{
private static void Main(string[] args)
{
var sword = new SwordBehavior();
var slap = new SlapBehavior();

var marshall = new Marshall();
var barney = new Barney();

Console.WriteLine("Slapbet round 1!");

Console.WriteLine("Barney annoys Marshall!");

Console.WriteLine("Marshall prepares his hand!");
marshall.SetWeapon(slap);

marshall.Fight();

Console.WriteLine("Barney is slapped in the face!");
Console.WriteLine("Barney equips a sword!");

barney.SetWeapon(sword);
barney.Fight();

Console.WriteLine("Marshall equips a Sword too!");
marshall.SetWeapon(sword);
marshall.Fight();

Console.WriteLine("They duel.");

Console.ReadLine();
}
}
}
12 changes: 12 additions & 0 deletions src/StrategyPattern/SlapBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace StrategyPattern
{
using System;

public class SlapBehavior : IWeaponBehavior
{
public void UseWeapon()
{
Console.WriteLine("You just got Slapped! In the face my friend.");
}
}
}
8 changes: 8 additions & 0 deletions src/StrategyPattern/StrategyPattern.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions src/StrategyPattern/SwordBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace StrategyPattern
{
using System;

public class SwordBehavior : IWeaponBehavior
{
public void UseWeapon()
{
Console.WriteLine("You got Slashed!");
}
}
}

0 comments on commit 1d84ee3

Please sign in to comment.