mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-21 08:24:40 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -25,23 +25,17 @@
|
||||
package com.iluwatar.model.view.intent;
|
||||
|
||||
/**
|
||||
* Model-View-Intent is a pattern for implementing user interfaces.
|
||||
* Its main advantage over MVVM which it closely mirrors is a
|
||||
* minimal public api with which user events can be exposed to the ViewModel.
|
||||
* In case of the MVI every event is exposed by using a single method
|
||||
* with 1 argument which implements UserEvent interface.
|
||||
* Specific parameters can be expressed as its parameters. In this case,
|
||||
* we'll be using MVI to implement a simple calculator
|
||||
* with +, -, /, * operations and the ability to set the variable.
|
||||
* It's important to note, that every user action happens through the
|
||||
* Model-View-Intent is a pattern for implementing user interfaces. Its main advantage over MVVM
|
||||
* which it closely mirrors is a minimal public api with which user events can be exposed to the
|
||||
* ViewModel. In case of the MVI every event is exposed by using a single method with 1 argument
|
||||
* which implements UserEvent interface. Specific parameters can be expressed as its parameters. In
|
||||
* this case, we'll be using MVI to implement a simple calculator with +, -, /, * operations and the
|
||||
* ability to set the variable. It's important to note, that every user action happens through the
|
||||
* view, we never interact with the ViewModel directly.
|
||||
*/
|
||||
public final class App {
|
||||
|
||||
|
||||
/**
|
||||
* To avoid magic value lint error.
|
||||
*/
|
||||
/** To avoid magic value lint error. */
|
||||
private static final double RANDOM_VARIABLE = 10.0;
|
||||
|
||||
/**
|
||||
@@ -61,10 +55,10 @@ public final class App {
|
||||
|
||||
// add calculator variable to output -> calculator output = 10.0
|
||||
view.add();
|
||||
view.displayTotal(); // display output
|
||||
view.displayTotal(); // display output
|
||||
|
||||
variable1 = 2.0;
|
||||
view.setVariable(variable1); // calculator variable = 2.0
|
||||
view.setVariable(variable1); // calculator variable = 2.0
|
||||
|
||||
// subtract calculator variable from output -> calculator output = 8
|
||||
view.subtract();
|
||||
@@ -77,9 +71,6 @@ public final class App {
|
||||
view.displayTotal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Avoid default constructor lint error.
|
||||
*/
|
||||
private App() {
|
||||
}
|
||||
/** Avoid default constructor lint error. */
|
||||
private App() {}
|
||||
}
|
||||
|
||||
+5
-13
@@ -27,21 +27,13 @@ package com.iluwatar.model.view.intent;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Current state of calculator.
|
||||
*/
|
||||
/** Current state of calculator. */
|
||||
@Data
|
||||
public class CalculatorModel {
|
||||
|
||||
/**
|
||||
* Current calculator variable used for operations.
|
||||
**/
|
||||
@Getter
|
||||
private final Double variable;
|
||||
/** Current calculator variable used for operations. */
|
||||
@Getter private final Double variable;
|
||||
|
||||
/**
|
||||
* Current calculator output -> is affected by operations.
|
||||
**/
|
||||
@Getter
|
||||
private final Double output;
|
||||
/** Current calculator output -> is affected by operations. */
|
||||
@Getter private final Double output;
|
||||
}
|
||||
|
||||
+11
-28
@@ -34,55 +34,38 @@ import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Exposes changes to the state of calculator
|
||||
* to {@link CalculatorViewModel} through
|
||||
* {@link com.iluwatar.model.view.intent.actions.CalculatorAction}
|
||||
* and displays its updated {@link CalculatorModel}.
|
||||
* Exposes changes to the state of calculator to {@link CalculatorViewModel} through {@link
|
||||
* com.iluwatar.model.view.intent.actions.CalculatorAction} and displays its updated {@link
|
||||
* CalculatorModel}.
|
||||
*/
|
||||
@Slf4j
|
||||
@Data
|
||||
public class CalculatorView {
|
||||
|
||||
/**
|
||||
* View model param handling the operations.
|
||||
*/
|
||||
@Getter
|
||||
private final CalculatorViewModel viewModel;
|
||||
/** View model param handling the operations. */
|
||||
@Getter private final CalculatorViewModel viewModel;
|
||||
|
||||
/**
|
||||
* Display current view model output with logger.
|
||||
*/
|
||||
/** Display current view model output with logger. */
|
||||
void displayTotal() {
|
||||
LOGGER.info(
|
||||
"Total value = {}",
|
||||
viewModel.getCalculatorModel().getOutput().toString()
|
||||
);
|
||||
LOGGER.info("Total value = {}", viewModel.getCalculatorModel().getOutput().toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle addition action.
|
||||
*/
|
||||
/** Handle addition action. */
|
||||
void add() {
|
||||
viewModel.handleAction(new AdditionCalculatorAction());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle subtraction action.
|
||||
*/
|
||||
/** Handle subtraction action. */
|
||||
void subtract() {
|
||||
viewModel.handleAction(new SubtractionCalculatorAction());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle multiplication action.
|
||||
*/
|
||||
/** Handle multiplication action. */
|
||||
void multiply() {
|
||||
viewModel.handleAction(new MultiplicationCalculatorAction());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle division action.
|
||||
*/
|
||||
/** Handle division action. */
|
||||
void divide() {
|
||||
viewModel.handleAction(new DivisionCalculatorAction());
|
||||
}
|
||||
|
||||
+14
-42
@@ -32,16 +32,12 @@ import com.iluwatar.model.view.intent.actions.SetVariableCalculatorAction;
|
||||
import com.iluwatar.model.view.intent.actions.SubtractionCalculatorAction;
|
||||
|
||||
/**
|
||||
* Handle transformations to {@link CalculatorModel}
|
||||
* based on intercepted {@link CalculatorAction}.
|
||||
* Handle transformations to {@link CalculatorModel} based on intercepted {@link CalculatorAction}.
|
||||
*/
|
||||
public final class CalculatorViewModel {
|
||||
|
||||
/**
|
||||
* Current calculator model (can be changed).
|
||||
*/
|
||||
private CalculatorModel model =
|
||||
new CalculatorModel(0.0, 0.0);
|
||||
/** Current calculator model (can be changed). */
|
||||
private CalculatorModel model = new CalculatorModel(0.0, 0.0);
|
||||
|
||||
/**
|
||||
* Handle calculator action.
|
||||
@@ -55,8 +51,7 @@ public final class CalculatorViewModel {
|
||||
case MultiplicationCalculatorAction.MULTIPLICATION -> multiply();
|
||||
case DivisionCalculatorAction.DIVISION -> divide();
|
||||
case SetVariableCalculatorAction.SET_VARIABLE -> {
|
||||
SetVariableCalculatorAction setVariableAction =
|
||||
(SetVariableCalculatorAction) action;
|
||||
SetVariableCalculatorAction setVariableAction = (SetVariableCalculatorAction) action;
|
||||
setVariable(setVariableAction.getVariable());
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Unknown tag");
|
||||
@@ -78,49 +73,26 @@ public final class CalculatorViewModel {
|
||||
* @param variable -> value of new calculator model variable.
|
||||
*/
|
||||
private void setVariable(final Double variable) {
|
||||
model = new CalculatorModel(
|
||||
variable,
|
||||
model.getOutput()
|
||||
);
|
||||
model = new CalculatorModel(variable, model.getOutput());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add variable to model output.
|
||||
*/
|
||||
/** Add variable to model output. */
|
||||
private void add() {
|
||||
model = new CalculatorModel(
|
||||
model.getVariable(),
|
||||
model.getOutput() + model.getVariable()
|
||||
);
|
||||
model = new CalculatorModel(model.getVariable(), model.getOutput() + model.getVariable());
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract variable from model output.
|
||||
*/
|
||||
/** Subtract variable from model output. */
|
||||
private void subtract() {
|
||||
model = new CalculatorModel(
|
||||
model.getVariable(),
|
||||
model.getOutput() - model.getVariable()
|
||||
);
|
||||
model = new CalculatorModel(model.getVariable(), model.getOutput() - model.getVariable());
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply model output by variable.
|
||||
*/
|
||||
/** Multiply model output by variable. */
|
||||
private void multiply() {
|
||||
model = new CalculatorModel(
|
||||
model.getVariable(),
|
||||
model.getOutput() * model.getVariable()
|
||||
);
|
||||
model = new CalculatorModel(model.getVariable(), model.getOutput() * model.getVariable());
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide model output by variable.
|
||||
*/
|
||||
/** Divide model output by variable. */
|
||||
private void divide() {
|
||||
model = new CalculatorModel(
|
||||
model.getVariable(),
|
||||
model.getOutput() / model.getVariable()
|
||||
);
|
||||
model = new CalculatorModel(model.getVariable(), model.getOutput() / model.getVariable());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-10
@@ -24,20 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.intent.actions;
|
||||
|
||||
/**
|
||||
* Addition {@link CalculatorAction}.
|
||||
* */
|
||||
/** Addition {@link CalculatorAction}. */
|
||||
public class AdditionCalculatorAction implements CalculatorAction {
|
||||
/**
|
||||
* Subclass tag.
|
||||
* */
|
||||
/** Subclass tag. */
|
||||
public static final String ADDITION = "ADDITION";
|
||||
|
||||
/**
|
||||
* Makes checking subclass type trivial.
|
||||
* */
|
||||
/** Makes checking subclass type trivial. */
|
||||
@Override
|
||||
public String tag() {
|
||||
return ADDITION;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-5
@@ -24,16 +24,13 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.intent.actions;
|
||||
|
||||
/**
|
||||
* Defines what outside interactions can be consumed by view model.
|
||||
* */
|
||||
/** Defines what outside interactions can be consumed by view model. */
|
||||
public interface CalculatorAction {
|
||||
|
||||
/**
|
||||
* Makes identifying action trivial.
|
||||
*
|
||||
* @return subclass tag.
|
||||
* */
|
||||
*/
|
||||
String tag();
|
||||
}
|
||||
|
||||
|
||||
+4
-10
@@ -24,20 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.intent.actions;
|
||||
|
||||
/**
|
||||
* Division {@link CalculatorAction}.
|
||||
* */
|
||||
/** Division {@link CalculatorAction}. */
|
||||
public class DivisionCalculatorAction implements CalculatorAction {
|
||||
/**
|
||||
* Subclass tag.
|
||||
* */
|
||||
/** Subclass tag. */
|
||||
public static final String DIVISION = "DIVISION";
|
||||
|
||||
/**
|
||||
* Makes checking subclass type trivial.
|
||||
* */
|
||||
/** Makes checking subclass type trivial. */
|
||||
@Override
|
||||
public String tag() {
|
||||
return DIVISION;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-10
@@ -24,20 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.intent.actions;
|
||||
|
||||
/**
|
||||
* Multiplication {@link CalculatorAction}.
|
||||
* */
|
||||
/** Multiplication {@link CalculatorAction}. */
|
||||
public class MultiplicationCalculatorAction implements CalculatorAction {
|
||||
/**
|
||||
* Subclass tag.
|
||||
* */
|
||||
/** Subclass tag. */
|
||||
public static final String MULTIPLICATION = "MULTIPLICATION";
|
||||
|
||||
/**
|
||||
* Makes checking subclass type trivial.
|
||||
* */
|
||||
/** Makes checking subclass type trivial. */
|
||||
@Override
|
||||
public String tag() {
|
||||
return MULTIPLICATION;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-15
@@ -27,28 +27,19 @@ package com.iluwatar.model.view.intent.actions;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* SetVariable {@link CalculatorAction}.
|
||||
*/
|
||||
/** SetVariable {@link CalculatorAction}. */
|
||||
@Data
|
||||
public final class SetVariableCalculatorAction implements CalculatorAction {
|
||||
|
||||
/**
|
||||
* Subclass tag.
|
||||
*/
|
||||
/** Subclass tag. */
|
||||
public static final String SET_VARIABLE = "SET_VARIABLE";
|
||||
|
||||
/**
|
||||
* Used by {@link com.iluwatar.model.view.intent.CalculatorViewModel}.
|
||||
*/
|
||||
@Getter
|
||||
private final Double variable;
|
||||
/** Used by {@link com.iluwatar.model.view.intent.CalculatorViewModel}. */
|
||||
@Getter private final Double variable;
|
||||
|
||||
/**
|
||||
* Makes checking subclass type trivial.
|
||||
*/
|
||||
/** Makes checking subclass type trivial. */
|
||||
@Override
|
||||
public String tag() {
|
||||
return SET_VARIABLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-10
@@ -24,20 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.intent.actions;
|
||||
|
||||
/**
|
||||
* Subtraction {@link CalculatorAction}.
|
||||
* */
|
||||
/** Subtraction {@link CalculatorAction}. */
|
||||
public class SubtractionCalculatorAction implements CalculatorAction {
|
||||
/**
|
||||
* Subclass tag.
|
||||
* */
|
||||
/** Subclass tag. */
|
||||
public static final String SUBTRACTION = "SUBTRACTION";
|
||||
|
||||
/**
|
||||
* Makes checking subclass type trivial.
|
||||
* */
|
||||
/** Makes checking subclass type trivial. */
|
||||
@Override
|
||||
public String tag() {
|
||||
return SUBTRACTION;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -23,8 +23,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
/**
|
||||
* Handle actions for {@link com.iluwatar.model.view.intent.CalculatorModel}
|
||||
* defined by {@link com.iluwatar.model.view.intent.actions.CalculatorAction}.
|
||||
* Handle actions for {@link com.iluwatar.model.view.intent.CalculatorModel} defined by {@link
|
||||
* com.iluwatar.model.view.intent.actions.CalculatorAction}.
|
||||
*/
|
||||
|
||||
package com.iluwatar.model.view.intent.actions;
|
||||
|
||||
@@ -22,9 +22,5 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
/**
|
||||
* Define Model, View and ViewModel.
|
||||
* Use them in {@link com.iluwatar.model.view.intent.App}
|
||||
*/
|
||||
|
||||
/** Define Model, View and ViewModel. Use them in {@link com.iluwatar.model.view.intent.App} */
|
||||
package com.iluwatar.model.view.intent;
|
||||
|
||||
Reference in New Issue
Block a user