Interface segregation principle
using System;
namespace interfacesegregation
{
public class Document
{
}
public interface IMachine
{
void Print(Document d);
void Scan(Document d);
void Fax(Document d);
}
public class MultiFunctionPrinter : IMachine
{
public void Print(Document d)
{
//
}
public void Scan(Document d)
{
//
}
public void Fax(Document d)
{
//
}
}
// The interface has too much functionaly that won't be used
public class Printer : IMachine
{
public void Print(Document d)
{
//
}
public void Scan(Document d)
{
throw new NotImplementedException();
}
public void Fax(Document d)
{
throw new NotImplementedException();
}
}
// We should separate the interface into smaller ones
public interface IPrinter
{
void Print(Document d);
}
public interface IScanner
{
void Scan(Document d);
}
public class Photocopier : IPrinter, IScanner
{
public void Print(Document d)
{
//
}
public void Scan(Document d)
{
//
}
}
public interface IMultiFunctionDevice : IPrinter, IScanner
{
}
public class MultiFunctionMachine : IMultiFunctionDevice
{
public void Print(Document d)
{
Console.WriteLine("Printing...");
}
public void Scan(Document d)
{
Console.WriteLine("Scanning...");
}
}
public class Program
{
static void Main(string[] args)
{
var machine = new MultiFunctionMachine();
machine.Scan(new Document());
machine.Print(new Document());
}
}
}
Comments
Post a Comment