-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
280 additions
and
205 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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,35 @@ | ||
using DesignPatterns.Utils.Display; | ||
|
||
namespace DesignPatterns.Structural.Flyweight | ||
{ | ||
public class Flyweight | ||
{ | ||
private readonly IOutput _output; | ||
|
||
public Flyweight(IOutput output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
public void Run() | ||
{ | ||
var factory = new InsuranceTypeFactory(); | ||
|
||
// Shared insurance types | ||
var healthType = factory.GetInsuranceType("Health", "Full Coverage"); | ||
var carType = factory.GetInsuranceType("Car", "Collision Coverage"); | ||
|
||
// Policies that share insurance types | ||
var policy1 = new InsurancePolicy(healthType, "Mdu Doe", "P123456"); | ||
var policy2 = new InsurancePolicy(healthType, "Trev Doe", "P123457"); | ||
var policy3 = new InsurancePolicy(carType, "Dane", "P123458"); | ||
var policy4 = new InsurancePolicy(carType, "Menzi", "P123459"); | ||
|
||
// Display policy details | ||
_output.Display(policy1.GetPolicyDetails()); | ||
_output.Display(policy2.GetPolicyDetails()); | ||
_output.Display(policy3.GetPolicyDetails()); | ||
_output.Display(policy4.GetPolicyDetails()); | ||
} | ||
} | ||
} |
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,21 @@ | ||
namespace DesignPatterns.Structural.Flyweight; | ||
|
||
//Extrinsic State: This represents the unique state | ||
public class InsurancePolicy | ||
{ | ||
private readonly InsuranceType _insuranceType; | ||
public string CustomerName { get; } | ||
public string PolicyNumber { get; } | ||
|
||
public InsurancePolicy(InsuranceType insuranceType, string customerName, string policyNumber) | ||
{ | ||
_insuranceType = insuranceType; | ||
CustomerName = customerName; | ||
PolicyNumber = policyNumber; | ||
} | ||
|
||
public string GetPolicyDetails() | ||
{ | ||
return $"Policy No: {PolicyNumber}, Customer: {CustomerName}, {_insuranceType.GetPolicyTypeDetails()}"; | ||
} | ||
} |
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 @@ | ||
namespace DesignPatterns.Structural.Flyweight; | ||
|
||
//Intrinsic State: This represents the shared state | ||
public class InsuranceType | ||
{ | ||
public string Type { get; } | ||
public string Coverage { get; } | ||
|
||
public InsuranceType(string type, string coverage) | ||
{ | ||
Type = type; | ||
Coverage = coverage; | ||
} | ||
|
||
public string GetPolicyTypeDetails() | ||
{ | ||
return $"Type: {Type}, Coverage: {Coverage}"; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/DesignPatterns.Adapter/Flyweight/InsuranceTypeFactory.cs
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,18 @@ | ||
namespace DesignPatterns.Structural.Flyweight; | ||
|
||
public class InsuranceTypeFactory | ||
{ | ||
private readonly Dictionary<string, InsuranceType> _insuranceTypes = new Dictionary<string, InsuranceType>(); | ||
|
||
public InsuranceType GetInsuranceType(string type, string coverage) | ||
{ | ||
var key = $"{type}_{coverage}"; | ||
|
||
if (!_insuranceTypes.ContainsKey(key)) | ||
{ | ||
_insuranceTypes[key] = new InsuranceType(type, coverage); | ||
} | ||
|
||
return _insuranceTypes[key]; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/DesignPatterns.Structural.Tests/FlyweightPatternTests.cs
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,60 @@ | ||
using DesignPatterns.Structural.Flyweight; | ||
using FluentAssertions; | ||
|
||
namespace DesignPatterns.Structural.Tests; | ||
|
||
public class FlyweightPatternTests | ||
{ | ||
public class InsuranceTypeFactoryTests | ||
{ | ||
[Test] | ||
public void ShouldReuseInsuranceTypeForSamePolicyType() | ||
{ | ||
// Arrange | ||
var factory = new InsuranceTypeFactory(); | ||
|
||
// Act | ||
var type1 = factory.GetInsuranceType("Health", "Full Coverage"); | ||
var type2 = factory.GetInsuranceType("Health", "Full Coverage"); | ||
|
||
// Assert | ||
type1.Should().BeSameAs(type2, "Factory should return the same instance for the same insurance type"); | ||
} | ||
|
||
[Test] | ||
public void ShouldCreateDifferentInsuranceTypesForDifferentPolicyTypes() | ||
{ | ||
// Arrange | ||
var factory = new InsuranceTypeFactory(); | ||
|
||
// Act | ||
var healthType = factory.GetInsuranceType("Health", "Full Coverage"); | ||
var carType = factory.GetInsuranceType("Car", "Collision Coverage"); | ||
|
||
// Assert | ||
healthType.Should().NotBeSameAs(carType, "Factory should return different instances for different insurance types"); | ||
} | ||
} | ||
|
||
public class InsurancePolicyTests | ||
{ | ||
[Test] | ||
public void ShouldStoreCustomerSpecificPolicyDetails() | ||
{ | ||
// Arrange | ||
var factory = new InsuranceTypeFactory(); | ||
var healthType = factory.GetInsuranceType("Health", "Full Coverage"); | ||
|
||
var policy1 = new InsurancePolicy(healthType, "Mdu Doe", "P123456"); | ||
var policy2 = new InsurancePolicy(healthType, "Trev Doe", "P123457"); | ||
|
||
// Act | ||
var details1 = policy1.GetPolicyDetails(); | ||
var details2 = policy2.GetPolicyDetails(); | ||
|
||
// Assert | ||
details1.Should().Be("Policy No: P123456, Customer: Mdu Doe, Type: Health, Coverage: Full Coverage"); | ||
details2.Should().Be("Policy No: P123457, Customer: Trev Doe, Type: Health, Coverage: Full Coverage"); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.