How to use different implementations from the same interface

Asked

Viewed 119 times

1

I’m having trouble understanding the diagram and specifications below.

How I do the method double getCalculaFrete() return the same value returned by the method double calcularFretePedido(), being called by the variable calculaFrete in class Pedido, that is to say, calculaFrete.calcularFretePedido().

And every concrete class that inherits from the abstract class CalcularFrete should return a value double for the value of your freight. An example would be, a standard freight costs R $ 3,50 and an express freight costs R $ 5,50.

Create two objects to test this program in the Main class. In order for the test to be done correctly, the objects need to be of the type of the Order class and to show the freight value of each one just access the double getCalculaFrete() method of that same object, ie, whereas an object was created with the following name Application requested 1... , to show the value of your freight just trigger the return so requested 1.getCalculaFrete() and print it. Thanks to polymorphism, the Request class constructor receives any object from a given class that has a "is one" relationship with the abstract class.

I’m having trouble printing it too... Help...

Thank you!

inserir a descrição da imagem aqui

1 answer

0


Like CalcularFrete is an abstract class, you can leave the method calcularFretePedido abstract:

public abstract class CalcularFrete {
    public abstract double calcularFretePedido();
}

Which means the subclasses of CalcularFrete that must have their specific rules to know the value of the freight. In your case, you would have standard freight returning $3.50 and express freight returning $5.50:

public class CalculadoraFretePadrao extends CalcularFrete {
    // frete padrao, 3.50
    public double calcularFretePedido() {
        return 3.5;
    }
}

public class CalculadoraFreteExpresso extends CalcularFrete {
    // frete expresso, 5.50
    public double calcularFretePedido() {
        return 5.5;
    }
}

Now you have 2 classes that know how to calculate freight, each taking care of their specific freight type (standard or express).

Already the class Pedido has a field of the type CalcularFrete. That is, she can receive any subclass of CalcularFrete, and the value of freteCalculado() will be returned by the type of freight he receives:

public class Pedido {
    private CalcularFrete calculaFrete;

    // recebe qualquer subclasse de CalcularFrete
    public Pedido(CalcularFrete calculaFrete) {
        this.calculaFrete = calculaFrete;
    }

    // retorna o valor do frete calculado
    public double freteCalculado() {
        return this.calculaFrete.calcularFretePedido();
    }
}

To use the class Pedido, you pass in the builder the type of freight you want to use:

Pedido pedidoComFretePadrao = new Pedido(new CalculadoraFretePadrao());
double valorFretePadrao = pedidoComFretePadrao.freteCalculado(); // 3.5

Pedido pedidoComFreteExpresso = new Pedido(new CalculadoraFreteExpresso());
double valorFreteExpresso = pedidoComFreteExpresso.freteCalculado(); // 5.5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.