Question

In: Computer Science

This violates the ISP principle in SOLID, can someone explain why and refactor? Thank you! public...

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();
        }
    }

Solutions

Expert Solution

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:

  1.  OnDoubleClick()
  2. 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.


Related Solutions

This violates Liskov Substitution Principle (LSP), can someone explain why and refactor it so it does...
This violates Liskov Substitution Principle (LSP), can someone explain why and refactor it so it does not? public abstract class Bird { public abstract void LayEgg(); public abstract void FlyAway(); } public class Penguin : Bird { public override void LayEgg() { //Make husband penguin do all the incubation work. } public override void FlyAway() { //Penguins can't fly. throw new NotImplementedException(); } public void SwimAway() { //Swimming is way more fun. } }
Hello can someone please explain the AS AD curve step by step to me? Thank you
Hello can someone please explain the AS AD curve step by step to me? Thank you
Can someone please explain these problems, I don't understand, please and thank you!! The patients in...
Can someone please explain these problems, I don't understand, please and thank you!! The patients in the Digoxin trial dataset can be considered a population of people suffering from heart failure. These patients were examined before the drug trial began, and their heart rate and blood pressure were recorded. The mean and standard deviation of the variables are listed below. Each variable follows a normal distribution. Heart rate (beats/min)                          μ = 78.8            σ = 12.66 Systolic blood pressure (mmHg)             μ...
Can you think of a firm that violates the concept of mean reversion? Why do you...
Can you think of a firm that violates the concept of mean reversion? Why do you think this occurs?
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your code to enhance the user experience and to use objects and classes. All functional requirements in Project 1 remain, except where enhancing the system replaces specific functions. Functional Requirements The console entry point for the user inputs is on the same line as the prompt. (new) User enters name at the beginning of a session. System covers four math operations – addition, subtraction, multiplication,...
Can someone explain me how to do these questions. Thank you 1) On July 1, 20X1,...
Can someone explain me how to do these questions. Thank you 1) On July 1, 20X1, three individuals invested $100,000 each to form MIX Corporation and each received 10,000 shares. MIX had total revenues of $15,000 during 20X1 and $40,000 during 20X2. Total expenses for the same periods were $8,000 and $22,000, respectively. Cash dividends paid out to shareholders totalled $6,000 in 20X1 and $12,000 in 20X2. What was the amount of MIX's retained earnings at the end of 20X1...
Can you explain why Eindtein never accepted Heisenberg’s uncertainty principle ???
Can you explain why Eindtein never accepted Heisenberg’s uncertainty principle ???
Can someone please provide a feedback on the discussion post below. Thank you. It is a...
Can someone please provide a feedback on the discussion post below. Thank you. It is a marketing management class. Amazon.com is one of the fastest growing companies to date. Amazon utilizes Facebook, Twitter, YouTube, and a company blog in order to reach out to their consumers. What I have noticed on their YouTube channel is that they have a variety of different kinds of videos that all relate to the average consumer. In addition, Amazon is able to use celebrity...
Could someone please explain the consequences of low lung compliance. Thank you
Could someone please explain the consequences of low lung compliance. Thank you
Can someone explain in terms of physics why or why not a strong magnet can damage...
Can someone explain in terms of physics why or why not a strong magnet can damage a laptop screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT