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 8 years ago.
Improve this question
Like in Scala? Is there any pattern in Java to avoid having all the boilerplate setter/getter without using 3rd party jars? Thanks
update:
my aim is to avoid having too many ghost methods for Dtos, thanks
If all you want is to avoid having to type, use auto generation feature of IDEs like some others mentioned.
If you want to avoid seeing getter/setter in your classes, use a library called lombok which can generate the getters/setters behind the scene
If the above options are not OK for you and you need to set the value only once, you can declare all your fields as public final and have a constructor setting the values.
CAUTION: I am not suggesting this third option as a good practice as it breaks the Javabeans convention. Also it exposes your class' internal structure, but honestly, even with prolific use of getters, you are exposing class fields to the client.
There are a lot of IDE's out there for java that will help you with this problem.
In Eclipse, when you right click on a variable, you can choose Source -> Generate Getters and Setters.
I never used Scala but this should do.
in idea intellij u can create as AlT+ insert and then choose getter+setter
Related
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
Lets say I have class Report and I want to add a functionality printReport(...) and shouldBePrinted(...). Printing it requires GeneralPrinter and LanguageTranslator which are given from outside. Furthermore, I should add members to make the shouldBePrintable method more optimized.
The way I see it there are three ways of doing it:
The simplest is to just add the members and functions to the Report class.
Create PrintableReport which extends Report and adds those members and functions.
Use the decorator pattern to add the needed functionality. (Not sure about that one. Please correct me if this is not the correct way to use a decorator.)
Am I missing some and which is the correct method to do it?
Consider: Separation of concerns
At a HIGH level...
While it's not clear exactly what role Report fills, one might surmise it represents information organized in some fashion.
Rendering is a separate concern. Often you'll want multiple ways to render: Generate PDF, HTML, XML, and/or print (postscript, other...).
So, perhaps you have multiple classes to work with Report, GeneralPrinter, ReportPrinter, ...
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 6 years ago.
Improve this question
I want to make a UML class diagram. By hand, not generated from code. The problem: UML is a very broad specification - I can't map all Java features to how they should look like in the diagram. There are associations, dependencies, aggregations, compositions. They are all well-documented, but not specifically for Java, so:
When should I use which type of connection?
How to handle inner classes (static or not)?
What about static, final, abstract methods/fields? I think I must make it bold/italic/underlined, but how do I map those together?
Abstract classes, Final classes, Enums?
too broad. Try asking for a specific application.
Just nest them.
Use according stereotypes.
Abstract classes are shown with name in italics. Use stereotype for <<final>>. There's a <<enumeration>> meta type.
I use draw.io when I need to make the UML and/or other diagrams. Everything there is manual, you just drag the design and then write the fields,functions, class, etc.
If you don't know when to use connections, classes, etc then go back to learning then try again once you know good enough.
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 6 years ago.
Improve this question
I was using this code to create some sort of a universal changer class:
//constructor method
public Change(Object affdObj, String affdField, float modifier) {
obj = affdObj;
//...
affectedField = affdObj.getClass().getField(affdField);
//...
affectedField.setFloat(obj, affectedField.getFloat(obj) + modifier);
}
But then I was advised to avoid reflection whenever possible since it's very slow. I was suggested to pay attention to interfaces. Unfortunately I can't see how to fit interfaces to my code.
Hence my question: if one needs to access a field which name he doesn't know in advance are there any options other than using reflection?
PS
Thank you for replies, guys.
And since my question is put on hold as primarily opinion-based, I consider this to be the answer to my question, i.e. there is no other way to achieve my goal which is better than mine in every aspect. In other words, I conclude that my approach is OK. Thank you.
First of all, reflection is not slow (anymore) and is widely used (Spring uses it, Hibernate uses it, etc.). So, you use it with confidence if your only concern is speed.
Regarding other ways to do what you want, since you provide the field name as as a string and identify it like that, you cannot do it with interfaces.
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 8 years ago.
Improve this question
my main.java file has a length of about 1000 lines. My code is getting more and more confused, and I would like to "split" it in different parts (e.g. in one file I would have essential stuff like OnCreate, in another file I would have for instance GetHttpRequest).
I already tried to put GetHttpRequest in a different class, but is there no simpler way? (It would take a really long time to adjust the code if I used this method)
You have to use classes and methods, and optionally packages.
This will solve your problem. There's no simpler way than that.
Please do not hard-code your program. There are several patterns on how to code a program, so it is efficient, everybody can easily read and understand it. I think you also have a "GUI", assuming to this, I recommend you to use the MVC pattern. It means Model-View-Controller, so you organize your program in Packages: "model", "view", "controller" and in those packages you put the classes. For instance, you have a simple Calculator. Then you have a class in view thats called "CalculatorView", where your graphical interface is and in controller you have your "CalculatorController" that works out the things like calculations. (You call the controller from the view) and you do not need model at all.
I hope that helps you. But you will have to rewrite all your code...
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 7 years ago.
Improve this question
When writing an application(in java) that you intend to open source, is it generally a good idea to have a lot of getters & setters and make variables private? More specifically, if that were to be an android application, would the answer to the question above still hold?
Edit: If you guys could give me a concrete reason as to why its better, that would be awesome.
If you are discussing getters & setters in ANDROID, check what android documentation tell:-
Avoid Internal Getters/Setters
On Android, this is a bad idea. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.
if all the private variables are expected to be accessed from out side then yes.
Suppose you have certain flags those aren't going to be used from outside then no need of getters/setters for those.
Also See
why-use-getters-and-setters
Lets put it this way, you don't feel any need of having getters/setters and you plan to make your properties non-private. It doesn't appear to be any problem with this approach. But you must ask few questions to yourself.
Are you having any property whose value should undergo some checking before assignment? (Need for a setter)
Do you have any mutable property which you don't want to expose as it is? (Need for a getter)
Now, if you think your few properties need getters/setters, but not all. Then I would say create getter/setter for all of them for the sake of consistency. :)
Further see, Effective Java 2nd Edition,
Item 13: Minimize the accessibility of classes and members
Item 14: In public classes, use accessor methods, not public fields
Item 15: Minimize mutability
Item 38: Check parameters for validity
Item 56: Adhere to generally accepted naming conventions