Created
October 1, 2021 14:07
-
-
Save mizrael/c365b9a2c1566756d67570b5d90afa96 to your computer and use it in GitHub Desktop.
sample Factory Pattern in C#, handling services with dependencies.
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 interface IDocumentProcessor { } | |
public class PdfProcessor : IDocumentProcessor { | |
public PdfProcessor(PdfReader reader) { } | |
} | |
public class OdtProcessor : IDocumentProcessor { | |
public OdtProcessor(OdtReader reader) { } | |
} | |
public class WordProcessor : IDocumentProcessor { | |
public WordProcessor(WordReader reader) { } | |
} | |
public interface IDocumentProcessorFactory | |
{ | |
IDocumentProcessor Create(string type); | |
} | |
public class DocumentProcessorFactory : IDocumentProcessorFactory | |
{ | |
private readonly PdfReader _pdfReader; | |
private readonly OdtReader _odtReader; | |
private readonly WordReader _wordReader; | |
public IDocumentProcessor Create(string type) | |
{ | |
return type.ToLower() switch | |
{ | |
"pdf" => new PdfProcessor(_pdfReader), | |
"odt" => new OdtProcessor(_odtReader), | |
"doc" => new WordProcessor(_wordReader), | |
_ => throw new NotImplementedException(), | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment