Is this method of initialization recommended? - java

So when I was coding my own project, a problem came up with initializing an object. My class has so many attributes that needs to be initiated BUT not always all of them, which . That's when I came up with this idea (but I'm not sure if it's used before anywhere):
public class MyClass {
public static final int DEFAULT1 = 1;
public static final int DEFAULT2 = 2;
public static final int DEFAULT3 = 3;
private int attr1;
private int attr2;
private int attr3;
private MyClass(){
attr1 = DEFAULT1;
attr2 = DEFAULT2;
attr3 = DEFAULT3;
}
public static MyClass create(){
return new MyClass();
}
public MyClass setAttr1(int attr1){
this.attr1 = attr1;
return this;
}
public MyClass setAttr2(int attr2){
this.attr2 = attr2;
return this;
}
public MyClass setAttr3(int attr3){
this.attr3 = attr3;
return this;
}
}
When I create a new MyClass instance I would do:
MyClass obj = MyClass.create().setAttr1(3).setAttr3(1);
It worked for me (kind of because I tested it but I haven't decided to use it in my project). My question is, is this legit? Would it create any kinds of memory/execution problems?

Your use case is a perfect example of Builder design pattern. Read more about here and oracle doc about builder. Also read why Builder pattern has an advantage over other creation pattern by Java legend Joshua which is discuss in his book, best practice of java
here.
Your converted class using Builder design pattern will look like below. Note that i assumed attr1 optional and attr2 and attr3 as mandatory.
class MyClass {
public static final int DEFAULT1 = 1;
public static final int DEFAULT2 = 2;
public static final int DEFAULT3 = 3;
// assume attr1 is optional
private int attr1;
// assume attr2,3 is mandatory
private int attr2;
private int attr3;
public MyClass(MyClassBuilder myClassBuilder) {
this.attr1 = myClassBuilder.attr1;
this.attr2 = myClassBuilder.attr2;
this.attr3 = myClassBuilder.attr3;
}
//Builder Class
public static class MyClassBuilder {
// required parameters
private int attr2;
private int attr3;
// optional parameters
private int attr1;
public MyClassBuilder(int attr2, int attr3){
this.attr2 = attr2;
this.attr3 = attr3;
}
public MyClassBuilder setAttr1(int attr1) {
this.attr1 = attr1;
return this;
}
public MyClass build(){
return new MyClass(this);
}
}
}
Now After that using below code, you can create the objects (with/without) optional param.
public class MyClassTest {
public static void main(String[] args) {
MyClass myClass = new MyClass.MyClassBuilder(10,20).build();
// without optional param
System.out.println("without optional param");
System.out.println("attr1 "+ myClass.getAttr1());
System.out.println("attr2 "+ myClass.getAttr2());
System.out.println("attr3 "+ myClass.getAttr3());
// with optional param
System.out.println("with optional param");
MyClass myClassWithAllParam = new MyClass.MyClassBuilder(10,20).setAttr1(5).build();
System.out.println("attr1 "+ myClassWithAllParam.getAttr1());
System.out.println("attr2 "+ myClassWithAllParam.getAttr2());
System.out.println("attr3 "+ myClassWithAllParam.getAttr3());
}
}
Output of which is below :-
without optional param
attr1 0
attr2 10
attr3 20
with optional param
attr1 5
attr2 10
attr3 20

Another name for it is Fluent Interface.
You just need to use with instead of set for method names. The set naming is kind of reserved for JavaBeans convention.

Related

How can I print an arraylist that is in one class, by a grouping from another class?

I'm trying to print an arraylist that is in one class, based on one of the parameters from another class. Is this possible?
import java.util.ArrayList;
public class TVShow {
private String title;
private String summary;
private String releaseDate;
private ArrayList<Episode> episodeList;
public TVShow(String title, String summary, String releaseDate) {
this.title = title;
this.summary = summary;
this.releaseDate = releaseDate;
this.episodeList = new ArrayList<>();
}
public void addEpisode(Episode episode) {
episodeList.add(episode);
}
public printEpisodesInSeason(int seasonNr) {
// How can I make this method access the other class and
// print the episodeList by season number?
for (Episode episode : episodeList) {
return System.out.println(episode.);
}
}
}
public class Episode {
private int episodeNr;
private int seasonNr;
private String eTitle;
private int runTime;
public Episode(int episodeNr, int seasonNr, String eTitle, int runTime) {
this.episodeNr = episodeNr;
this.seasonNr = seasonNr;
this.eTitle = eTitle;
this.runTime = runTime;
}
}
EDIT: I think I misinterpreted the question. You want to only print the episodes from a specific season. This can be done by applying the filter function on the episodeList as follows:
for (Episode episode : episodeList.stream().filter(episode -> episode.getSeasonNr() == seasonNr).collect(Collectors.toList()))
{ ... }
This is ofcourse assuming you apply the getter setter pattern as described below before I edited the answer.
The filter function takes an anonymous function and applies it to all members of a collection. This way, only the episodes which have a season number that is supplied by the user are returned. Then, the foreach loop iterates over the resulting collection.
You could either make the members of Episode public by defining:
public class Episode {
public int episodeNr;
public int seasonNr;
public String eTitle;
public int runTime;
public Episode(int episodeNr, int seasonNr, String eTitle, int runTime) {
this.episodeNr = episodeNr;
this.seasonNr = seasonNr;
this.eTitle = eTitle;
this.runTime = runTime;
}
}
But this is seen as bad practice. The better way to do it is by defining methods in your Episode class to return the value of the class' fields like for example:
public class Episode {
public int episodeNr;
public int seasonNr;
public String eTitle;
public int runTime;
public Episode(int episodeNr, int seasonNr, String eTitle, int runTime) {
this.episodeNr = episodeNr;
this.seasonNr = seasonNr;
this.eTitle = eTitle;
this.runTime = runTime;
}
public String getTitle() {
return this.eTitle;
}
}
This practice is called getters and setters and it positively impacts the encapsulation of the code. You could then obtain the value of the Episode's members by calling, for example episode.getTitle().

How to assign a value to a final static variable from a child inside java?

Suppose we have a hierarchy like this:
class Parent {
public static final String BASE_NAME = "parent";
public static final String X_URL = BASE_NAME + "/x";
public static final String Y_URL = BASE_NAME + "/y";
}
class ChildA extends Parent {
public static final String BASE_NAME = "childA";
public static final String X_URL = BASE_NAME + "/x";
public static final String Y_URL = BASE_NAME + "/y";
#RequestMapping(value= X_URL)
public String renderXPage(){...}
}
as you can see the X_URL and Y_URL are repeating both in parent and child, if the child can feed in its own BASE_NAME to the parent, we can eliminate redundant repetition of those constants and then we can use them for the annotation value. How to do this in Java ?
There is no way to reliable achieve this.
You can have #RequestMapping annotation on class and put base part of URL there.
Example:
#RequestMapping("ChildA")
class ChildA {
#RequestMapping(value= "x")
public String renderXPage(){...}
}
renderXPage will handle "ChildA/x" URL.
Using the solution of #talex I came up with this neat solution:
public interface CommonRelativeUrls {
String X_URL = "/x";
String Y_URL = "/y";
}
public interface Entities {
String CLASS_A = "class-a";
String CLASS_B = "class-b";
...
}
#RequestMapping(value = Entities.CLASS_A)
public class ClassA implements CommonRelativeUrls {
#RequestMapping(value= X_URL)
public String renderXPage(){...}
}

Good way to organize group of constants in enum-like fashion

I'm trying to find a good way to orginize a group of constant values that are used simply for immutable data.
Here is what I'm currently attempting:
public class FishType {
//PredatorFishType extends FishType
public static final PredatorFishType SHARK = new PredatorFishType(5, 20, "Shark");
public static final FishType CAT_FISH = new FishType("Cat Fish");
private String name;
private FishType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
I use reflection to gather the final values into a collection aswell. I used to utilize enum but was forced to think of a new way to do this when different types of fish came into play such as the predator which contains other data such as food and so on. These constants are only used for data displaying purposes and have no reason to be mutated.
If there is some way to have multiple enum types within the same enum (If that makes any sense at all), that'd be great.
Thanks for reading.
You can either use constructor overloading or a combination of overloading and a wrapper class. If you know for certain that this data is immutable and will always be that way, I don't see anything wrong with sticking to enums for it. For the sake of putting it into one class, I've included the enums in the EnumTester class, but you may not want to do that.
Here's an example that prints "Cat Fish 5 20 Shark" and "Cow Fish" when run, using nothing but enums and a wrapper class. You could put accessors wherever you need them, depending on what you actually want to do with the information - I'm trying to demonstrate how to compose the two enums, not how to use them.
package enums;
public class EnumTester
{
public enum MainType {
CAT_FISH("Cat Fish"), DOG_FISH("Dog Fish"), COW_FISH("Cow Fish"); //everything has a name...
private String name;
private MainType(String name){
this.name = name;
}
public String getTypeDetails(){
return name;
}
}
public enum SubType {
PREDATOR(5, 20, "Shark"), PREY(), MANATEE(); //but not everything has any additional information
private boolean isFullSubType;
private int val1;
private int val2;
private String subName;
private SubType(int val1, int val2, String subName){
this.isFullSubType = true;
this.val1 = val1;
this.val2 = val2;
this.subName = subName;
}
private SubType(){
this.isFullSubType = false;
this.val1 = -1;
this.val2 = -1;
this.subName = "none";
}
public String getSubTypeDetails()
{
if( isFullSubType ) {
return val1 + " " + val2 + " " + subName;
}
else {
return "";
}
}
}
private MainType mainType;
private SubType subType;
public EnumTester(MainType mainType, SubType subType)
{
this.mainType = mainType;
this.subType = subType;
}
public static void main(String[] args)
{
EnumTester kittyShark = new EnumTester(MainType.CAT_FISH, SubType.PREDATOR);
System.out.println(kittyShark.printDetails());
EnumTester cowManatee = new EnumTester(MainType.COW_FISH, SubType.MANATEE);
System.out.println(cowManatee.printDetails());
}
public String printDetails(){
return mainType.getTypeDetails()+" "+subType.getSubTypeDetails();
}
}
I typically follow a similar pattern to what you've done above. I might make the class FishTypes to be the collector, just to keep the FishType interface a bit cleaner. You can also invent some syntactic sugar to help you collect registered FishTypes:
public static final Set<FishType> registeredFish = new HashSet<>();
public static final PredatorFishType SHARK = register(new PredatorFishType(5, 20, "Shark"));
public static final FishType CAT_FISH = register(new FishType("Cat Fish"));
public static <T extends FishType> T register(T fishType) {
registeredFish.add(fishType);
return fishType;
}

How do I automacially assign a variable in constructor while creating objects

Let's say we just have a simple object Person with int ID which identifies it. How can I give new ID value (+1) for each new instance of Person but in constructor of that class Person? (I use no DB for this)
Use a static AtomicInteger:
final class Foo {
private static final AtomicInteger seed = new AtomicInteger();
private final int id;
public Foo() {
this.id = seed.incrementAndGet();
}
}
See here for more information: https://stackoverflow.com/a/4818753/17713
You should use something like
public class YourClass {
private static int generalIdCount = 0;
private int id;
public YourClass() {
this.id = generalIdCount;
generalIdCount++;
}
}
Use static counting field which is shared accros all instances of Person:
class Person {
private static int nextId = 1;
private final int id;
Person() {
id = nextId++;
}
}
You could create a static variable for the current counter value, and assign that to the ID when created...
public class Person {
// same across all instances of this class
static int currentCounter = 0;
// only for this instance
int personId;
public Person(){
personId = currentCounter;
currentCounter++;
}
}
use static variables; static variables aren't bound to class instances, rather to classes directly.
Example (in C#):
public class Person{
public static int Increment = 1;
public int ID;
public Person(){
this.ID = Increment;
Increment++;
}
}
This way all class instances will have unique ID-s (incremented by 1).
EDIT: This approach isn't thread-safe, see #Mudu's answer.

Builder Pattern in Effective Java

I have recently started to read Effective Java by Joshua Bloch. I found the idea of the Builder pattern [Item 2 in the book] really interesting. I tried to implement it in my project but there were compilation errors. Following is in essence what I was trying to do:
The class with multiple attributes and its builder class:
public class NutritionalFacts {
private int sodium;
private int fat;
private int carbo;
public class Builder {
private int sodium;
private int fat;
private int carbo;
public Builder(int s) {
this.sodium = s;
}
public Builder fat(int f) {
this.fat = f;
return this;
}
public Builder carbo(int c) {
this.carbo = c;
return this;
}
public NutritionalFacts build() {
return new NutritionalFacts(this);
}
}
private NutritionalFacts(Builder b) {
this.sodium = b.sodium;
this.fat = b.fat;
this.carbo = b.carbo;
}
}
Class where I try to use the above class:
public class Main {
public static void main(String args[]) {
NutritionalFacts n =
new NutritionalFacts.Builder(10).carbo(23).fat(1).build();
}
}
I am getting the following compiler error:
an enclosing instance that contains
effectivejava.BuilderPattern.NutritionalFacts.Builder
is required
NutritionalFacts n = new
NutritionalFacts.Builder(10).carbo(23).fat(1).build();
I do not understand what the message means. Please explain. The above code is similar to the example suggested by Bloch in his book.
Make the builder a static class. Then it will work. If it is non-static, it would require an instance of its owning class - and the point is not to have an instance of it, and even to forbid making instances without the builder.
public class NutritionFacts {
public static class Builder {
}
}
Reference: Nested classes
You should make the Builder class as static and also you should make the fields final and have getters to get those values. Don't provide setters to those values. In this way your class will be perfectly immutable.
public class NutritionalFacts {
private final int sodium;
private final int fat;
private final int carbo;
public int getSodium(){
return sodium;
}
public int getFat(){
return fat;
}
public int getCarbo(){
return carbo;
}
public static class Builder {
private int sodium;
private int fat;
private int carbo;
public Builder sodium(int s) {
this.sodium = s;
return this;
}
public Builder fat(int f) {
this.fat = f;
return this;
}
public Builder carbo(int c) {
this.carbo = c;
return this;
}
public NutritionalFacts build() {
return new NutritionalFacts(this);
}
}
private NutritionalFacts(Builder b) {
this.sodium = b.sodium;
this.fat = b.fat;
this.carbo = b.carbo;
}
}
And now you can set the properties as follows:
NutritionalFacts n = new NutritionalFacts.Builder().sodium(10).carbo(15).
fat(5).build();
To generate an inner builder in Intellij IDEA, check out this plugin: https://github.com/analytically/innerbuilder
You are trying access a non-static class in a static way. Change Builder to static class Builder and it should work.
The example usage you give fails because there is no instance of Builder present. A static class for all practical purposes is always instantiated. If you don't make it static, you'd need to say:
Widget = new Widget.Builder(10).setparm1(1).setparm2(3).build();
Because you would need to construct a new Builder every time.
You need to declare the Builder inner class as static.
Consult some documentation for both non-static inner classes and static inner classes.
Basically the non-static inner classes instances cannot exist without attached outer class instance.
Once you've got an idea, in practice, you may find lombok's #Builder much more convenient.
#Builder lets you automatically produce the code required to have your class be instantiable with code such as:
Person.builder()
.name("Adam Savage")
.city("San Francisco")
.job("Mythbusters")
.job("Unchained Reaction")
.build();
Official documentation: https://www.projectlombok.org/features/Builder
This mean that you cant create enclose type. This mean that first you have to cerate a instance of "parent" class and then from this instance you can create nested class instances.
NutritionalFacts n = new NutritionalFacts()
Builder b = new n.Builder(10).carbo(23).fat(1).build();
Nested Classes
The Builder class should be static. I don't have time right now to actually test the code beyond that, but if it doesn't work let me know and I'll take another look.
I personally prefer to use the other approach, when you have 2 different classes. So you don't need any static class. This is basically to avoid write Class.Builder when you has to create a new instance.
public class Person {
private String attr1;
private String attr2;
private String attr3;
// package access
Person(PersonBuilder builder) {
this.attr1 = builder.getAttr1();
// ...
}
// ...
// getters and setters
}
public class PersonBuilder (
private String attr1;
private String attr2;
private String attr3;
// constructor with required attribute
public PersonBuilder(String attr1) {
this.attr1 = attr1;
}
public PersonBuilder setAttr2(String attr2) {
this.attr2 = attr2;
return this;
}
public PersonBuilder setAttr3(String attr3) {
this.attr3 = attr3;
return this;
}
public Person build() {
return new Person(this);
}
// ....
}
So, you can use your builder like this:
Person person = new PersonBuilder("attr1")
.setAttr2("attr2")
.build();
As many already stated here you need to make the class static.
Just small addition - if you want, there is a bit different way without static one.
Consider this. Implementing a builder by declaring something like withProperty(value) type setters inside the class and make them return a reference to itself. In this approach, you have a single and an elegant class which is a thread safe and concise.
Consider this:
public class DataObject {
private String first;
private String second;
private String third;
public String getFirst(){
return first;
}
public void setFirst(String first){
this.first = first;
}
...
public DataObject withFirst(String first){
this.first = first;
return this;
}
public DataObject withSecond(String second){
this.second = second;
return this;
}
public DataObject withThird(String third){
this.third = third;
return this;
}
}
DataObject dataObject = new DataObject()
.withFirst("first data")
.withSecond("second data")
.withThird("third data");
Check it out for more Java Builder examples.
You need to change Builder class to static class Builder. Then it will work fine.
The other solutions double the memory allocation to instantiate the object. The following solution does not have that problem.
public class NutritionalFacts{
private int sodium;
private int fat;
private int carbo;
private NutritionalFacts(){}
public int getSodium(){ return sodium;}
public int getFat(){ return fat;}
public int getCarbo(){ return carbo;}
public static class Builder{
private NutritionalFacts nutrionalFacts;
public Builder(){
nutrionalFacts = new NutritionalFacts();
}
public Builder sodium(int s){
nutrionalFacts.sodium = s;
return this;
}
public Builder fat(int f){
nutrionalFacts.fat = f;
return this;
}
public Builder carbo(int c){
nutrionalFacts.carbo = c;
return this;
}
public NutritionalFacts build(){
return nutrionalFacts;
}
}
}

Categories