Skip to content

Commit

Permalink
Add State
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Mar 3, 2020
1 parent fa84a73 commit 6d0a66c
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package decorator;

public class ComplexTaxTest {
public class ComplexTaxMain {

public static void main(String[] args) {
System.out.println("--------------------------------------------- Begin Test for Complex Tax");
Expand Down
25 changes: 25 additions & 0 deletions src/state/Annotation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##############################
# State #
##############################





##### Language - EN

- When we have states and we want to change some implementation according to the state, or even block the state by going to a state that it shouldn't.

- The main situation that gives rise to the Design Pattern State is the need to implement a state machine. Usually,
the control of possible transitions are several and complex, making the implementation not simple.
The State helps to keep track of the states simple and organized by creating classes that represent each state and know how to control the transitions.



##### Language - PT-BR

- Quando temos estados e queremos alterar alguma implementação de acordo com o estado, ou até mesmo bloquear o estado indo para um estado que não deve.

- A principal situação que faz emergir o Design Pattern State é a necessidade de implementação de uma máquina de estados. Geralmente,
o controle das possíveis transições são vários e complexos, fazendo com que a implementação não seja simples.
O State auxilia a manter o controle dos estados simples e organizados através da criação de classes que representem cada estado e saiba controlar as transições.
25 changes: 25 additions & 0 deletions src/state/Approved.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package state;

public class Approved implements StateOfBudget {

@Override
public void applyExtraDiscount(Budget budget) {
budget.value -= budget.value * 0.02;
}

@Override
public void toApprove(Budget budget) {
throw new RuntimeException("Budget is already approved.");
}

@Override
public void toDisapprove(Budget budget) {
throw new RuntimeException("Budget is already disapprove.");
}

@Override
public void toFinish(Budget budget) {
budget.currentState = new Finished();
}

}
51 changes: 51 additions & 0 deletions src/state/Budget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package state;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import templateMethod.Item;

public class Budget {

protected double value;
private final List<Item> items;

protected StateOfBudget currentState;

public Budget(double value) {
super();
this.value = value;
items = new ArrayList<Item>();
currentState = new OnApproval(); //State Initial
}

public double getValue() {
return value;
}

public void addItem(Item item) {
items.add(item);
}

public List<Item> getItems() {
return Collections.unmodifiableList(items);// unmodifiableList -> Return a List immutable.
}

public void applyExtraDiscount() {
currentState.applyExtraDiscount(this); // this -> we pass the object itself, because this is where we want to make the changes
}

public void toApprove() {
currentState.toApprove(this);
}

public void toDisapprove() {
currentState.toDisapprove(this);
}

public void toFinish() {
currentState.toFinish(this);
}

}
25 changes: 25 additions & 0 deletions src/state/Disapproved.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package state;

public class Disapproved implements StateOfBudget {

@Override
public void applyExtraDiscount(Budget budget) {
throw new RuntimeException("Disapproved budget does not receive extra discounts.");
}

@Override
public void toApprove(Budget budget) {
throw new RuntimeException("Disapproved budgets cannot be approved.");
}

@Override
public void toDisapprove(Budget budget) {
throw new RuntimeException("Budget is already disapprove.");
}

@Override
public void toFinish(Budget budget) {
budget.currentState = new Finished();
}

}
25 changes: 25 additions & 0 deletions src/state/Finished.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package state;

public class Finished implements StateOfBudget {

@Override
public void applyExtraDiscount(Budget budget) {
throw new RuntimeException("Finalized budget do not receive extra discounts.");
}

@Override
public void toApprove(Budget budget) {
throw new RuntimeException("Budgets already finalized.");
}

@Override
public void toDisapprove(Budget budget) {
throw new RuntimeException("Budgets already finalized.");
}

@Override
public void toFinish(Budget budget) {
throw new RuntimeException("Budgets already finalized.");
}

}
25 changes: 25 additions & 0 deletions src/state/OnApproval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package state;

public class OnApproval implements StateOfBudget {

@Override
public void applyExtraDiscount(Budget budget) {
budget.value -= budget.value * 0.05;
}

@Override
public void toApprove(Budget budget) {
budget.currentState = new Approved();
}

@Override
public void toDisapprove(Budget budget) {
budget.currentState = new Disapproved();
}

@Override
public void toFinish(Budget budget) {
throw new RuntimeException("Approving budgets, so they can't go straight to finalized.");
}

}
30 changes: 30 additions & 0 deletions src/state/StateMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package state;

public class StateMain {

public static void main(String[] args) {
System.out.println("--------------------------------------------- Begin Test State");


Budget budgetExtra = new Budget(500);

// Current State = OnApproval = 0.05
budgetExtra.applyExtraDiscount();
System.out.println("Aplly Extra Discount: " + budgetExtra.getValue()); // 500 -= 500 * 0.05 ->>> 475.0


// Current State HERE = Approve = 0.02
budgetExtra.toApprove();
budgetExtra.applyExtraDiscount();
System.out.println("Aplly Extra Discount with 'approve': " + budgetExtra.getValue()); // 475.0 -= 475.0 * 0.02 ->>> 465.0


// the code below will generate an error, as we cannot apply a discount for this state ''
budgetExtra.toFinish(); //OK - PROCESS FINISH
//budgetExtra.applyExtraDiscount(); //Error


System.out.println("--------------------------------------------- End Test State");
}

}
12 changes: 12 additions & 0 deletions src/state/StateOfBudget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package state;

public interface StateOfBudget {

void applyExtraDiscount(Budget budget);

// Apply State
void toApprove(Budget budget);
void toDisapprove(Budget budget);
void toFinish(Budget budget);

}
Binary file added src/state/print-for-help-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6d0a66c

Please sign in to comment.