Skip to content

Commit 3dc514f

Browse files
Merge pull request #48 from vivopensource/dev
Adds Flyweight and mediator pattern implementation to Main
2 parents 3eeaaf4 + b201084 commit 3dc514f

File tree

37 files changed

+610
-28
lines changed

37 files changed

+610
-28
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,6 @@ typings/
123123
package-lock.json
124124
src/environments/environment-local.ts
125125

126+
ProgramExample.cs
127+
ProgramExampleTests.cs
128+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Core.Console.Interfaces;
2+
using GofPatterns.Behavioral.MediatorPattern;
3+
4+
namespace GofConsoleApp.Examples.Behavioral.MediatorPattern;
5+
6+
internal class ChatPerson : MediatorColleague<string, string>
7+
{
8+
private readonly IConsoleLogger logger;
9+
10+
public ChatPerson(string identifier, Mediator<string, string> mediator, IConsoleLogger logger) :
11+
base(identifier, mediator)
12+
{
13+
this.logger = logger;
14+
}
15+
16+
public override void Process(string input) => Listening(input);
17+
18+
private void Listening(string input) => logger.Log($"Listening '{Identifier}': {input}");
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using GofPatterns.Behavioral.MediatorPattern;
2+
3+
namespace GofConsoleApp.Examples.Behavioral.MediatorPattern;
4+
5+
internal class MediatorPatternExample : AbstractExample
6+
{
7+
protected override bool Execute()
8+
{
9+
var chatRoom = new Mediator<string, string>();
10+
var person1 = new ChatPerson("Person1", chatRoom, Logger);
11+
var person2 = new ChatPerson("Person2", chatRoom, Logger);
12+
13+
chatRoom.Send(person1.Identifier, "Welcome in chatroom!"); // Mediator Calls Colleague (Person1)
14+
chatRoom.Send("Person2", "Welcome in chatroom!"); // Mediator Calls Colleague (Person2)
15+
16+
person1.Send(person2.Identifier, $"Hello from {person1.Identifier}"); // Colleague (Person1) Calls Colleague (Person2)
17+
person2.Send("Person1", $"Hello from {person2.Identifier}"); // Colleague (Person2) Calls Colleague (Person1)
18+
19+
return true;
20+
}
21+
}

GofConsoleApp/Examples/ExecutionHelpers/PatternOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using GofConsoleApp.Examples.Behavioral.CommandPattern;
22
using GofConsoleApp.Examples.Behavioral.CorPattern;
3+
using GofConsoleApp.Examples.Behavioral.MediatorPattern;
34
using GofConsoleApp.Examples.Behavioral.StatePattern;
45
using GofConsoleApp.Examples.Behavioral.StrategyPattern;
56
using GofConsoleApp.Examples.Structural.AdapterPattern;
67
using GofConsoleApp.Examples.Structural.DecoratorPattern;
8+
using GofConsoleApp.Examples.Structural.FlyweightPattern;
79
using GofConsoleApp.Examples.Structural.ProxyPattern;
810

911
namespace GofConsoleApp.Examples.ExecutionHelpers;
@@ -17,6 +19,8 @@ internal static class PatternOptions
1719
internal const string ProxyPatternOptionBoundedInput = "12.2";
1820
internal const string ProxyPatternOptionBoundedInputOutput = "12.3";
1921
internal const string AdapterPatternOption = "13";
22+
internal const string FlyweightPatternOption = "14";
23+
internal const string MediatorPatternOption = "15";
2024
internal const string ChainOfResponsibilityPatternOption = "21";
2125
internal const string ChainOfResponsibilityPatternOption2 = "21.2";
2226
internal const string ChainOfResponsibilityPatternOption3 = "21.3";
@@ -55,6 +59,14 @@ internal static class PatternOptions
5559
AdapterPatternOption,
5660
new PatternExampleMap("Adapter Pattern >> Adaptee:Employee , Target:Traveller", new AdapterPatternExample())
5761
},
62+
{
63+
FlyweightPatternOption,
64+
new PatternExampleMap("Flyweight Pattern >> Drawing shapes", new FlyweightPatternExample())
65+
},
66+
{
67+
MediatorPatternOption,
68+
new PatternExampleMap("Flyweight Pattern >> Drawing shapes", new MediatorPatternExample())
69+
},
5870

5971
// Behavioral Patterns
6072
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace GofConsoleApp.Examples.Structural.FlyweightPattern.Components;
2+
3+
internal class CoOrdinates
4+
{
5+
// ReSharper disable UnusedAutoPropertyAccessor.Global
6+
public int X1 { get; set; }
7+
public int X2 { get; set; }
8+
public int Y1 { get; set; }
9+
public int Y2 { get; set; }
10+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Core.Console.Interfaces;
2+
using Core.Extensions;
3+
using GofPatterns.Structural.FlyweightPattern.Interfaces;
4+
5+
namespace GofConsoleApp.Examples.Structural.FlyweightPattern.Components;
6+
7+
internal interface IShape : IFlyweight<CoOrdinates> { }
8+
9+
internal class CircleShape : IShape
10+
{
11+
private readonly IConsoleLogger logger;
12+
13+
public CircleShape(IConsoleLogger logger)
14+
{
15+
this.logger = logger;
16+
}
17+
18+
public void Action(CoOrdinates points)
19+
{
20+
logger.Log("Drawing circle at co-ordinates:");
21+
logger.Log(points.ToYamlString());
22+
}
23+
}
24+
25+
internal class SquareShape : IShape
26+
{
27+
private readonly IConsoleLogger logger;
28+
29+
public SquareShape(IConsoleLogger logger)
30+
{
31+
this.logger = logger;
32+
}
33+
34+
public void Action(CoOrdinates points)
35+
{
36+
logger.Log("Drawing square at co-ordinates:");
37+
logger.Log(points.ToYamlString());
38+
}
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GofConsoleApp.Examples.Structural.FlyweightPattern.Components;
2+
3+
internal enum ShapeType
4+
{
5+
Circle,
6+
Square
7+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using GofConsoleApp.Examples.Structural.FlyweightPattern.Components;
2+
using GofPatterns.Structural.FlyweightPattern;
3+
using GofPatterns.Structural.FlyweightPattern.Interfaces;
4+
5+
namespace GofConsoleApp.Examples.Structural.FlyweightPattern;
6+
7+
internal class FlyweightPatternExample : AbstractExample
8+
{
9+
protected override bool Execute()
10+
{
11+
IShape circle = new CircleShape(Logger);
12+
IShape square = new SquareShape(Logger);
13+
14+
IFlyweightFactory<ShapeType, CoOrdinates> factory = new FlyweightFactory<ShapeType, CoOrdinates>
15+
(
16+
new FlyweightMapping<ShapeType, CoOrdinates>(ShapeType.Circle, circle),
17+
new FlyweightMapping<ShapeType, CoOrdinates>(ShapeType.Square, square)
18+
);
19+
20+
var circleObj1 = factory.GetObject(ShapeType.Circle);
21+
circleObj1.Action(new CoOrdinates { X1 = 1, X2 = 2, Y1 = 3, Y2 = 4 });
22+
23+
var squareObj1 = factory.GetObject(ShapeType.Square);
24+
squareObj1.Action(new CoOrdinates { X1 = 5, X2 = 6, Y1 = 7, Y2 = 8 });
25+
26+
var circleObj2 = factory.GetObject(ShapeType.Circle);
27+
circleObj2.Action(new CoOrdinates { X1 = 9, X2 = 10, Y1 = 11, Y2 = 12 });
28+
29+
var squareObj2 = factory.GetObject(ShapeType.Square);
30+
squareObj2.Action(new CoOrdinates { X1 = 5, X2 = 6, Y1 = 7, Y2 = 8 });
31+
32+
Logger.Log($"Is circleObj1 same as circleObj2? {circleObj1 == circleObj2}");
33+
Logger.Log($"Is squareObj1 same as squareObj2? {squareObj1 == squareObj2}");
34+
35+
return true;
36+
}
37+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace GofPatterns.Behavioral.MediatorPattern.Interfaces;
2+
3+
public interface ICommunicator<in TColleagueIdentifier, in TInput>
4+
{
5+
void Send(TColleagueIdentifier identifier, TInput input);
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GofPatterns.Behavioral.MediatorPattern.Interfaces;
2+
3+
public interface IMediatorColleague<TColleagueIdentifier, in TInput> : ICommunicator<TColleagueIdentifier, TInput>
4+
{
5+
void Process(TInput input);
6+
7+
TColleagueIdentifier Identifier { get; }
8+
}

0 commit comments

Comments
 (0)