2
I did this program, simple and nothing complicated. My problem is that although this is doing exactly what I want, it does not follow correct patterns of object-oriented design. What should I do to, for example, end variable statics, extends, gets and sets...?
This is the code I have.
package com.mycompany.exerciciocontadores;
public abstract class Contador {
private String cliente;
private String id;
private double consumo;
private static String CLIENTE_POR_OMISSAO = "sem cliente";
private static String ID_POR_OMISSAO = "sem ID";
private static double CONSUMO_POR_OMISSAO = 0;
public Contador(String cliente, String id, double consumo) {
this.cliente = cliente;
this.id = id;
this.consumo = consumo;
}
public Contador() {
cliente = CLIENTE_POR_OMISSAO;
id = ID_POR_OMISSAO;
consumo = CONSUMO_POR_OMISSAO;
}
public String getCliente() {
return cliente;
}
public String getId() {
return id;
}
public double getConsumo() {
return consumo;
}
public void setCliente(String cliente) {
this.cliente = cliente;
}
public void setId(String id) {
this.id = id;
}
public void setConsumo(double consumo) {
this.consumo = consumo;
}
@Override
public String toString() {
return "Contador{" + "cliente=" + cliente + ", id=" + id + ", consumo=" + consumo + '}';
}
public abstract double calcularCusto();
}
The subclass:
package com.mycompany.exerciciocontadores;
public class ContadorGas extends Contador {
private static final String PREFIXO = "GAS-";
private static int contador = 0;
private double custoUnitario = 0.8;
public ContadorGas(String cliente, double consumo) {
super(cliente, PREFIXO + (++contador), consumo);
}
public static int getContador() {
return contador;
}
public double getCustoUnitario() {
return custoUnitario;
}
public static void setContador(int contador) {
ContadorGas.contador = contador;
}
public void setCustoUnitario(double custoUnitario) {
this.custoUnitario = custoUnitario;
}
@Override
public double calcularCusto() {
return getConsumo() * custoUnitario;
}
}
And what’s the problem with using static variables, using inheritance, getters and settter? If the resource exists in the language, it is because it can be used, what it cannot use is not knowing what it is doing.
– user28595
@Articuno no problem. if it is a language resource, it should be used but does not follow correct patterns of object-oriented design. , for example, gets and sets in many cases are unnecessaries extends can seriously affect ease of code maintenance
– skidils
https://answall.com/questions/tagged/orienta%C3%A7%C3%a3o-objetos? Sort=votes&pageSize=50
– Maniero
Where did you read that it doesn’t follow the right standards? Can you show? I’ve never heard anything about it.
– user28595
http://www.oodesign.com/ Here for example or here http://williamdurand.fr/2013/07/30/from-stupid-to-solid-code/
– skidils
It doesn’t make much sense to say that
get
s andset
s are not good OO practice, since without them all variables would be public, without encapsulation, this could be a bad practice– Costamilam
@Guilhermecostamilam The question of get and set is the incorrect use of them. Many developers often create get/set methods for all class attributes which is the same as leaving public attributes only using a longer path.
– Fagner Fonseca
@skidils I think you’ve taken principles to the extreme. Static variables, inheritance and gets/sets are not a bad practice. What the SOLID principles say is that you should consider when using them. Simply removing them from your code will not leave your code with a better design. You have to understand why and in which situations these approaches are not interesting.
– Fagner Fonseca
Tips: It’s interesting to note that you are returning
String
forgetCliente()
instead of returning an objectCliente
; you’re also usingdouble
to calculate money instead ofBigDecimal
, and I’m not sure, but it looks like you’re using strings in that code Model that will end up in the GUI. Also put validation code to ensure that the set values will be intact, as @Rogi93 said. Finally, review the Encapsulation (try not to let an external code use the Counter as "puppet", let the Counter itself take care of its logic, operation and integrity).– Douglas