Last active
February 14, 2021 01:15
-
-
Save MelbourneDeveloper/1eaf53ffc5944ab3eb4f339bde4ca306 to your computer and use it in GitHub Desktop.
Null Object Example
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
#nullable enable | |
namespace NullObjectExample | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var someService = new SomeService(); | |
} | |
} | |
public class SomeService | |
{ | |
private readonly IPerformsAction performsAction; | |
public SomeService(IPerformsAction? performsAction = null) | |
{ | |
this.performsAction = performsAction ?? NullActionPerformer.Instance; | |
} | |
} | |
public interface IPerformsAction | |
{ | |
void PerformAction(); | |
} | |
public class NullActionPerformer : IPerformsAction | |
{ | |
public static NullActionPerformer Instance { get; } = new NullActionPerformer(); | |
public void PerformAction() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment