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

  • Home
  • SEARCH
  • 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 1021487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:19:06+00:00 2026-05-16T11:19:06+00:00

I have a design problem I’d like to solve. I have an interface, lets

  • 0

I have a design problem I’d like to solve.
I have an interface, lets call it IProtocol, which is implemented by two separate classes. We’re looking at over 600 lines of code here. The vast majority of the stuff they do is the same, except for some specific areas, like DiffStuff();

Current structure is something like this:

public class Protocol1 : IProtocol
{
  MyInterfaceMethod1()
  {
     Same1();
     DiffStuff();
     Same2();
  }
}

And

public class Protocol2 : IProtocol
{
  MyInterfaceMethod1()
  {
     Same1();
     Same2();
  }
}

I’m concerned with having copy-paste errors and the classic problem of code duplication if I keep the two protocols separate. We’re talking about a full 600 lines of code each, not some simple methods.

I’m considering changing the implementation of Protocol1 to inherit from protocol2, like this (Protocol2 would mostly stay the same, except I’d have to wrap Same1() and Same2() into private methods.)

public class Protocol1 : Protocol2
{
  void Same1()
  {
     base.Same1();
  }

  void Same2()
  {
     base.Same2();
  }

  MyInterfaceMethod1()
  {
     Same1();
     DiffStuff();
     Same2();
  }
}  

Is this the right way to go about dealing with this problem?

Edit:
Many people helped me with this question, thanks for the clear understanding. In my case, the two objects are not of the same type, even though much of their implementation is shared, so I went with Bobby’s suggestion to use abstract base class, creating small methods to encapsulate changes between the classes. Additional Thanks to:

  • jloubert
  • Hans Passant
  • Jeff Sternal
  • 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-16T11:19:06+00:00Added an answer on May 16, 2026 at 11:19 am
        /// <summary>
        /// IProtocol interface
        /// </summary>
        public interface IProtocol
        {
            void MyInterfaceMethod1();
            void Same1();
            void Same2();
        }
    

    then…

    public abstract class ProtocolBase : IProtocol
    {
        #region IProtocol Members
    
        public void MyInterfaceMethod1()
        {
            // Implementation elided...
        }
    
        public void Same1()
        {
            // Implementation elided...
        }
    
        public void Same2()
        {
            // Implementation elided...
        }
    
        public abstract void DiffStuff();
    
        #endregion
    }
    

    finally…

    public sealed class Protocol1 : ProtocolBase
    {
        public override void DiffStuff()
        {
            // Implementation elided...
        }
    }
    
    public sealed class Protocol2 : ProtocolBase
    {
        public override void DiffStuff()
        {
            // Implementation elided...
        }
    }
    
    • 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.