The document contains examples demonstrating the interface segregation principle (ISP). ISP states that clients should not be forced to depend on interfaces that they do not use. The first example shows an interface IWorker that defines work() and eat() methods, forcing classes like Robot to implement an unnecessary eat() method. The second example separates the interface into IWorkable and IFeedable, allowing classes like Robot to only implement the needed IWorkable interface.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
31 views
Examples Design Principles
The document contains examples demonstrating the interface segregation principle (ISP). ISP states that clients should not be forced to depend on interfaces that they do not use. The first example shows an interface IWorker that defines work() and eat() methods, forcing classes like Robot to implement an unnecessary eat() method. The second example separates the interface into IWorkable and IFeedable, allowing classes like Robot to only implement the needed IWorkable interface.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
SRP
public class OxygenMeter
{ public double OxygenSaturation { get; set; } public void ReadOxygenLevel() { using (MeterStream ms = new MeterStream("O2")) { int raw = ms.ReadByte(); OxygenSaturation = (double)raw / 255 * 100; } } public bool OxygenLow() { return OxygenSaturation <= 75; } public void ShowLowOxygenAlert() { Console.WriteLine("Oxygen low ({0:F1}%)", OxygenSaturation); } }
public class WeatherDisplay
{ private readonly int _Temperature; private readonly int _WindSpeed; public WeatherDisplay(int temperature, int windSpeed) { _Temperature = temperature; _WindSpeed = windSpeed; } public void DisplayWeather() { DisplayTemperature(); DisplayWindSpeed(); } private void DisplayTemperature() { Console.WriteLine("The current temperature is: " + _Temperature + " degrees celcius."); } private void DisplayWindSpeed() {
Console.WriteLine("The current wind speed is: " + _WindSpeed + "
meters per second."); }
// interface segregation principle - bad example
interface IWorker { public void work(); public void eat(); } class Worker : IWorker { public void work() { // ....working } public void eat() { // ...... eating in launch break } } class SuperWorker : IWorker { public void work() { //.... working much more } public void eat() { //.... eating in launch break } } class Manager { IWorker worker; public void setWorker(IWorker w) { worker = w; } public void manage() { worker.work(); } }
// interface segregation principle - good example
interface IWorker : IFeedable, IWorkable { } interface IWorkable { public void work(); } interface IFeedable { public void eat(); } class Worker : IWorkable, IFeedable { public void work() { // ....working } public void eat() { //.... eating in launch break } } class Robot : IWorkable { public void work() { // ....working } } class SuperWorker : IWorkable, IFeedable { public void work() { //.... working much more } public void eat() { //.... eating in launch break } } class Manager { IWorkable worker; public void setWorker(IWorkable w) { worker = w; } public void manage() { worker.work(); } }