-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/decorator/ComplexTaxTest.java → src/decorator/ComplexTaxMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.