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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:40:47+00:00 2026-06-02T04:40:47+00:00

I have three major classes, lets say A, B and C. A instantiates both

  • 0

I have three major classes, lets say A, B and C.
A instantiates both B and C. B also depends on C. B accesses C’s object through setter/getter methods. This is fine.
My problem is that A’s implementation is tightly coupled with B’s and C’s implementation.
I want to decouple this.
I don’t want to use the spring framework. Is there any other way? I thought of creating factories for their(B,C) initialization s but that means whenever A or C will need to access B they have to create a new instance of the Factory again.. this doesn’t seem right.
How should I solve this?
I am not sure if the IOC container is useful 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-02T04:40:49+00:00Added an answer on June 2, 2026 at 4:40 am

    A should not directly instantiate B or C. It should instead accept instances of them as constructor parameters:

    public class A
    {
        private B b;
        private C c;
    
        public A(B b, C c)
        {
            this.b = b;
            this.c = c;
        }
    }
    
    // When creating your object graph, in the "composition root":
    B b = new B();
    C c = new C(b);
    A a = new A(b, c);
    

    This decouples A from the details of instantiating Bs and Cs, by injecting it with those dependencies (thus, dependency injection).

    To decouple A from the implementation, you’ll want to create interfaces for the functionality of B and C that A cares about. Say, Bable and Cable. Then A can accept as its constructor parameters a Bable and a Cable, and doesn’t care what class happens to implement those interfaces, or how the implementation works—just that it conforms to the contracts laid out by Bable and Cable.

    Here is a fully fleshed-out example of what I am talking about: https://gist.github.com/2402514

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

Sidebar

Related Questions

So in EhCache we have three major classes: Cache , CacheManager , and Store
Have three classes User, Group and Field. Many to many relationship on User /
I inherited a project that needs to be mutithreaded. There is three major classes
I have this XML file: http://www.basket.ba/ksbih/xml/200_07.xml I've used all the major functions and classes
I have a JSF application with several major components and a component tree under
Have three divs in a container that I want to float over a large
I have three tables A: A.pID primary key, A.Name nvarchar(250) B: B.pID primary key,
I have three tables: videos, videos_categories, and categories. The tables look like this: videos:
I have three column in my datagridview .One is text ,one is Combo and
I have three computers on my LAN, one running ubuntu , one running openSuse

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.