Ch3Beverage: My version of a Decorator without the construction and hold of the Beverage inside the decorator abstract class.
This commit is contained in:
51
Ch3Beverage/Beverage.cs
Normal file
51
Ch3Beverage/Beverage.cs
Normal file
@@ -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;
|
||||
}
|
||||
11
Ch3Beverage/Ch3Beverage.csproj
Normal file
11
Ch3Beverage/Ch3Beverage.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<LangVersion>latestmajor</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
3
Ch3Beverage/Program.cs
Normal file
3
Ch3Beverage/Program.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
Console.WriteLine("Hello, World!");
|
||||
Reference in New Issue
Block a user