diff --git a/Ch3Beverage/Beverage.cs b/Ch3Beverage/Beverage.cs new file mode 100644 index 0000000..05e7236 --- /dev/null +++ b/Ch3Beverage/Beverage.cs @@ -0,0 +1,51 @@ +namespace Ch3Beverage; + +public abstract class Beverage { + public virtual string Description => "Unknown beverage"; + + public abstract double Cost(); +} + +/* + The only real benefit of putting the constructor in CondimentDecorator is if you need to: + Add validation logic (e.g., null checks) once for all decorators + Add shared behavior that all decorators need when wrapping + Enforce that the wrapped beverage is immutable across all decorators + * + If you don't need shared initialization logic, your approach is cleaner and more direct. + It's a legitimate design choice, not a mistake. + The "always put it in the base class" advice is dogmatic - useful when you have multiple decorators and want consistency, + but not a hard rule. Your implementation follows the Decorator pattern correctly. + */ + +public abstract class CondimentDecorator : Beverage; + +public class Espresso : Beverage { + public override string Description => "Espresso"; + + public override double Cost() => 1.99; +} + +public class HouseBlend : Beverage { + public override string Description => "House Blend Coffee"; + + public override double Cost() => 0.89; +} + +public class DarkRoast : Beverage { + public override string Description => "Dark Roast Coffee"; + + public override double Cost() => 0.99; +} + +public class Decaf : Beverage { + public override string Description => "Decaf Coffee"; + + public override double Cost() => 1.05; +} + +public class Mocha(Beverage beverage) : CondimentDecorator { + public override string Description => $"{beverage.Description}, Mocha"; + + public override double Cost() => beverage.Cost() + 0.20; +} \ No newline at end of file diff --git a/Ch3Beverage/Ch3Beverage.csproj b/Ch3Beverage/Ch3Beverage.csproj new file mode 100644 index 0000000..46e6c92 --- /dev/null +++ b/Ch3Beverage/Ch3Beverage.csproj @@ -0,0 +1,11 @@ + + + + Exe + net9.0 + latestmajor + enable + enable + + + diff --git a/Ch3Beverage/Program.cs b/Ch3Beverage/Program.cs new file mode 100644 index 0000000..e5dff12 --- /dev/null +++ b/Ch3Beverage/Program.cs @@ -0,0 +1,3 @@ +// See https://aka.ms/new-console-template for more information + +Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/HeadFirstDesignPatterns.sln b/HeadFirstDesignPatterns.sln index 977f876..6532c04 100644 --- a/HeadFirstDesignPatterns.sln +++ b/HeadFirstDesignPatterns.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch1Duck", "Ch1Duck\Ch1Duck. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch2Weather", "Ch2Weather\Ch2Weather.csproj", "{D457D364-C0AB-42D2-A0B7-245D038CE8D1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ch3Beverage", "Ch3Beverage\Ch3Beverage.csproj", "{8F380143-DDBC-4785-B4F2-7B32EBE54515}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +20,9 @@ Global {D457D364-C0AB-42D2-A0B7-245D038CE8D1}.Debug|Any CPU.Build.0 = Debug|Any CPU {D457D364-C0AB-42D2-A0B7-245D038CE8D1}.Release|Any CPU.ActiveCfg = Release|Any CPU {D457D364-C0AB-42D2-A0B7-245D038CE8D1}.Release|Any CPU.Build.0 = Release|Any CPU + {8F380143-DDBC-4785-B4F2-7B32EBE54515}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8F380143-DDBC-4785-B4F2-7B32EBE54515}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8F380143-DDBC-4785-B4F2-7B32EBE54515}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F380143-DDBC-4785-B4F2-7B32EBE54515}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal