Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 750027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:30:11+00:00 2026-05-14T14:30:11+00:00

One stumbles upon this phrase when reading about design patterns. But I don’t understand

  • 0

One stumbles upon this phrase when reading about design patterns.

But I don’t understand it, could someone explain this for me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:30:12+00:00Added an answer on May 14, 2026 at 2:30 pm

    Interfaces are just contracts or signatures and they don’t know
    anything about implementations.

    Coding against interface means, the client code always holds an Interface object which is supplied by a factory. Any instance returned by the factory would be of type Interface which any factory candidate class must have implemented. This way the client program is not worried about implementation and the interface signature determines what all operations can be done. This can be used to change the behavior of a program at run-time. It also helps you to write far better programs from the maintenance point of view.

    Here’s a basic example for you.

    public enum Language
    {
        English, German, Spanish
    }
    
    public class SpeakerFactory
    {
        public static ISpeaker CreateSpeaker(Language language)
        {
            switch (language)
            {
                case Language.English:
                    return new EnglishSpeaker();
                case Language.German:
                    return new GermanSpeaker();
                case Language.Spanish:
                    return new SpanishSpeaker();
                default:
                    throw new ApplicationException("No speaker can speak such language");
            }
        }
    }
    
    [STAThread]
    static void Main()
    {
        //This is your client code.
        ISpeaker speaker = SpeakerFactory.CreateSpeaker(Language.English);
        speaker.Speak();
        Console.ReadLine();
    }
    
    public interface ISpeaker
    {
        void Speak();
    }
    
    public class EnglishSpeaker : ISpeaker
    {
        public EnglishSpeaker() { }
    
        #region ISpeaker Members
    
        public void Speak()
        {
            Console.WriteLine("I speak English.");
        }
    
        #endregion
    }
    
    public class GermanSpeaker : ISpeaker
    {
        public GermanSpeaker() { }
    
        #region ISpeaker Members
    
        public void Speak()
        {
            Console.WriteLine("I speak German.");
        }
    
        #endregion
    }
    
    public class SpanishSpeaker : ISpeaker
    {
        public SpanishSpeaker() { }
    
        #region ISpeaker Members
    
        public void Speak()
        {
            Console.WriteLine("I speak Spanish.");
        }
    
        #endregion
    }
    

    alt text

    This is just a basic example and
    actual explanation of the principle is
    beyond the scope of this answer.

    EDIT

    I have updated the example above and added an abstract Speaker base class. In this update, I added a feature to all Speakers to “SayHello”. All speaker speak “Hello World”. So that’s a common feature with similar function. Refer to the class diagram and you’ll find that Speaker abstract class implement ISpeaker interface and marks the Speak() as abstract which means that the each Speaker implementation is responsible for implementing the Speak() method since it varies from Speaker to Speaker. But all speaker say “Hello” unanimously. So in the abstract Speaker class we define a method that says “Hello World” and each Speaker implementation will derive the SayHello() method.

    Consider a case where SpanishSpeaker cannot Say Hello so in that case you can override the SayHello() method for Spanish Speaker and raise proper exception.

    Please note that, we have
    not made any changes to Interface
    ISpeaker. And the client code and
    SpeakerFactory also remain unaffected
    unchanged. And this is what we achieve by Programming-to-Interface.

    And we could achieve this behavior by simply adding a base abstract class Speaker and some minor modification in Each implementation thus leaving the original program unchanged. This is a desired feature of any application and it makes your application easily maintainable.

    public enum Language
    {
        English, German, Spanish
    }
    
    public class SpeakerFactory
    {
        public static ISpeaker CreateSpeaker(Language language)
        {
            switch (language)
            {
                case Language.English:
                    return new EnglishSpeaker();
                case Language.German:
                    return new GermanSpeaker();
                case Language.Spanish:
                    return new SpanishSpeaker();
                default:
                    throw new ApplicationException("No speaker can speak such language");
            }
        }
    }
    
    class Program
    {
        [STAThread]
        static void Main()
        {
            //This is your client code.
            ISpeaker speaker = SpeakerFactory.CreateSpeaker(Language.English);
            speaker.Speak();
            Console.ReadLine();
        }
    }
    
    public interface ISpeaker
    {
        void Speak();
    }
    
    public abstract class Speaker : ISpeaker
    {
    
        #region ISpeaker Members
    
        public abstract void Speak();
    
        public virtual void SayHello()
        {
            Console.WriteLine("Hello world.");
        }
    
        #endregion
    }
    
    public class EnglishSpeaker : Speaker
    {
        public EnglishSpeaker() { }
    
        #region ISpeaker Members
    
        public override void Speak()
        {
            this.SayHello();
            Console.WriteLine("I speak English.");
        }
    
        #endregion
    }
    
    public class GermanSpeaker : Speaker
    {
        public GermanSpeaker() { }
    
        #region ISpeaker Members
    
        public override void Speak()
        {
            Console.WriteLine("I speak German.");
            this.SayHello();
        }
    
        #endregion
    }
    
    public class SpanishSpeaker : Speaker
    {
        public SpanishSpeaker() { }
    
        #region ISpeaker Members
    
        public override void Speak()
        {
            Console.WriteLine("I speak Spanish.");
        }
    
        public override void SayHello()
        {
            throw new ApplicationException("I cannot say Hello World.");
        }
    
        #endregion
    }
    

    alt text

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.