Strategy in Java to decouple freight calculation
Autor: Emanuel Antônio Lima Rocha 1 Base fundamental Introdução O Strategy é um padrão comportamental que reúne maneiras alternativas de executar uma operação sob um contrato comum, permitindo escolher a implementação usada pelo contexto. No catálogo de Gamma e colaboradores, ele pertence à categoria dos padrões comportamentais [1]. A separação entre contexto, interface e estratégias concretas…
The article discusses the application of the Strategy design pattern in a Java-based e-commerce system. The pattern allows for the selection of different calculation strategies for shipping costs, such as economic and express shipping. The example code provided demonstrates how the pattern can be implemented, with the `FreteEconomico` and `FreteExpresso` classes implementing the `FreteStrategy` interface.
The `CalculadoraFrete` class acts as the context, delegating the calculation to the chosen strategy. The article also presents a UML diagram and a high-level architecture of the system.
Written by urgent.news from Dev.to's report — not a translation of it. Machine-written — may contain errors; check the original before relying on it.