In: Computer Science
This violates the ISP principle in SOLID, can someone explain why and refactor? Thank you! public interface IGesture { void OnClick(); void OnSwipe(); void OnDoubleClick(); } public class LaunchButton : IGesture { public void OnClick() { //Launch nuclear missiles } public void OnDoubleClick() { throw new NotImplementedException(); } public void OnSwipe() { throw new NotImplementedException(); } }
Hi,
Interface Segregation Principle is - The Class should have implemented only those interface methods or elements that are necessary for that particular class. If there are unnecessary elements are available in the interface which implemented by the class then segragate the interface and declare only realted elemtns into it and user that in the class.
In the above example,
Class does not follow the Interface Segregation Principle because Class LaunchButton does not need to Implement the below functions:
OnDoubleClick()
OnSwipe()
We can fix this issue as shown below: See the Refractor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RandomNumberGuess
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
/// <summary>
/// Interface with Click Method
/// </summary>
public interface IGestureClick
{
void OnClick();
}
/// <summary>
/// Interface with Swipe Method
/// </summary>
public interface IGesturSwipe
{
void OnSwipe();
}
/// <summary>
/// Interface with Double Click Method
/// </summary>
public interface IGesturDoubleClick
{
void OnDoubleClick();
}
/// <summary>
/// Class who only need ClickMethod implement the IGestureClick Interface
/// </summary>
public class LaunchButton : IGestureClick
{
public void OnClick()
{
//Launch nuclear missiles
}
}
/// <summary>
/// Class who only need DoubleClick implement the IGesturDoubleClick Interface
/// </summary>
public class LaunchDoubleButton : IGesturDoubleClick
{
public void OnDoubleClick()
{
//Launch nuclear missiles
}
}
/// <summary>
/// Class who only need OnSwipe implement the IGesturSwipe Interface
/// </summary>
public class LaunchSwipe : IGesturSwipe
{
public void OnSwipe()
{
//Launch nuclear missiles
}
}
/// <summary>
/// Class who only need Click and DoubleClick methods, Implement the IGesturDoubleClick, IGestureClick Interfacec
/// </summary>
public class LaunchSingleDoubleClick : IGesturDoubleClick, IGestureClick
{
public void OnDoubleClick()
{
//Launch nuclear missiles
}
public void OnClick()
{
//Launch nuclear missiles
}
}
/// <summary>
/// Class who need Click, DoubleClick and swipe methods implement the IGesturDoubleClick, IGestureClick, IGesturSwipe Interfacec
/// </summary>
public class LaunchSingleDoubleSwipeClick : IGesturDoubleClick, IGestureClick, IGesturSwipe
{
public void OnDoubleClick()
{
//Launch nuclear missiles
}
public void OnClick()
{
//Launch nuclear missiles
}
public void OnSwipe()
{
//Launch nuclear missiles
}
}
}
In the above example, I have a segregated Interface i.e. Created 3 interfaces and each interface is having only a single purpose so this is easy to implement whenever our Class needs then we cad decide which interface need to implement based on the user requirement and we can take the decision.
Thanks.