-
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
1 parent
0406979
commit 1338ca3
Showing
8 changed files
with
100 additions
and
0 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
18 changes: 18 additions & 0 deletions
18
Structural/DesignPatterns.Decorator/CompressionDecorator.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.Decorator; | ||
|
||
internal class CompressionDecorator : UploaderDecorator | ||
{ | ||
public CompressionDecorator(IFileUploader uploader) | ||
: base(uploader) | ||
{ | ||
} | ||
|
||
public override async Task Upload(string name, Stream stream) | ||
{ | ||
// TODO: add compression logic here. | ||
Console.WriteLine($"{nameof(CompressionDecorator)}.{nameof(Upload)}: started compressing a file..."); | ||
await Task.Delay(1000); | ||
Console.WriteLine($"{nameof(CompressionDecorator)}.{nameof(Upload)}: completed compressing a file."); | ||
await base.Upload(name, stream); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Structural/DesignPatterns.Decorator/DesignPatterns.Decorator.csproj
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>DesignPatterns.Decorator</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
Structural/DesignPatterns.Decorator/EncryptionDecorator.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.Decorator; | ||
|
||
internal class EncryptionDecorator : UploaderDecorator | ||
{ | ||
public EncryptionDecorator(IFileUploader uploader) | ||
: base(uploader) | ||
{ | ||
} | ||
|
||
public override async Task Upload(string name, Stream stream) | ||
{ | ||
// TODO: add encryption logic here. | ||
Console.WriteLine($"{nameof(EncryptionDecorator)}.{nameof(Upload)}: started encrypting a file..."); | ||
await Task.Delay(1000); | ||
Console.WriteLine($"{nameof(EncryptionDecorator)}.{nameof(Upload)}: completed encrypting a file."); | ||
await base.Upload(name, stream); | ||
} | ||
} |
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,12 @@ | ||
namespace DesignPatterns.Decorator; | ||
|
||
internal class FileUploader : IFileUploader | ||
{ | ||
public async Task Upload(string name, Stream stream) | ||
{ | ||
// TODO: add a client to upload files into cloud. | ||
Console.WriteLine($"{nameof(FileUploader)}.{nameof(this.Upload)}: started uploading a file..."); | ||
await Task.Delay(1000); | ||
Console.WriteLine($"{nameof(FileUploader)}.{nameof(this.Upload)}: completed uploading a file."); | ||
} | ||
} |
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,6 @@ | ||
namespace DesignPatterns.Decorator; | ||
|
||
public interface IFileUploader | ||
{ | ||
Task Upload(string name, Stream stream); | ||
} |
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,10 @@ | ||
using DesignPatterns.Decorator; | ||
|
||
IFileUploader fileUploader = new FileUploader(); | ||
fileUploader = new CompressionDecorator(fileUploader); | ||
fileUploader = new EncryptionDecorator(fileUploader); | ||
|
||
await using var stream = new MemoryStream(); | ||
await fileUploader.Upload("file.txt", stream); | ||
|
||
Console.WriteLine(); |
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,16 @@ | ||
namespace DesignPatterns.Decorator; | ||
|
||
abstract class UploaderDecorator : IFileUploader | ||
{ | ||
private readonly IFileUploader uploader; | ||
|
||
public UploaderDecorator(IFileUploader uploader) | ||
{ | ||
this.uploader = uploader; | ||
} | ||
|
||
public virtual Task Upload(string name, Stream stream) | ||
{ | ||
return this.uploader.Upload(name, stream); | ||
} | ||
} |