Skip to content

Instantly share code, notes, and snippets.

@code-machina
Created December 15, 2018 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-machina/7ef5b42a65bab24f55caf267a9f98731 to your computer and use it in GitHub Desktop.
Save code-machina/7ef5b42a65bab24f55caf267a9f98731 to your computer and use it in GitHub Desktop.
Interface Segregation Principle (ISP) PoC
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