Skip to content

Commit 929edd0

Browse files
Merge pull request #67 from vivopensource/64-add-bridge-pattern
64 add bridge pattern
2 parents 82fa634 + 3ed942b commit 929edd0

40 files changed

Lines changed: 1135 additions & 17 deletions

GofConsoleApp/Examples/ExecutionHelpers/PatternOptions.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GofConsoleApp.Examples.Creational.BuilderPattern;
99
using GofConsoleApp.Examples.Creational.FactoryPattern;
1010
using GofConsoleApp.Examples.Structural.AdapterPattern;
11+
using GofConsoleApp.Examples.Structural.BridgePattern;
1112
using GofConsoleApp.Examples.Structural.DecoratorPattern;
1213
using GofConsoleApp.Examples.Structural.FlyweightPattern;
1314
using GofConsoleApp.Examples.Structural.ProxyPattern;
@@ -37,6 +38,9 @@ internal static class PatternOptions
3738
internal const string StatePatternOptionDriveExample = "23.2";
3839
internal const string StrategyPatternOptionSender = "24";
3940
internal const string StrategyPatternOptionPayment = "24.2";
41+
internal const string BridgePatterOptionSingleImplementations = "25";
42+
internal const string BridgePatterOptionMultipleImplementations = "25.2";
43+
internal const string BridgePatterOptionMarker = "25.3";
4044

4145
internal const string FactoryOption = "31";
4246
internal const string AbstractFactoryOption = "32";
@@ -84,7 +88,8 @@ internal static class PatternOptions
8488
},
8589
{
8690
ObserverPatternOptionWithType,
87-
new PatternExampleMap("Observer Pattern >> News Publisher with type", new ObserverPatternExampleWithCategory())
91+
new PatternExampleMap("Observer Pattern >> News Publisher with type",
92+
new ObserverPatternExampleWithCategory())
8893
},
8994

9095
// Behavioral Patterns
@@ -127,6 +132,20 @@ internal static class PatternOptions
127132
StrategyPatternOptionPayment,
128133
new PatternExampleMap("Strategy Pattern >> Payment Example", new StrategyPatternPaymentExample())
129134
},
135+
{
136+
BridgePatterOptionSingleImplementations,
137+
new PatternExampleMap("Bridge Pattern >> With Single Implementation",
138+
new BridgePatternExampleSingleImplementations())
139+
},
140+
{
141+
BridgePatterOptionMultipleImplementations,
142+
new PatternExampleMap("Bridge Pattern >> With Multiple Implementations",
143+
new BridgePatternExampleMultipleImplementations())
144+
},
145+
{
146+
BridgePatterOptionMarker,
147+
new PatternExampleMap("Bridge Pattern >> With Marker", new BridgePatternExampleWithMarker())
148+
},
130149

131150
// Creational Patterns
132151
{

GofConsoleApp/Examples/Structural/AdapterPattern/Adaptee/Employee.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public Employee(string id, string firstName, string lastName, string address)
1111
}
1212

1313
public string Id { get; }
14+
1415
public string FirstName { get; }
16+
1517
public string LastName { get; }
18+
1619
public string Address { get; }
1720
}

GofConsoleApp/Examples/Structural/AdapterPattern/Adaptee/IEmployee.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
internal interface IEmployee
77
{
88
string Id { get; }
9+
910
string FirstName { get; }
11+
1012
string LastName { get; }
13+
1114
string Address { get; }
1215
}

GofConsoleApp/Examples/Structural/AdapterPattern/EmployeeTravellerAdapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace GofConsoleApp.Examples.Structural.AdapterPattern;
66

7-
internal class EmployeeTravellerAdapter : IAdapter<IEmployee, ITraveller>, ITraveller
7+
internal class EmployeeTravellerAdapter : IAdapter<Adaptee.IEmployee, ITraveller>, ITraveller
88
{
9-
public EmployeeTravellerAdapter(IEmployee adaptee)
9+
public EmployeeTravellerAdapter(Adaptee.IEmployee adaptee)
1010
{
1111
FullName = $"{adaptee.FirstName} {adaptee.LastName}";
1212
Address = adaptee.Address;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using GofConsoleApp.Examples.Structural.BridgePattern.Implementations;
2+
using GofConsoleApp.Examples.Structural.BridgePattern.Implementations.Multiple;
3+
4+
namespace GofConsoleApp.Examples.Structural.BridgePattern;
5+
6+
internal class BridgePatternExampleMultipleImplementations : BaseExample
7+
{
8+
protected override bool Execute()
9+
{
10+
var emp1 = new Employee("1", "Jane", "Doe");
11+
var emp2 = new Employee("2", "John", "Doe");
12+
13+
IProcess registerEvent = new Registration(Logger);
14+
IProcess taskPresentation = new TaskAssignment("Goals presentation", Logger);
15+
16+
IManagement annualConference1 = new EventManagement("Annual conference", Logger);
17+
annualConference1.Add(registerEvent, taskPresentation);
18+
annualConference1.Execute(emp1);
19+
20+
IManagement annualConference2 = new EventManagement("Annual conference", Logger);
21+
annualConference2.Add(registerEvent);
22+
annualConference2.Execute(emp2);
23+
24+
IProcess taskSalesPitch = new TaskAssignment("Sales pitch", Logger);
25+
IManagement salesTravel = new TravelManagement("Sales", Logger);
26+
salesTravel.Add(taskSalesPitch);
27+
salesTravel.Execute(emp1);
28+
29+
IProcess registerTravel = new Registration(Logger);
30+
IProcess taskSystemUpgrade = new TaskAssignment("System upgrade", Logger);
31+
IManagement maintenanceTravel = new TravelManagement("Maintenance", Logger);
32+
maintenanceTravel.Add(registerTravel, taskSystemUpgrade);
33+
maintenanceTravel.Execute(emp2);
34+
35+
return annualConference1.ImplementationCount == 2 &&
36+
annualConference2.ImplementationCount == 1 &&
37+
salesTravel.ImplementationCount == 1 &&
38+
maintenanceTravel.ImplementationCount == 2;
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using GofConsoleApp.Examples.Structural.BridgePattern.Implementations;
2+
using GofConsoleApp.Examples.Structural.BridgePattern.Implementations.Single;
3+
4+
namespace GofConsoleApp.Examples.Structural.BridgePattern;
5+
6+
internal class BridgePatternExampleSingleImplementations : BaseExample
7+
{
8+
protected override bool Execute()
9+
{
10+
var emp1 = new Employee("1", "Jane", "Doe");
11+
var emp2 = new Employee("2", "John", "Doe");
12+
13+
IProcess registerEvent = new Registration(Logger);
14+
IManagement annualConference1 = new EventManagement("Annual conference", Logger);
15+
annualConference1.Add(registerEvent);
16+
annualConference1.Execute(emp1);
17+
18+
IManagement annualConference2 = new EventManagement("Annual conference", Logger);
19+
annualConference2.Add(registerEvent);
20+
annualConference2.Execute(emp2);
21+
22+
IProcess taskSalesPitch = new TaskAssignment("Sales pitch", Logger);
23+
IManagement salesTravel = new TravelManagement("Sales", Logger);
24+
salesTravel.Add(taskSalesPitch);
25+
salesTravel.Execute(emp1);
26+
27+
IProcess taskSystemUpgrade = new TaskAssignment("System upgrade", Logger);
28+
IManagement maintenanceTravel = new TravelManagement("Maintenance", Logger);
29+
maintenanceTravel.Add(taskSystemUpgrade);
30+
maintenanceTravel.Execute(emp2);
31+
32+
return true;
33+
}
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using GofConsoleApp.Examples.Structural.BridgePattern.Marker;
2+
3+
namespace GofConsoleApp.Examples.Structural.BridgePattern;
4+
5+
internal class BridgePatternExampleWithMarker : BaseExample
6+
{
7+
protected override bool Execute()
8+
{
9+
var emp1 = new Employee("1", "Jane", "Doe");
10+
var emp2 = new Employee("2", "John", "Doe");
11+
12+
IProcess registerEvent = new Registration();
13+
IProcess taskPresentation = new TaskAssignment("Goals presentation");
14+
15+
IManagement annualConference1 =
16+
new EventManagement("Annual conference", Logger, registerEvent, taskPresentation);
17+
annualConference1.Manage(emp1);
18+
19+
IManagement annualConference2 = new EventManagement("Annual conference", Logger, registerEvent);
20+
annualConference2.Manage(emp2);
21+
22+
IProcess taskSalesPitch = new TaskAssignment("Sales pitch");
23+
IManagement salesTravel = new TravelManagement("Sales", Logger, taskSalesPitch);
24+
salesTravel.Manage(emp1);
25+
26+
IProcess registerTravel = new Registration();
27+
IProcess taskSystemUpgrade = new TaskAssignment("System upgrade");
28+
IManagement maintenanceTravel = new TravelManagement("Maintenance", Logger, registerTravel, taskSystemUpgrade);
29+
maintenanceTravel.Manage(emp2);
30+
31+
return true;
32+
}
33+
}
34+
35+
// Data
36+
internal class Employee
37+
{
38+
public Employee(string id, string firstName, string lastName)
39+
{
40+
Id = id;
41+
FirstName = firstName;
42+
LastName = lastName;
43+
}
44+
45+
public string Id { get; }
46+
47+
public string FirstName { get; }
48+
49+
public string LastName { get; }
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Core.Console.Interfaces;
2+
using GofPatterns.Structural.BridgePattern.Implementations;
3+
4+
namespace GofConsoleApp.Examples.Structural.BridgePattern.Implementations;
5+
6+
// Implementor for bridge pattern
7+
internal interface IProcess : IBridgeImplementationImpl<Employee> { }
8+
9+
// Concrete implementor - Register
10+
internal class Registration : IProcess
11+
{
12+
private readonly IConsoleLogger logger;
13+
14+
public Registration(IConsoleLogger logger)
15+
{
16+
this.logger = logger;
17+
}
18+
19+
public void Execute(Employee emp)
20+
{
21+
logger.Log($" - Registering employee: {emp.Id} [{emp.FirstName} {emp.LastName}].");
22+
}
23+
}
24+
25+
// Concrete implementor - Task assignment
26+
internal class TaskAssignment : IProcess
27+
{
28+
private readonly string name;
29+
private readonly IConsoleLogger logger;
30+
31+
public TaskAssignment(string name, IConsoleLogger logger)
32+
{
33+
this.logger = logger;
34+
this.name = name;
35+
}
36+
37+
public void Execute(Employee emp)
38+
{
39+
logger.Log($" - Assigning employee {emp.Id} [{emp.FirstName} {emp.LastName}] with task [{name}].");
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Core.Console.Interfaces;
2+
using GofPatterns.Structural.BridgePattern.Implementations;
3+
4+
namespace GofConsoleApp.Examples.Structural.BridgePattern.Implementations.Multiple;
5+
6+
// Abstraction in bridge pattern
7+
internal interface IManagement : IBridgeAbstractionsImpl<IProcess, Employee> { }
8+
9+
// Refined abstraction - Event management
10+
internal class EventManagement : BridgeAbstractionsImpl<IProcess, Employee>, IManagement
11+
{
12+
public EventManagement(string purpose, IConsoleLogger logger)
13+
{
14+
logger.Log($"Managing event for: {purpose}.");
15+
}
16+
}
17+
18+
// Refined abstraction - Travel management
19+
internal class TravelManagement : BridgeAbstractionsImpl<IProcess, Employee>, IManagement
20+
{
21+
public TravelManagement(string purpose, IConsoleLogger logger)
22+
{
23+
logger.Log($"Managing travel for: {purpose}.");
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Core.Console.Interfaces;
2+
using GofPatterns.Structural.BridgePattern.Implementations;
3+
4+
namespace GofConsoleApp.Examples.Structural.BridgePattern.Implementations.Single;
5+
6+
// Abstraction in bridge pattern
7+
internal interface IManagement : IBridgeAbstractionImpl<IProcess, Employee> { }
8+
9+
// Refined abstraction - Event management
10+
internal class EventManagement : BridgeAbstractionImpl<IProcess, Employee>, IManagement
11+
{
12+
public EventManagement(string purpose, IConsoleLogger logger)
13+
{
14+
logger.Log($"Managing event for: {purpose}.");
15+
}
16+
}
17+
18+
// Refined abstraction - Travel management
19+
internal class TravelManagement : BridgeAbstractionImpl<IProcess, Employee>, IManagement
20+
{
21+
public TravelManagement(string purpose, IConsoleLogger logger)
22+
{
23+
logger.Log($"Managing travel for: {purpose}.");
24+
}
25+
}

0 commit comments

Comments
 (0)