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 7609811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:15:52+00:00 2026-05-31T01:15:52+00:00

I have an Interface lets say ISendOut which I’ve inherited two different classes from

  • 0

I have an Interface lets say ISendOut which I’ve inherited two different classes from it
for example TransferViaSerialPort and TransferViaWirelessModule (I mean implement this Interface in these two classes). How can I design my software to both giving the ability to the user to choose (IN THE UI) between the methods of sending his/her data out via SerialPort or WirelessModule and not violate the OCP? Because if I want to have a “Switch Case” or an “If/Else” statement I will Violate the OCP.

  • 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-31T01:15:53+00:00Added an answer on May 31, 2026 at 1:15 am

    You need to use the Factory Pattern. And to make the Factory Pattern dynamic you can use Reflection and to show the Types of your Classes in the UI which are Implemented from ISendOut you can use Custom Attributes or other methods like using a Dictionary.

    [System.AttributeUsage(System.AttributeTargets.Class)]
    public class DisplayNameAttribute : Attribute
    {
        public DisplayNameAttribute(string displayName)
        {
            DisplayName = displayName;
        }
    
        public string DisplayName { get; set; }
    }
    
    public interface ISendOut
    {
        void Send(string data);
    }
    
    [DisplayName("Wireless")]
    public class WirelessSendOut : ISendOut
    {
        public void Send(string data)
        {
            MessageBox.Show("data sent through wireless.");
        }
    }
    
    [DisplayName("Serial")]
    public class SerialSendOut : ISendOut
    {
        public void Send(string data)
        {
            MessageBox.Show("data sent through serial port.");
        }
    }
    
    public static class SendOutFactory
    {
        public static ISendOut CreateSendOut(string typeName)
        {
            var types = Assembly.GetExecutingAssembly().GetTypes();
            var sendOutType = types.First(x => (typeof(ISendOut)).IsAssignableFrom(x) && x.Name == typeName);
            return (ISendOut) Activator.CreateInstance(sendOutType);
        }
    }
    
    public static class SendOutDiscovery
    {
        public static IEnumerable<NameType> Discover()
        {
            var types = Assembly.GetExecutingAssembly().GetTypes();
            var sendOutTypes = types.Where(x => x != typeof(ISendOut) && (typeof(ISendOut)).IsAssignableFrom(x));
            return sendOutTypes.Select(type => GetNameType(type)).ToList();
        }
    
        private static NameType GetNameType(Type type)
        {
            var nameType = new NameType
                               {
                                   DisplayName = GetDisplayName(type),
                                   TypeName = type.Name
                               };
            return nameType;
        }
    
        private static string GetDisplayName(Type type)
        {
            return ((DisplayNameAttribute)type.GetCustomAttributes(typeof (DisplayNameAttribute), false).First()).DisplayName;
        }
    }
    
    public class NameType //for binding in UI
    {
        public string DisplayName { get; set; }
        public string TypeName { get; set; }
    }
    
    public class SendOutViewModel //sample using in wpf (window contains a combobox)
    {
        public SendOutViewModel()
        {
            SendTypes = new ObservableCollection<NameType>(SendOutDiscovery.Discover());
        }
    
        public NameType SelectedSendType { get; set; } //bind to selected item in combobox
    
        public ObservableCollection<NameType> SendTypes { get; private set; } //bind to item source of combo
    
        public string Data { get; set; } //data to be sent
    
        public void Send()
        {
            ISendOut sendOut = SendOutFactory.CreateSendOut(SelectedSendType.TypeName);
            sendOut.Send(Data);
        }
    }
    

    Later I add UsbSendOut without modifying existing code (so not Breaking the OCP)

    [DisplayName("Usb")]
    public class UsbSendOut : ISendOut
    {
        public void Send(string data)
        {
            MessageBox.Show("data sent through usb.");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say we have a program which contains such classes: public interface AbstractItem {
lets say i have a custom widget which has a ClickHandler. Here's the example:
Lets say we have this interface: interface IVehicle { ... } And some classes
Lets say I have a class, which implements a generic interface public interface IItem
Lets say you have interface definition. That interface can be Operation . Then you
So lets say I have this interface: public interface IBox { public void setSize(int
To keep things simplified lets say I have an interface RandomProvider interface public interface
lets say I have aclass @interface Foo :NSobject { NSstring *a; NSDate *b; }
I have inherited a project that has an awkwardly big interface declared (lets call
Lets say i have the following set of interfaces.... public interface IStart { void

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.