How to program according to good OO design practices? [closed]

2

I did this program, simple and nothing complicated. My problem is that even though I'm doing exactly what I want, it does not follow correct patterns of object-oriented design. What should I do to, for example, wipe out static variables, 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;
}

}
    
asked by anonymous 13.03.2018 / 16:32

2 answers

3

The basic part you have already done. You can think about whether to use get and set on all attributes because there is something the class should not change. You can ask why change the counter id, if it can not change, it can not have a set.

Another thing that can change and have a method that increases and maybe some that will lower the counter. Can you let the meter go anyways? Can you reset, can you put a high number? And it can have a validation as the friend above wrote.

You might want to make the consumption protected so you do not have to call getConsumo.

Oh I remembered that the UI cost should be static as well.

    
22.03.2018 / 13:05
0

Maybe not exactly what you're looking for, but a good practice and use the set method to control data consistencies and prevent negative values from being saved where they should not.

Instead:

public void setConsumo(double consumo) 
{
    this.consumo = consumo;
}

That:

public void setConsumo(double consumoCliente )

     {
         if(consumoCliente >= 0.0)
            consumo = consumoCliente;
         else
           consumo = 0.0;
    }

And about the use of inheritance in your code, I honestly do not see anything out of rule, alias is an excellent practice. An alternative to inheritance would be composition, but there will be repetition of code that is not a good practice.

    
13.03.2018 / 18:11