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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:48:55+00:00 2026-06-09T13:48:55+00:00

So say I have class A, B, and C. Class A has a single

  • 0

So say I have class A, B, and C. Class A has a single responsibility but needs functionality from class B and C so at first I wanted to get A to inherit from B and C then realized by following the “composition over inheritance” principle and making B and C members of A I could reduce the rigidity of the design and make those two classes more reusable.

At first B and C only needed to be instantiated in the constructor of A but eventually they had methods that needed to be called in two or three other places- as I was reusing the classes elsewhere I was forgetting to call some of the methods in the right places creating a lot of unnecessary defects and time wasted… my question is does dependency injection help with this problem, does it help reduce the complexity of using composition, and if so how?

public class A
{
     private mB;
     private mC;
     public A(IB b, IC c)
     {
         mB = b;
         mC = c;
     }
     public MethodX()
     {
         mB.DoWhatever();
     }
     public MethodY()
     {
         mC.DoSomething();
     }
}

From what I understand DI would let me configure what concrete classes for IB and IC get put through the constructor like this and handle its creation- but how else would it help (complexity wise)?

I decided to ask this question based on not understanding this article: http://lostechies.com/chadmyers/2010/02/13/composition-versus-inheritance/

For a real life example, say I have a State class that contains an EventListener class which has to register its events when the State calls its Begin method, unregister when it calls its End method.

  • 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-09T13:48:56+00:00Added an answer on June 9, 2026 at 1:48 pm

    Your reasoning is sound, and you are on the right track.

    The use of DI and composition via interfaces/abstractions makes it bot easier to tst and construct the code. You can use temporary “fake” implementations of B and C while building A, if that helps your process.

    The typical solution is to factor the construction and configuration of A, B and C out into a factory class or a subclass of A.

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

Sidebar

Related Questions

Say I have a class Customer which has a property FirstName . Then I
Okay so let's say I have class A... CLASS A has a method that
Let's say we have a class foo which has a private instance variable bar
Let's say I have a Size class which has height and width properties (in
Let's say I have an class called Star which has an attribute color .
Let's say we have a class that embed another parametric one, that has to
Lets say I have a LunchBox class with a property for FreezerPack that has
To keep things simple, lets say I have a Node class, each node has
Lets say I have the following DataMapper resources: class Post include DataMapper::Resource has n,
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()

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.