-3

I want to change my code, also I want to use Interfaces so if someone wants a future so it could be easily implemented, without changing too much code.

So I successfully implemented my interface IPalindromeChecker. But the problem is in MainWindow now. So I'm not sure but I would make another Interface and with public void Output(string text) method. I've tried adding a method in IPalindromeChecker public void Output(string text) but it didn't work.

 interface ICheckPalindrome
    {
        bool IsPalindrome(string text);
    }
public class PalindromeChecker : ICheckPalindrome
    {

        /// <summary>
        /// Method for checking if the word/text is a palindrome.
        /// </summary>
        public bool IsPalindrome(string text)
        {
            int min = 0;
            int max = text.Length - 1;

            while (true)
            {
                if (min > max)
                {
                    return true;
                }

                char a = text[min];
                char b = text[max];

                if (a != b)
                {
                    return false;
                }

                min++;
                max--;
            }
        }
    }
  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            lblInput.Foreground = Brushes.ForestGreen;
            lblResult.Foreground = Brushes.ForestGreen;
            lblTitel.Foreground = Brushes.ForestGreen;

        }

        /// <summary>
        /// User input and checking the input if the word a palindrome is.
        /// </summary>
        private void InputText_TextChanged(object sender, TextChangedEventArgs e)
        {
            string text = InputText.Text;

            bool isPalindrome = TextChecker.PalindromeChecker(text); // HERE IS THE PROBLEM

            OutputText.Text = text + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");

            if (InputText.Text == string.Empty)
            {
                OutputText.Clear();
            }
        }
 public class PalindromeChecker : ICheckPalindrome
    {

        /// <summary>
        /// Method for checking if the word/text is a palindrome.
        /// </summary>
        public bool IsPalindrome(string text)
        {
            int min = 0;
            int max = text.Length - 1;

            while (true)
            {
                if (min > max)
                {
                    return true;
                }

                char a = text[min];
                char b = text[max];

                if (a != b)
                {
                    return false;
                }

                min++;
                max--;
            }
        }
    }
3
  • What is TextChecker.PalindromeChecker? How are you using the interface in MainWindow? Commented Jul 5, 2019 at 14:35
  • @Çöđěxěŕ: If you want to be able to easily switch the implementation of IsPalindrome without modifying the MainWindow, it shouldn't be static. This would defeat the purpose of using an interface in this first place. And since it implements the ICheckPalindrome interface, it cannot be static. Commented Jul 5, 2019 at 14:43
  • TBH, see this question along with that answer... Commented Jul 5, 2019 at 14:52

1 Answer 1

3

It's unclear what TextChecker.PalindromeChecker is but if you want to be able to switch implementations of the ICheckPalindrome interface without having to modify your MainWindow, you should inject the window with an implementation of ICheckPalindrome at runtime and write your code against the interface.

You could for example use a property to do this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        lblInput.Foreground = Brushes.ForestGreen;
        lblResult.Foreground = Brushes.ForestGreen;
        lblTitel.Foreground = Brushes.ForestGreen;

        //use the PalindromeChecker as the default implementation
        PalindromeChecker = new PalindromeChecker();

    }

    public ICheckPalindrome PalindromeChecker { get; set; } //<--

    private void InputText_TextChanged(object sender, TextChangedEventArgs e)
    {
        string text = InputText.Text;

        bool isPalindrome = PalindromeChecker.IsPalindrome(text);

        OutputText.Text = text + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");

        if (InputText.Text == string.Empty)
        {
            OutputText.Clear();
        }
    }
}

Switching to another implementation is then as simple as just setting the PalindromeChecker property of the MainWindow to an instance of another class that implements the same ICheckPalindrome interface.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, PalindromeChecker is method where I check if the word is Palindrome, and I used TextChecker.PalindromeChecker to call the method. I can't set the instance public ICheckPalindrome PalindromeChecker { get; set; } //<--
I don't understand. Why can't you define a property?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.