java composition to reduce code complexity [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 days ago.
Improve this question
Let us say there is a class LargeClass with 4k lines of code. I would like to reduce the file size to manage it better by extracting the code into some other classes.
class LargeClass extends SomeLibraryClass {
void featureOneLargeMethod1() {
}
void featureOneLargeMethod2() {
}
void featureOneLargeMethod3() {
}
void featureTwoLargeMethod1() {
}
void featureTwoLargeMethod2() {
}
void featureTwoLargeMethod3() {
}
}
I could create two classes FeatureOne and FeatureTwo and move the corresponding methods into these new classes but the featureOneLargeMethod1() calls protected SomeLibraryClass.someLibraryMethod() which makes it impossible to move these methods. I was thinking of creating a wrapper interface that LargeClass implements and thereby FeatureOne and FeatureTwo class can access them. Any better approach in java or kotlin instead of creating the wrapper interface?

Related

Is there a way in java that interface can deliver function with already implemented part of code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is there a way in java that interface can deliver function with already implemented part of code? e.g.
//somewhere in java classes
void function1(){
MyClient client = MyClient.getNew();
if(client.isReal()){
// function2 is an expansion for function1
}
}
//someInterface.java
public interface someInterface{
public void function2(MyClient client);
}
//someClass.java
public class someClass implements someInterface{
#Override
public void function2(MyClient client){
client.send("Hi!");
}
}
Maybe I didn't make it clear. I want to make an interface that delivers a function. No problem. But this function must implement some logic checks. How can I do that?
Yes, you can. You have to write a default method in your interface. Please go through this tutorial to find more details.
Java 8 has introduced the notion of "default methods", so you can definitively implement a method on an interface.

What's better, property of a class or local variable passed through methods? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
If a variable is passed through multiple methods, yet only used in one of the last ones; is it better to declare this variable as a property of a class?
I feel this question is better asked by examples, so:
Example #1
public class eg1 {
private int circleSize;
void a() {
circleSize = ...
b();
}
void b() {
...
c();
}
void c() {
... circleSize ...
}
}
Example #2
public class eg2 {
void a() {
int circleSize = ...
b(circleSize);
}
void b(int circleSize) {
...
c(circleSize);
}
void c(int circleSize) {
... circleSize ...
}
}
Is there a certain way that should be used? Why?
Edit: Seems this question is situational, and opinion based. Some good points have been made for doing it either way.
If you are only going to use that variable in one method, then there is no need for you to make it private, which exposes it to all other methods. By choosing to pass the variable instead of making it private:
You avoid exposing it to other methods
Your code is easier to read, since you won't be looking to see where the variable is changed
You avoid having unnecessary attributes for your class
The answer is very broad, but as a rule you could follow Law of Demeter:
Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
Each unit should only talk to its friends; don't talk to strangers.
Only talk to your immediate friends.
https://en.wikipedia.org/wiki/Law_of_Demeter
So, on this particular case, use arguments. The resultant app will be more testable and easier to understand.

Declaring a constant in java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have come across something rather strange today. A java developer has declared a constant like this:
public final class Constants {
public static final String KEY_COLUMN = AbstractDataRow.standardiseColumnName("key");
... // rest of implementation
}
public abstract class AbstractDataRow implements IRow {
public static String standardiseColumnName(String columnName) {
return columnName.trim().toLowerCase();
... // rest of implementation
}
}
What is strange to me is that KEY_COLUMN calls a method from an abstract class.
Is this good practice?
answering your question.. Not really.
You could just as easy call that string into the method already defined.. then convert that string to lowercase.
send the string to the method instead of the method to the string.

Java/Processing is that possible ? void hello(void funktion) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hey guys i got a fast question, is that possible in Java or Processing ?
void hello(void bye)
{
//
}
With Java 8 you can use a lambda.
With Processing, you can use an anonymous class that implements a functional interface. Something like this:
interface DoFunction{
void do();
}
void hello(DoFunction function){
function.do();
}
void setup(){
hello(new DoFunction(){
void do(){
println("here");
}
});
}

Java: Passing an instance of "this" during instantiation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
myClass1:
public class myClass1
{
public myClass2 myclass2;
public void createsecondclass (String[] args)
{
myclass2 = new myClass2(this);
myclass2.dosomething();
}
myClass2:
public class myClass2
{
public myclass1;
public myClass2(myClass1 myclass1)
{
this.myclass1 = myclass1;
}
public void dosomething()
{
myclass1.another_object_that_could_be_placed_here.dosomething();
}
}
would this not make the code cleaner when trying to access a large amount of objects which all in one way or another are all instantiated under a single class? i ask because i am trying to learn libGDX and in the large assortment of class files that are made to handle each element of the game it just seems easier to pass my application listener down the line since the application listener contains the screen which contains the gameworld which contains the player and so on...
But the problem that i worry about is that by setting it to a variable in the object i am creating a myclass1 that contains a myclass2 that contains a myclass1 and so on. i worry that this might cause memory leaks and since my target is android, memory is a big concern.
if anyone has any thoughts on the subject, directly relevent or not i would appreciate the input. i am after all still learning.
thanks =)

Categories

Resources