Created
December 15, 2018 01:05
-
-
Save code-machina/7ef5b42a65bab24f55caf267a9f98731 to your computer and use it in GitHub Desktop.
Interface Segregation Principle (ISP) PoC
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
using System; | |
namespace DotNetDesignPatternDemos.SOLID.InterfaceSegregationPrinciple | |
{ | |
public class Document | |
{ | |
} | |
public interface IMachine | |
{ | |
void Print(Document d); | |
void Fax(Document d); | |
void Scan(Document d); | |
} | |
// ok if you need a multifunction machine | |
public class MultiFunctionPrinter : IMachine | |
{ | |
public void Print(Document d) | |
{ | |
// | |
} | |
public void Fax(Document d) | |
{ | |
// | |
} | |
public void Scan(Document d) | |
{ | |
// | |
} | |
} | |
public class OldFashionedPrinter : IMachine | |
{ | |
public void Print(Document d) | |
{ | |
// yep | |
} | |
public void Fax(Document d) | |
{ | |
throw new System.NotImplementedException(); | |
} | |
public void Scan(Document d) | |
{ | |
throw new System.NotImplementedException(); | |
} | |
} | |
public interface IPrinter | |
{ | |
void Print(Document d); | |
} | |
public interface IScanner | |
{ | |
void Scan(Document d); | |
} | |
public class Printer : IPrinter | |
{ | |
public void Print(Document d) | |
{ | |
} | |
} | |
public class Photocopier : IPrinter, IScanner | |
{ | |
public void Print(Document d) | |
{ | |
throw new System.NotImplementedException(); | |
} | |
public void Scan(Document d) | |
{ | |
throw new System.NotImplementedException(); | |
} | |
} | |
public interface IMultiFunctionDevice : IPrinter, IScanner // | |
{ | |
} | |
public struct MultiFunctionMachine : IMultiFunctionDevice | |
{ | |
// compose this out of several modules | |
private IPrinter printer; | |
private IScanner scanner; | |
public MultiFunctionMachine(IPrinter printer, IScanner scanner) | |
{ | |
if (printer == null) | |
{ | |
throw new ArgumentNullException(paramName: nameof(printer)); | |
} | |
if (scanner == null) | |
{ | |
throw new ArgumentNullException(paramName: nameof(scanner)); | |
} | |
this.printer = printer; | |
this.scanner = scanner; | |
} | |
public void Print(Document d) | |
{ | |
printer.Print(d); | |
} | |
public void Scan(Document d) | |
{ | |
scanner.Scan(d); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment