Skip to content

Commit 110f3c5

Browse files
committed
Update decorator pattern examples
1 parent cc9903c commit 110f3c5

File tree

16 files changed

+83
-133
lines changed

16 files changed

+83
-133
lines changed

src/StructuralPatterns/Decorator/DecoratorLibrary/DataStorageExample/Components/Common/IDataSource.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
/// </summary>
66
public interface IDataSource
77
{
8-
string Read();
9-
108
void Write(string data);
11-
9+
string Read();
1210
void ClearContent();
13-
}
11+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/DataStorageExample/Components/File.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,23 @@ namespace DecoratorLibrary.DataStorageExample.Components;
1010
public class File : IDataSource
1111
{
1212
private readonly string _name;
13-
private StringBuilder _data;
13+
private readonly StringBuilder _data;
1414

1515
public File(string name)
1616
{
1717
_name = name;
1818
_data = new StringBuilder();
1919
}
2020

21-
public void ClearContent()
22-
{
23-
_data.Clear();
24-
}
25-
26-
public string Read()
27-
{
28-
return _data.ToString();
29-
}
30-
3121
public void Write(string data)
3222
{
3323
_data.Append(data);
3424

25+
Console.WriteLine($"Writing to the file: {_name}");
3526
Console.WriteLine($"Written: {data}");
3627
}
37-
}
28+
29+
public string Read() => _data.ToString();
30+
31+
public void ClearContent() => _data.Clear();
32+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/DataStorageExample/DataStorageExecutor.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ private static void ProcessFileWithPlainData()
2020
{
2121
IDataSource source = new File("file.dat");
2222

23-
source.Write("Hello world");
24-
Console.WriteLine($"Read: {source.Read()}\n");
23+
WriteAndRead(source);
2524
}
2625

2726
private static void ProcessFileWithCompressedData()
2827
{
2928
IDataSource source = new File("file.dat");
3029
source = new CompressionDecorator(source);
3130

32-
source.Write("Hello world");
33-
Console.WriteLine($"Read: {source.Read()}\n");
31+
WriteAndRead(source);
3432
}
3533

3634
private static void ProcessFileWithCompressedAndEncryptedData()
@@ -39,7 +37,12 @@ private static void ProcessFileWithCompressedAndEncryptedData()
3937
source = new EncryptionDecorator(source);
4038
source = new CompressionDecorator(source);
4139

40+
WriteAndRead(source);
41+
}
42+
43+
private static void WriteAndRead(IDataSource source)
44+
{
4245
source.Write("Hello world");
4346
Console.WriteLine($"Read: {source.Read()}\n");
4447
}
45-
}
48+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/DataStorageExample/Decorators/Common/DataSourceDecorator.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,23 @@ namespace DecoratorLibrary.DataStorageExample.Decorators.Common;
1010
/// </summary>
1111
public abstract class DataSourceDecorator : IDataSource
1212
{
13-
private readonly IDataSource _wrapee;
13+
protected readonly IDataSource _wrapee;
1414

15-
public DataSourceDecorator(IDataSource dataSource)
15+
protected DataSourceDecorator(IDataSource dataSource)
1616
{
1717
_wrapee = dataSource;
1818
}
1919

20-
public void ClearContent()
21-
{
22-
_wrapee.ClearContent();
23-
}
20+
// The base decorator simply delegates all work to the wrapped component.
21+
// Extra behaviors can be added in concrete decorators.
22+
public virtual void Write(string data) =>
23+
_wrapee.Write(data);
2424

25-
public virtual string Read()
26-
{
27-
// Concrete decorators may call the parent implementation of the operation
28-
// instead of calling the wrapped object directly.
29-
// This approach simplifies extension of decorator classes.
30-
return _wrapee.Read();
31-
}
25+
// With this approach, concrete decorators can call the parent implementation of the operation
26+
// instead of calling the wrapped object directly.
27+
// That simplifies extension of decorator classes.
28+
public virtual string Read() =>
29+
_wrapee.Read();
3230

33-
public virtual void Write(string data)
34-
{
35-
// The base decorator simply delegates all work to the wrapped component.
36-
// Extra behaviors can be added in concrete decorators.
37-
_wrapee.Write(data);
38-
}
39-
}
31+
public void ClearContent() => _wrapee.ClearContent();
32+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/DataStorageExample/Decorators/CompressionDecorator.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,14 @@ namespace DecoratorLibrary.DataStorageExample.Decorators;
1010
/// </summary>
1111
public class CompressionDecorator : DataSourceDecorator
1212
{
13-
private readonly IDataSource _wrapee;
1413
private readonly string _compressionBlock;
1514

1615
public CompressionDecorator(IDataSource wrapee)
1716
: base(wrapee)
1817
{
19-
_wrapee = wrapee;
2018
_compressionBlock = "--COMPRESSED--";
2119
}
2220

23-
public override string Read()
24-
{
25-
var compressedData = _wrapee.Read();
26-
return Decompress(compressedData);
27-
}
28-
2921
public override void Write(string data)
3022
{
3123
Console.WriteLine("Compression");
@@ -34,13 +26,15 @@ public override void Write(string data)
3426
_wrapee.Write(compressedData);
3527
}
3628

37-
private string Decompress(string data)
29+
public override string Read()
3830
{
39-
return data.Replace(_compressionBlock, string.Empty);
31+
var compressedData = _wrapee.Read();
32+
return Decompress(compressedData);
4033
}
4134

42-
private string Compress(string data)
43-
{
44-
return $"{_compressionBlock}{data}{_compressionBlock}";
45-
}
46-
}
35+
private string Compress(string data) =>
36+
$"{_compressionBlock}{data}{_compressionBlock}";
37+
38+
private string Decompress(string data) =>
39+
data.Replace(_compressionBlock, string.Empty, StringComparison.InvariantCulture);
40+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/DataStorageExample/Decorators/EncryptionDecorator.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,14 @@ namespace DecoratorLibrary.DataStorageExample.Decorators;
1010
/// </summary>
1111
public class EncryptionDecorator : DataSourceDecorator
1212
{
13-
private readonly IDataSource _wrapee;
1413
private readonly string _encryptionBlock;
1514

1615
public EncryptionDecorator(IDataSource wrapee)
1716
: base(wrapee)
1817
{
19-
_wrapee = wrapee;
2018
_encryptionBlock = "##3NCRYPT3D##";
2119
}
2220

23-
public override string Read()
24-
{
25-
var encryptedData = _wrapee.Read();
26-
return Decrypt(encryptedData);
27-
}
28-
2921
public override void Write(string data)
3022
{
3123
Console.WriteLine("Encryption");
@@ -34,13 +26,15 @@ public override void Write(string data)
3426
_wrapee.Write(encryptedData);
3527
}
3628

37-
private string Decrypt(string data)
29+
public override string Read()
3830
{
39-
return data.Replace(_encryptionBlock, string.Empty);
31+
var encryptedData = _wrapee.Read();
32+
return Decrypt(encryptedData);
4033
}
4134

42-
private string Encrypt(string data)
43-
{
44-
return $"{_encryptionBlock}{data}{_encryptionBlock}";
45-
}
46-
}
35+
private string Encrypt(string data) =>
36+
$"{_encryptionBlock}{data}{_encryptionBlock}";
37+
38+
private string Decrypt(string data) =>
39+
data.Replace(_encryptionBlock, string.Empty, StringComparison.InvariantCulture);
40+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/PancakeExample/Components/BigPancake.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ public BigPancake()
99
Description = "Big pancake";
1010
}
1111

12-
public override double CalculatePrice()
13-
{
14-
return 10.0;
15-
}
16-
}
12+
public override double CalculatePrice() => 10.0;
13+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/PancakeExample/Components/Common/Pancake.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
public abstract class Pancake
44
{
5-
public string Description { get; set; }
5+
public string Description { get; set; } = string.Empty;
66

77
public abstract double CalculatePrice();
88

9-
public virtual string GetDescriptionAndAddIns()
10-
{
11-
return Description;
12-
}
13-
}
9+
public virtual string GetDescriptionAndAddIns() => Description;
10+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/PancakeExample/Components/SmallPancake.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ public SmallPancake()
99
Description = "Small pancake";
1010
}
1111

12-
public override double CalculatePrice()
13-
{
14-
return 6.0;
15-
}
16-
}
12+
public override double CalculatePrice() => 6.0;
13+
}

src/StructuralPatterns/Decorator/DecoratorLibrary/PancakeExample/Decorators/Common/PancakeDecorator.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ public PancakeDecorator(Pancake pancake)
1111
_pancake = pancake;
1212
}
1313

14-
public override double CalculatePrice()
15-
{
16-
return _pancake.CalculatePrice();
17-
}
14+
public override double CalculatePrice() => _pancake.CalculatePrice();
1815

19-
public override string GetDescriptionAndAddIns()
20-
{
21-
return $"{_pancake.GetDescriptionAndAddIns()}, {Description}";
22-
}
23-
}
16+
public override string GetDescriptionAndAddIns() => $"{_pancake.GetDescriptionAndAddIns()}, {Description}";
17+
}

0 commit comments

Comments
 (0)