Last active
November 25, 2018 12:14
-
-
Save NTCoding/c773da292428255fe9f9795d6c0a5d17 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public abstract class Comment | |
| { | |
| public static ProposedComment Parse(String proposedComment) | |
| { | |
| return new ProposedComment(proposedComment); | |
| } | |
| } | |
| public class ProposedComment : Comment | |
| { | |
| public ProposedComment(String value) | |
| { | |
| Value = value; | |
| } | |
| public Value { get; } | |
| } | |
| public abstract class ReviewedComment : Comment | |
| { | |
| ... | |
| } | |
| public class CleanComment : ReviewedComment | |
| { | |
| ... | |
| } | |
| public class ProfaneComment : ReviewedComment | |
| { | |
| ... | |
| } | |
| public interface ReviewComment | |
| { | |
| ReviewedComment Review(ProposedComment comment): ReviewedComment; | |
| } | |
| public class Program | |
| { | |
| ReviewComment rc; | |
| public static void Main(String[] args) | |
| { | |
| // As a caller, all I need to know is the Comment.Parse method. The type system guides me from here | |
| ProposedComment p = Comment.Parse("comment"); | |
| ReviewedComment r = rc.Review(p); | |
| // Make all states explicit and handle them at compile time. Avoid throwing hidden exceptions at runtime. | |
| switch (r) | |
| { | |
| case CleanComment cl: | |
| ... | |
| case ProfaneComment pr: | |
| ... | |
| } | |
| } | |
| } |
Author
I'd go for var p = ProposedComment.Parse("comment"); putting the Parse on ProposedComment. Because that's what you're starting out with. Why do ProposedComment and ReviewedComment share a base type? I'd totally decouple unless there's some behavior that needs both.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Key things I want to express in this pattern: