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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:25:39+00:00 2026-06-01T12:25:39+00:00

The project has a goal: use interface centric design. Basically we declare classes of

  • 0

The project has a goal: use interface centric design. Basically we declare classes of only public pure functions and have real classes inherit from them.

One question comes up now – how to reuse the utility functions?

Here is an example:

class InterF1 {
  public:
    int f1() = 0;
    int g1() = 0;   
}; 

class InterF2 {
  public:
    int f2() = 0;
}; 

class C1: public InterF1 {
  public:
    int f1();
    int g1();
};


class C2: public InterF1, InterF2 {
  public:
    int f1();
    int g1();
    int f2();
};

While I am implementing C1::f1() and C2::f1(), I see code duplication between these 2 functions. How should I remove the duplication?

Here are my thoughts:

1) I can add protected to interface class like this:

class InterF1 {
  public:
    int f1() = 0;
    int g1() = 0;   
  protected:
    int util();
    int convenient_var_calculated_by_util;
}; 

in the above design, C1::f1() and C2::f2() can both call util().

2) I can abstract a new interface:

class UtilI {
    public:
      int util(int in_var) = 0;
};

In this design, InterF1, C1 and C2 are changed to

class InterF1 {
  public:
    int f1(UtilI u) = 0;
    int g1() = 0;   
}; 

class C1: public InterF1 {
  public:
    int f1(UtilI u);
    int g1();
};


class C2: public InterF1, InterF2 {
  public:
    int f1(UtilI u);
    int g1();
    int f2();
};

and C1::f1() and C2::f1() both call UtilI API.

Solution 2 seems more aligned with our “interface centric” goal and it does look better. But I have one concern, InterF1 and InterF2 themselves should be higher level abstraction than UtilI which is more of implementation detail, am I mixing the 2 levels? Another words, if implementation changes later, I will need to come back to update f1() and f2() signature again, that does not sound right.

Solution 1 does seem handy, but it looks less “pure” to me, am I too much dogmatic here?

  • 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-06-01T12:25:41+00:00Added an answer on June 1, 2026 at 12:25 pm

    Normally I would say you should put the common implementation in the base class – but you have a requirement that the base class is purely virtual. So instead I’d recommend either:

    1. Create an intermediate implementation class

      This approach is okay if every class that wants to implement InterF2 also wants to implement InterF1.

      The approach is to implement

      class InterF1Impl: public InterF1 {
        public:
          int f1();
          int g1();
      }
      

      Then C1 and C2 can both be derived from InterF1Impl which implements the common f1() and g1() methods.

      However, this approach won’t extend well if you have another class, C3 : public InterF2, which wants to share an implementation of f2() with C2 but doesn’t want to implement InterF1.

      An alternative (and better) approach is

    2. Use composition to include an implementation class.

      In this approach, implement InterF1Impl: public InterF1 as above, but instead of deriving C1: public InterF1Impl, let InterF1Impl be one part of the C1 class.

      class C1: public InterF1 {
        private:
          InterF1Impl * f1impl;
        public:
          int f1();
          int g1();
      }
      

      The C1::f1() and C1::g1() methods simply call the corresponding methods from f1impl.

      This way, if you need a common implementation of f2(), then you can also implement InterF2: public InterF2, and C2 can be implemented in a similar way:

      class C2: public InterF1, InterF2 {
        private:
          InterF1Impl * f1impl;
          InterF2Impl * f2impl;
        public:
          int f1(); /* calls f1impl->f1() */
          int g1(); /* calls f1impl->g1() */
          int f2(); /* calls f2impl->f2() */
      }
      

      And another class could just use the implementation of InterF2 without also implementing InterF1 if you later needed such a class.

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

Sidebar

Related Questions

My project has collected CSS entropy (unused classes) during its course of development. Now
I have a project that has a few dependencies on other Jars. I currently
The goal: For a personal project, I'd like to simulate an old-fashioned computer interface
I'm working on a legacy project that has heavy use of Singletons. While most
I currently have a project that is a 'Business Object' project, and our goal
I have a project which I would like to make more use of smart
My project has both client and server components in the same solution file. I
Our project has one folder that is not part of the solution. How can
My project has multiple themes with different colors. I need to skin certain textboxes
The project has been working fine in 2003 but when opening it in 2008

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.