-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implemented Builder Pattern with regards to issure #8 * Changed folder structure for BuilderPattern * Added new folder structure to solution * Added Chain of Responsibility design pattern
- Loading branch information
1 parent
abd12be
commit 5914dbf
Showing
18 changed files
with
257 additions
and
125 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace BuilderPattern { | ||
|
||
// This class can also be called the Director | ||
public class Cook { | ||
private IBuilder _builder; | ||
public Cook(IBuilder builder) { | ||
AcceptBuilder(builder); | ||
} | ||
|
||
public void ChangeBuilder(IBuilder builder) { | ||
AcceptBuilder(builder); | ||
} | ||
|
||
public Hamburger Build() { | ||
_builder.AddIngredients(); | ||
_builder.AddShape(); | ||
_builder.AddSize(); | ||
return _builder.Build(); | ||
} | ||
|
||
private void AcceptBuilder(IBuilder builder) { | ||
_builder = builder; | ||
_builder.Reset(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace BuilderPattern | ||
{ | ||
public class Hamburger | ||
{ | ||
public int Size { get; set; } | ||
public string Shape { get; set; } | ||
public string[] Ingredients { get; set; } | ||
public override string ToString() | ||
{ | ||
var hamburger=""; | ||
foreach (var ingredient in Ingredients) { | ||
hamburger += $"{ingredient} "; | ||
} | ||
return $"Ingredients: {hamburger}, Size: {Size}, Shape: {Shape}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace BuilderPattern { | ||
public interface IBuilder { | ||
void AddIngredients(); | ||
void AddShape(); | ||
void AddSize(); | ||
void Reset(); | ||
Hamburger Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace BuilderPattern | ||
{ | ||
public class MyHamburgerBuilder : IBuilder { | ||
private Hamburger _hamburger; | ||
public void AddIngredients() { | ||
_hamburger.Ingredients = new string[] { "Bread", "Meat", "Tomato", "Salad", "Mayonnaise" }; | ||
} | ||
|
||
public void AddShape() { | ||
_hamburger.Shape = "Kite"; | ||
} | ||
|
||
public void AddSize() { | ||
_hamburger.Size = 10; //inches | ||
} | ||
public void Reset() { | ||
_hamburger = new Hamburger(); | ||
} | ||
|
||
public Hamburger Build() { | ||
return _hamburger; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace BuilderPattern | ||
{ | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
var builder = new MyHamburgerBuilder(); | ||
var cook = new Cook(builder); | ||
var myHamburger = cook.Build(); | ||
|
||
cook.ChangeBuilder(new WifesHamburgerBuilder()); | ||
var wifesHamburger = cook.Build(); | ||
|
||
Console.WriteLine($"My Hamburger: {myHamburger}"); | ||
Console.WriteLine($"My Wife's Hamburger: {wifesHamburger}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
|
||
namespace BuilderPattern { | ||
|
||
public class WifesHamburgerBuilder : IBuilder { | ||
private Hamburger _hamburger; | ||
public void AddIngredients() { | ||
_hamburger.Ingredients = new string[] { "Bread", "Salad" }; | ||
} | ||
|
||
public void AddShape() { | ||
_hamburger.Shape = "Cuboid"; | ||
} | ||
|
||
public void AddSize() { | ||
_hamburger.Size = 6; //inches | ||
} | ||
|
||
public void Reset() { | ||
_hamburger = new Hamburger(); | ||
} | ||
public Hamburger Build() { | ||
return _hamburger; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ChainOfResponsibilityPattern { | ||
public class AdditionHandler : BaseHandler { | ||
public override double? Handle(double[] values, string action) { | ||
if (action.ToLower() == "add") { | ||
double result=0.0; | ||
foreach (var value in values) { | ||
result += value; | ||
} | ||
return result; | ||
} | ||
else { | ||
return _nextInLine?.Handle(values,action); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace ChainOfResponsibilityPattern { | ||
public abstract class BaseHandler : IHandler { | ||
public void AddChain(IHandler handler) { | ||
_nextInLine = handler; | ||
} | ||
|
||
public abstract double? Handle(double[] values, string action); | ||
|
||
protected IHandler _nextInLine; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
ChainOfResponsibilityPattern/ChainOfResponsibilityPattern.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ChainOfResponsibilityPattern { | ||
public interface IHandler { | ||
void AddChain(IHandler handler); | ||
double? Handle(double[] values, string action); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace ChainOfResponsibilityPattern { | ||
public class MultiplicationHandler : BaseHandler { | ||
public override double? Handle(double[] values, string action) { | ||
if (action.ToLower() == "multiply") { | ||
var result = 1.0; | ||
foreach (var value in values) { | ||
result *= value; | ||
} | ||
return result; | ||
} | ||
else { | ||
return _nextInLine?.Handle(values, action); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
|
||
namespace ChainOfResponsibilityPattern { | ||
class Program { | ||
static void Main(string[] args) { | ||
//create handlers | ||
var additionHandler = new AdditionHandler(); | ||
var subtractionHandler = new SubtractionHandler(); | ||
var multiplicationHander = new MultiplicationHandler(); | ||
//create chain | ||
subtractionHandler.AddChain(multiplicationHander); | ||
additionHandler.AddChain(subtractionHandler); | ||
//Execution | ||
double[] numbers = new double[] { 2, 3, 4, 5 }; | ||
var additionResult = additionHandler.Handle(numbers, "Add"); | ||
var subtractionResult = additionHandler.Handle(numbers, "Minus"); | ||
var multResult = additionHandler.Handle(numbers, "Multiply"); | ||
var divisionResult = additionHandler.Handle(numbers, "divide"); // Divide is not in the chain!!! | ||
|
||
Console.WriteLine("Addition = {0}",additionResult); | ||
Console.WriteLine("Subtraction = {0}", subtractionResult); | ||
Console.WriteLine("Multiplication = {0}", multResult); | ||
Console.WriteLine("Division = {0}", divisionResult); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace ChainOfResponsibilityPattern { | ||
public class SubtractionHandler : BaseHandler { | ||
public override double? Handle(double[] values, string action) { | ||
if (action.ToLower() == "minus") { | ||
var result = values[0]; | ||
for (int i = 1; i < values.Length; i++) { | ||
result -= values[i]; | ||
} | ||
return result; | ||
} | ||
else { | ||
return _nextInLine?.Handle(values, action); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.