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
I have a public class called ProcessOrder. Within this class I have a private method called Login(), which logs into another company's system. I want to be able to initiated as many ProcessOrder objects as I want, but the Login() process to be synchronized, which means all the ProcessOrder objects need to wait inline to process Login().
What's the best practice to design my code?
Thanks,
You could use a synchronized block, specifying the object on which to synchronize on (here, the ProcessOrder class):
public class ProcessOrder {
private void login() {
synchronized(ProcessOrder.class) {
// one at a time, please
}
}
}
Related
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 12 months ago.
Improve this question
I have a class with a method which somtimes uses notifyAll().
class A{
public void method(*some args*) //sometime i call for notifyAll()
}
I want to build a test to see if class A has called for notifyAll()
#Test
public void test(){
A.method
boolean isNotified
}
how can I do it?
Couple of ways
Verify it has been called by checking the effects of potential call - eg some object did change its state as a result and it is verifiable
Spy on instance that is supposed to have method called - you can then check if the method have been called directly
Use observable pattern and add 'observe' behavior to the test case.
probably much more.
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 3 years ago.
Improve this question
Currently, I have an interface which looks like this
public interface SortInfoDialogListener {
void onSortInfoSelected(SortInfo sortInfo);
void onSortInfoDialogDismiss(DialogInterface dialog);
}
Since most of the implementors are not interested in onSortInfoDialogDismiss, I was wondering, should I make it as an empty defender method?
public interface SortInfoDialogListener {
void onSortInfoSelected(SortInfo sortInfo);
default void onSortInfoDialogDismiss(DialogInterface dialog) {
}
}
I was wondering, what else factors I should consider, before making an interface function having an empty defender method?
I try to find answers in Effective Java 3 Item 20 and Item 21. But, still can't derive a concrete conclusion yet.
SOLID principles states to segregate interfaces as per the requirement. Currently i could see onSortInfoDialogDismiss method is not required for many implementors.
Try to extract onSortInfoDialogDismiss() to another interface, so it could be implemented on demand.
keep current interface with onSortInfoSelected() alone, as below.
public interface SortInfoDialogListener {
void onSortInfoSelected(SortInfo sortInfo);
}
Hope this helps.
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
My question is in regards to better convention for class design. I am currently working on a project for school where I am to use a class to create an object for a unit conversion calculator. I want to store these objects into an arraylist.
My question is, does it matter if I create and .add to the arraylist from the main method, or is it better to think about using the conversion class to deal with the array?
The main method is a starting point for execution. That's a place only for processing application arguments (args) and creating an instance that represents the whole application*. Normally, the class that contains main should be stateless.
A good example - a Spring Boot initialiser:
public class Runner {
public static void main(String... args) {
SpringApplication.run(Runner.class, args); // all the magic is in there
}
}
*
Sometimes, to adhere to the single responsibility principle, we direct args handling and initialization of the principal object to different classes.
It is better to create a separate class. It is very good practice and help you a lot to maintain when your project will grow in future.
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 1 year ago.
Improve this question
Where is preferred place to store functions for improve readibility of streams?
For example:
private BinaryOperator<Cash> sumPayments() {
return (payment1, payment2) -> payment1.sum(payment2);
}
I'm not sure if it should be in service class where it's used, but maybe it will be in many places so whats about some extra static Utils? or in entity?
Where possible avoid unnecessary helper methods.
In this particular example, the helper method can easily be replaced with a method reference, which is (arguably more) readable:
Cash total = subtotals.stream().reduce(Cash.ZERO, Cash::sum);
In other cases, where you actually do need a helper: think about where you would search for such a helper method in a year, when you have to adapt your now-barely-familiar code to new requirements.
Is it more entity related? Put it in the entity class
Is it more business logic related? Put it into the business logic
Is it generally useful? Put it into a utility class
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 8 years ago.
Improve this question
If I have a variable defined in a method,can I get its Class Object using reflection
public void check(List<?> String> list){
Map<String,String> map // do something
}
Can i Obtain the Class Object to perform reflection for list and map variables,both are local variables
No, reflection does not expose local variables. Byte code analysis may help, but I don't know what you are trying to do.