Class design - create a object from String data? [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 years ago.
Improve this question
I am working on simple application of Universal Turing Machine. I have a data to create machine from, my questions is not about UTM I would like to illustrate it only on this app. I need to create an object from String data, e.g. this method create one transition function of my UTM:
public static Transition createFromData(String data) {
Transition trans = new Transition();
String[] dataSplitted = data.split("1");
trans.setInputState(new State(dataSplitted[0]));
trans.setInputSymbol(dataSplitted[1]);
trans.setNewState(new State(dataSplitted[2]));
trans.setNewSymbol(dataSplitted[3]);
trans.setMovement(Movement.getByCode(dataSplitted[4]));
return trans;
}
Is it good idea (from class design perspective) to have such methods in Transition class or should I separate them to another / tool classes? What is bets practice for this issue?

Using tools/utils classes always remind me of structured programming ;) but would help if you just had a constructor on the Transition class the would receive the raw data and create a new transition object with that. Since you will be creating a new transition each time you find some raw data the best fit is in the constructor (or maybe in a factory method, but that is another discussion)

what about having this method in a another class for eg: RawData, which will have static factory methods for creating different objects from data.
In your case you can have one method will will return Transition object and then you can have multiple methods and each return a different object depending upon how data is organized / represented in that object.

Related

What's the right way to implement a service in a spring boot? [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 3 years ago.
Improve this question
I am using is SpringBoot + JPA.
Java is object-oriented, so it has to manipulate objects, not data.
The method has to be split enough to split.
When implementing the Service end in a Spring Boot, should I hand over an object to findById?
Or findAddrById, findAgeById... Is this a better way to hand over the prosperties of an object?
findById
The controller requires .getXxx() processing (methods for importing elements of objects) and Null inspection, which results in business logic.
Because the method can be broken down sufficiently, the object must be returned completely even if the value of a single professional is required.
findAddrById, findAgeById
Not object-oriented.
Data is sent and received, not objects.
I'm not really sure what you are asking, but I suspect you are asking what the method signatures your service should return.
Imagine the Entity that is stored in your database are of type User, I would make the service return User objects.
If you need someone's address, you can always just get the User object and call someUser.getAddress().
An example
User someUser = userService.getUserById(1);
String address = someUser.getAddress();
int age = someUser.getAge();
In any case, the answer to this question is more or less dependant on personal preference.

while creating a class in java ,why we use class keyword not type? [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
a class in java (or in other oops languages) represent a data structure. every data structure has a type. so why one use CLASS keyword ? why can't TYPE keyword ?
i.e
we define new data structure in java like below -
public class CustomDataType {
//bla bla
}
why cant it be defined as below -
public type CustomDataType {
//bla bla
}
Please explain.
First answer: It's just a name. When you learn a (natural or programming) language, you have to accept the vocabulary that's in use.
Second answer: In my opinion, choosing the word "class" over "type" was a good decision. Pre-OOP languages like PASCAL or C already had "types", meaning static, open data structures. The OOP idea is that the data structure used to implement the type is just an implementation detail, but the operations you can do with the objects are the important aspect. And inheritance was rarely found / used in pre-OOP programming, but is at the very core of OOP.
So choosing a new term "class" made people aware that there were some really new concepts in the new languages.
And still today there are developers that didn't get it and use classes just like structures from the old days...

Java class convention dealing with arraylist [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
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.

Where to store java stream helper 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 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

Java: Create Instances based on prototype [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 6 years ago.
Improve this question
How can I, and What is the best/proper way (ie, most performant and clearest syntactically) in Java to create object instances based on a prototype object instance, when this will occur repeatedly and in a performance critical code path?
I have thought about cloning via a cloning support library, but is that the best/only way? (These need to be arbitrary objects, btw, not ones that implement Clonable).
To clarify what I mean: I have an existing instance of Class T, which has fields set on it, and I want to pop out many versions of the same object to use separately, with the best performance and syntactic clarity possible.
Thanks.
Create a builder, which receives this class instance:
Person newOne = new PersonBuidler(oldOne).setAge(42)
Implementation of this builder may use apache common BeanUtils for cloning Java Beans or some other utility library for cloning arbitrary class.
See How do I copy an object in Java?

Categories