Java: when to use generics method and when explicit method
Lets suppose we have 10 types of Car:Bmw, Renault etc. And we have Repo. So there are two ways for developing API of Repo:
The first way:
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
The second way:
class Repo{
public Bmw getBmw(){..}
public Reno getRenault(){..}
...
}
Firstly I thought that I should follow the first way as it lets write less code and -> it is better for supporting. And besides, I thought that if I have 20 types of car the first way is obvious advantage (less code).
However, as the number of car is growing - you start to forget what car you have. When you follow the second way - you have clear API of the repo and the types.
So,could anyone explain when to use which method?
java generics
add a comment |
Lets suppose we have 10 types of Car:Bmw, Renault etc. And we have Repo. So there are two ways for developing API of Repo:
The first way:
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
The second way:
class Repo{
public Bmw getBmw(){..}
public Reno getRenault(){..}
...
}
Firstly I thought that I should follow the first way as it lets write less code and -> it is better for supporting. And besides, I thought that if I have 20 types of car the first way is obvious advantage (less code).
However, as the number of car is growing - you start to forget what car you have. When you follow the second way - you have clear API of the repo and the types.
So,could anyone explain when to use which method?
java generics
2
How aboutpublic Car getCar()
? That would encourage callers to be agnostic of the car type, making the code more extensible to future car types.
– Andy Thomas
Jun 29 '16 at 13:31
This is not really a good SO question as it doesn't really contain a specifically answerable programming question, at least, as currently stated. Also it's spelled 'Renault'.
– pvg
Jun 29 '16 at 13:34
what is a Reno?
– njzk2
Jun 29 '16 at 13:44
@njzk2 en.wikipedia.org/wiki/Suzuki_Reno, perhaps? (More likely a misspelling of Renault, though).
– Andy Turner
Jun 29 '16 at 13:45
I don't think the make of a car is sufficient to justify polymorphism. You can probably have aCar
class, with an enum for the make. And in your repo, a list or map of cars that you can search to return those of a certain brand, model, year, color, ...
– njzk2
Jun 29 '16 at 13:46
add a comment |
Lets suppose we have 10 types of Car:Bmw, Renault etc. And we have Repo. So there are two ways for developing API of Repo:
The first way:
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
The second way:
class Repo{
public Bmw getBmw(){..}
public Reno getRenault(){..}
...
}
Firstly I thought that I should follow the first way as it lets write less code and -> it is better for supporting. And besides, I thought that if I have 20 types of car the first way is obvious advantage (less code).
However, as the number of car is growing - you start to forget what car you have. When you follow the second way - you have clear API of the repo and the types.
So,could anyone explain when to use which method?
java generics
Lets suppose we have 10 types of Car:Bmw, Renault etc. And we have Repo. So there are two ways for developing API of Repo:
The first way:
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
The second way:
class Repo{
public Bmw getBmw(){..}
public Reno getRenault(){..}
...
}
Firstly I thought that I should follow the first way as it lets write less code and -> it is better for supporting. And besides, I thought that if I have 20 types of car the first way is obvious advantage (less code).
However, as the number of car is growing - you start to forget what car you have. When you follow the second way - you have clear API of the repo and the types.
So,could anyone explain when to use which method?
java generics
java generics
edited Nov 19 '18 at 13:15
Pavel_K
asked Jun 29 '16 at 13:29
Pavel_KPavel_K
2,70142070
2,70142070
2
How aboutpublic Car getCar()
? That would encourage callers to be agnostic of the car type, making the code more extensible to future car types.
– Andy Thomas
Jun 29 '16 at 13:31
This is not really a good SO question as it doesn't really contain a specifically answerable programming question, at least, as currently stated. Also it's spelled 'Renault'.
– pvg
Jun 29 '16 at 13:34
what is a Reno?
– njzk2
Jun 29 '16 at 13:44
@njzk2 en.wikipedia.org/wiki/Suzuki_Reno, perhaps? (More likely a misspelling of Renault, though).
– Andy Turner
Jun 29 '16 at 13:45
I don't think the make of a car is sufficient to justify polymorphism. You can probably have aCar
class, with an enum for the make. And in your repo, a list or map of cars that you can search to return those of a certain brand, model, year, color, ...
– njzk2
Jun 29 '16 at 13:46
add a comment |
2
How aboutpublic Car getCar()
? That would encourage callers to be agnostic of the car type, making the code more extensible to future car types.
– Andy Thomas
Jun 29 '16 at 13:31
This is not really a good SO question as it doesn't really contain a specifically answerable programming question, at least, as currently stated. Also it's spelled 'Renault'.
– pvg
Jun 29 '16 at 13:34
what is a Reno?
– njzk2
Jun 29 '16 at 13:44
@njzk2 en.wikipedia.org/wiki/Suzuki_Reno, perhaps? (More likely a misspelling of Renault, though).
– Andy Turner
Jun 29 '16 at 13:45
I don't think the make of a car is sufficient to justify polymorphism. You can probably have aCar
class, with an enum for the make. And in your repo, a list or map of cars that you can search to return those of a certain brand, model, year, color, ...
– njzk2
Jun 29 '16 at 13:46
2
2
How about
public Car getCar()
? That would encourage callers to be agnostic of the car type, making the code more extensible to future car types.– Andy Thomas
Jun 29 '16 at 13:31
How about
public Car getCar()
? That would encourage callers to be agnostic of the car type, making the code more extensible to future car types.– Andy Thomas
Jun 29 '16 at 13:31
This is not really a good SO question as it doesn't really contain a specifically answerable programming question, at least, as currently stated. Also it's spelled 'Renault'.
– pvg
Jun 29 '16 at 13:34
This is not really a good SO question as it doesn't really contain a specifically answerable programming question, at least, as currently stated. Also it's spelled 'Renault'.
– pvg
Jun 29 '16 at 13:34
what is a Reno?
– njzk2
Jun 29 '16 at 13:44
what is a Reno?
– njzk2
Jun 29 '16 at 13:44
@njzk2 en.wikipedia.org/wiki/Suzuki_Reno, perhaps? (More likely a misspelling of Renault, though).
– Andy Turner
Jun 29 '16 at 13:45
@njzk2 en.wikipedia.org/wiki/Suzuki_Reno, perhaps? (More likely a misspelling of Renault, though).
– Andy Turner
Jun 29 '16 at 13:45
I don't think the make of a car is sufficient to justify polymorphism. You can probably have a
Car
class, with an enum for the make. And in your repo, a list or map of cars that you can search to return those of a certain brand, model, year, color, ...– njzk2
Jun 29 '16 at 13:46
I don't think the make of a car is sufficient to justify polymorphism. You can probably have a
Car
class, with an enum for the make. And in your repo, a list or map of cars that you can search to return those of a certain brand, model, year, color, ...– njzk2
Jun 29 '16 at 13:46
add a comment |
3 Answers
3
active
oldest
votes
The first way will produce less duplicate code. But if you want to model the types of cars, you could use an enum
enum Cars {
BMW(Bmw.class),
RENO(Reno.class)
;
Class<? extends Car> type;
Cars(Class<? extends Car> type){
this.type = type;
}
Class<? extends Car> getType() {
return type;
}
}
And the access the car using this enum
public <T extends Car> T getCarByType(Cars car){
Class<T> type = car.getType();
...
}
add a comment |
First if your cars aren't so different you may use only one class Car that will have a type property which will allow you to make difference between cars type, however if you wan't to continue with your solution I think you may use both of the ways, you can create a generic Repo which will have all code used to get Data then you can make on top of it a specific layer that will expose methods to retrieve each car by it's type
Generic Repo
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
RepoFacade
class RepoFacade{
public Bmw getBmw(){
Repo<Bmw> = new Repo<>();
return repo.getCarByType(BMW.class);
}
public Reno getReno(){..}
}
add a comment |
I would go with something simpler. No polymorphism based on brand, because it is just a label, it does not add any behavior:
public class Car {
public enum Make { BMW, ... };
public Make make;
public Color color;
public int year;
// Cars have other properties, I suppose
}
public class Repo {
private List<Car> cars = new ArrayList<Car>;
// This could also return a list of all cars of that model,
// because there is no reason to have exactly one of each make.
@Nullable
public Car findByMake(Car.Make make) {
for (Car car : cars) {
if (car.make == make) {
return car;
}
}
return null;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38101069%2fjava-when-to-use-generics-method-and-when-explicit-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The first way will produce less duplicate code. But if you want to model the types of cars, you could use an enum
enum Cars {
BMW(Bmw.class),
RENO(Reno.class)
;
Class<? extends Car> type;
Cars(Class<? extends Car> type){
this.type = type;
}
Class<? extends Car> getType() {
return type;
}
}
And the access the car using this enum
public <T extends Car> T getCarByType(Cars car){
Class<T> type = car.getType();
...
}
add a comment |
The first way will produce less duplicate code. But if you want to model the types of cars, you could use an enum
enum Cars {
BMW(Bmw.class),
RENO(Reno.class)
;
Class<? extends Car> type;
Cars(Class<? extends Car> type){
this.type = type;
}
Class<? extends Car> getType() {
return type;
}
}
And the access the car using this enum
public <T extends Car> T getCarByType(Cars car){
Class<T> type = car.getType();
...
}
add a comment |
The first way will produce less duplicate code. But if you want to model the types of cars, you could use an enum
enum Cars {
BMW(Bmw.class),
RENO(Reno.class)
;
Class<? extends Car> type;
Cars(Class<? extends Car> type){
this.type = type;
}
Class<? extends Car> getType() {
return type;
}
}
And the access the car using this enum
public <T extends Car> T getCarByType(Cars car){
Class<T> type = car.getType();
...
}
The first way will produce less duplicate code. But if you want to model the types of cars, you could use an enum
enum Cars {
BMW(Bmw.class),
RENO(Reno.class)
;
Class<? extends Car> type;
Cars(Class<? extends Car> type){
this.type = type;
}
Class<? extends Car> getType() {
return type;
}
}
And the access the car using this enum
public <T extends Car> T getCarByType(Cars car){
Class<T> type = car.getType();
...
}
answered Jun 29 '16 at 13:35
Gerald MückeGerald Mücke
6,84212547
6,84212547
add a comment |
add a comment |
First if your cars aren't so different you may use only one class Car that will have a type property which will allow you to make difference between cars type, however if you wan't to continue with your solution I think you may use both of the ways, you can create a generic Repo which will have all code used to get Data then you can make on top of it a specific layer that will expose methods to retrieve each car by it's type
Generic Repo
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
RepoFacade
class RepoFacade{
public Bmw getBmw(){
Repo<Bmw> = new Repo<>();
return repo.getCarByType(BMW.class);
}
public Reno getReno(){..}
}
add a comment |
First if your cars aren't so different you may use only one class Car that will have a type property which will allow you to make difference between cars type, however if you wan't to continue with your solution I think you may use both of the ways, you can create a generic Repo which will have all code used to get Data then you can make on top of it a specific layer that will expose methods to retrieve each car by it's type
Generic Repo
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
RepoFacade
class RepoFacade{
public Bmw getBmw(){
Repo<Bmw> = new Repo<>();
return repo.getCarByType(BMW.class);
}
public Reno getReno(){..}
}
add a comment |
First if your cars aren't so different you may use only one class Car that will have a type property which will allow you to make difference between cars type, however if you wan't to continue with your solution I think you may use both of the ways, you can create a generic Repo which will have all code used to get Data then you can make on top of it a specific layer that will expose methods to retrieve each car by it's type
Generic Repo
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
RepoFacade
class RepoFacade{
public Bmw getBmw(){
Repo<Bmw> = new Repo<>();
return repo.getCarByType(BMW.class);
}
public Reno getReno(){..}
}
First if your cars aren't so different you may use only one class Car that will have a type property which will allow you to make difference between cars type, however if you wan't to continue with your solution I think you may use both of the ways, you can create a generic Repo which will have all code used to get Data then you can make on top of it a specific layer that will expose methods to retrieve each car by it's type
Generic Repo
class Repo{
public <T extends Car> T getCarByType(Class<T> clazz){..}
}
RepoFacade
class RepoFacade{
public Bmw getBmw(){
Repo<Bmw> = new Repo<>();
return repo.getCarByType(BMW.class);
}
public Reno getReno(){..}
}
edited Jun 29 '16 at 13:42
answered Jun 29 '16 at 13:36
elkorchi anaselkorchi anas
1,654921
1,654921
add a comment |
add a comment |
I would go with something simpler. No polymorphism based on brand, because it is just a label, it does not add any behavior:
public class Car {
public enum Make { BMW, ... };
public Make make;
public Color color;
public int year;
// Cars have other properties, I suppose
}
public class Repo {
private List<Car> cars = new ArrayList<Car>;
// This could also return a list of all cars of that model,
// because there is no reason to have exactly one of each make.
@Nullable
public Car findByMake(Car.Make make) {
for (Car car : cars) {
if (car.make == make) {
return car;
}
}
return null;
}
}
add a comment |
I would go with something simpler. No polymorphism based on brand, because it is just a label, it does not add any behavior:
public class Car {
public enum Make { BMW, ... };
public Make make;
public Color color;
public int year;
// Cars have other properties, I suppose
}
public class Repo {
private List<Car> cars = new ArrayList<Car>;
// This could also return a list of all cars of that model,
// because there is no reason to have exactly one of each make.
@Nullable
public Car findByMake(Car.Make make) {
for (Car car : cars) {
if (car.make == make) {
return car;
}
}
return null;
}
}
add a comment |
I would go with something simpler. No polymorphism based on brand, because it is just a label, it does not add any behavior:
public class Car {
public enum Make { BMW, ... };
public Make make;
public Color color;
public int year;
// Cars have other properties, I suppose
}
public class Repo {
private List<Car> cars = new ArrayList<Car>;
// This could also return a list of all cars of that model,
// because there is no reason to have exactly one of each make.
@Nullable
public Car findByMake(Car.Make make) {
for (Car car : cars) {
if (car.make == make) {
return car;
}
}
return null;
}
}
I would go with something simpler. No polymorphism based on brand, because it is just a label, it does not add any behavior:
public class Car {
public enum Make { BMW, ... };
public Make make;
public Color color;
public int year;
// Cars have other properties, I suppose
}
public class Repo {
private List<Car> cars = new ArrayList<Car>;
// This could also return a list of all cars of that model,
// because there is no reason to have exactly one of each make.
@Nullable
public Car findByMake(Car.Make make) {
for (Car car : cars) {
if (car.make == make) {
return car;
}
}
return null;
}
}
answered Jun 29 '16 at 13:53
njzk2njzk2
32.7k44989
32.7k44989
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38101069%2fjava-when-to-use-generics-method-and-when-explicit-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
How about
public Car getCar()
? That would encourage callers to be agnostic of the car type, making the code more extensible to future car types.– Andy Thomas
Jun 29 '16 at 13:31
This is not really a good SO question as it doesn't really contain a specifically answerable programming question, at least, as currently stated. Also it's spelled 'Renault'.
– pvg
Jun 29 '16 at 13:34
what is a Reno?
– njzk2
Jun 29 '16 at 13:44
@njzk2 en.wikipedia.org/wiki/Suzuki_Reno, perhaps? (More likely a misspelling of Renault, though).
– Andy Turner
Jun 29 '16 at 13:45
I don't think the make of a car is sufficient to justify polymorphism. You can probably have a
Car
class, with an enum for the make. And in your repo, a list or map of cars that you can search to return those of a certain brand, model, year, color, ...– njzk2
Jun 29 '16 at 13:46