#What is best way to access some commonly used instance of object (avoiding lack of OPP)?

1 messages · Page 1 of 1 (latest)

rain spade
#

What is best way to access some commonly used instance of object (avoiding lack of OPP)?

native driftBOT
#

<@&987246399047479336> please have a look, thanks.

native driftBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

rain spade
#

also this class will have only one instance

#

also if i will use dependency injection then i dont know some common rules, like imagine that some dependency object needs only one field from my parent object, should i pass entire parent object to constructor or just one field, same for bigger amounts of needed fields, what are limits etc.

#

also another thing is what if i create field of parent class in dependency "A" class and get it through constructor to get values of parent's fields? and then for example dependency "B" needs dependency "A" + some other fields from parent, should i pass dependency "A" and just get field of parent from "A" and rest from this field or pass to "B" each needed parent field one after another?

#

im just not sure about what should be code structure in these cases

cobalt kite
# rain spade im just not sure about what should be code structure in these cases

It's hard to say without any context. Your thoughts seem all over the place and you're not giving any context to how you came to these issues so it's hard to help. You've also asked several vague questions and it's hard to really see how they're all relevant. It's usually best to stick to one specific question, provide the context to your problem and give us code. Nevertheless, I'll give it a shot but I suspect I may be giving answers that aren't useful for you context.

What is best way to access some commonly used instance of object (avoiding lack of OPP)?

You access a commonly used object in the same way you access an uncommonly used object. There isn't really a rule saying you have to use an object one way if you access it x times and a different way if you access it y times. Again though, maybe extra context would change the answer.

also this class will have only one instance

That's called a singleton

also if i will use dependency injection then i dont know some common rules, like imagine that some dependency object needs only one field from my parent object, should i pass entire parent object to constructor or just one field, same for bigger amounts of needed fields, what are limits etc.

You usually want to encapsulate the fields of a class so you typically don't want to expose them. It is difficult to give you a specific answer though since the question is very vague in the first place. Again, context might change this answer.

also another thing is what if i create field of parent class in dependency "A" class and get it through constructor to get values of parent's fields? and then for example dependency "B" needs dependency "A" + some other fields from parent, should i pass dependency "A" and just get field of parent from "A" and rest from this field or pass to "B" each needed parent field one after another?

No clue what you're saying here. Show code.

rain spade
#
public class Main {
    private final DependencyA dependencyA;
    private final DependencyB dependencyB;
    private final int something = 321312312;

    public Main() {
        this.dependencyA = new DependencyA(this);
        this.dependencyB = new DependencyB(this);
    }

    public DependencyA getDependencyA() {
        return dependencyA;
    }

    public DependencyB getDependencyB() {
        return dependencyB;
    }

    public int getSomething() {
        return something;
    }
}
public class DependencyA {
    //lets say that it needs dependencyB instance and "something" field
    private final Main main;

    public DependencyA(Main main) {
        this.main = main;
    }
}
public class DependencyB {
    //lets say that it needs dependencyA instance and "something" field

    private final Main main;

    public DependencyB(Main main) {
        this.main = main;
    }
}
native driftBOT
rain spade
#

and what is proper way to provide things that these dependencies needs

#

should i use getters from Main class?

#

or should i pass only needed things to each dependency instead of passing main instance

#

like i dont want to violate laws like law and demeter and also if i will create much more dependencies in dependencies then this code will get messy, should i pass main to every depencency of dependency?
like it will cause bad code if it will be like main.getDependencyA().getDependencyC().getDependencyD().getSomeField()

#

also bad thing will be if dependency wont have main instance as field and it will have other dependency and i will get main instance from this other dependency

#

it will go in reverse way kinda

#

and idk how to keep my code clean in these situations, are there some rules? like some chained call limit or dependency limits etc?

#

also imagine that i have Util that have static method that requires some instance and i will use it a lot, do i need to pass this instance to this method every time if this instance is like singleton?? and still i want to avoid creating static field in other places than Util

cobalt kite
# rain spade like i dont want to violate laws like law and demeter and also if i will create ...

So you normally just want to dependency inject DependencyA and DependencyB themselves. You don't want to inject Main and then access DependencyA and B through setters. The same for the field:

public class Main {

  public static void main(String[] args) {
    String something = "123";
    DependencyA a = new DependencyA(something);
    DependencyB b = new DependencyB(b);

    b.doSomething();
  }
}

Note, that your example had a circular dependency. so I couldn't inject B into A, because B would need A to be created, creating a bit of a dilemma. Although there are ways to work around this, I would try your best to just avoid circular dependencies.

#

Now, should you always just inject the lowest child dependency? The answer is no. Sometimes you want but sometimes you don't and this is where software development gets tricky. The question you want to ask "Is the one cohesive piece of code?". If it is then you want to inject A, the root dependency. But if A, B, C and D are supposed to be decoupled then you want to inject D, the lowest child.

#

It can be tricky to figure out when code is cohesive or not but usually when multiple classes are cohesive they make up a composite. You need to ask yourself "Does it conceptually make sense for D to exist without A?". If the answer is no then it's a cohesive composite.

For example, take a Car. A Car can easily be seen as a composite of a bunch of different things for example, wheels and an engine. If you are a user who just wants to get from A to B, then is having a wheel without a car useful? No, so chances are you want to think of the Car and everything in it to be a composite. You probably would just inject Car rather than Wheel in this case.

On the other hand you might have an application that sells cars and car parts. If you only ever allowed a wheel to exist alongside a car and never by itself then this would be problematic. In this case you probably want to think of each part as being separate and inject the wheel by itself.

I appreciate that is a bit of oversimplified example. In a real software project, it's usually not as clear cut and sometimes you do get into debates with your colleagues on whether it should be one or another so this is never really a solved problem that you can always know for sure.

rain spade
#

thx

rain spade
#

i have also found something like Model-View-Controller (MVC) pattern