Reformat rest of the design patterns - Issue #224
This commit is contained in:
@ -7,20 +7,20 @@ package com.iluwatar.state;
|
||||
*/
|
||||
public class AngryState implements State {
|
||||
|
||||
private Mammoth mammoth;
|
||||
private Mammoth mammoth;
|
||||
|
||||
public AngryState(Mammoth mammoth) {
|
||||
this.mammoth = mammoth;
|
||||
}
|
||||
public AngryState(Mammoth mammoth) {
|
||||
this.mammoth = mammoth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void observe() {
|
||||
System.out.println(String.format("%s is furious!", mammoth));
|
||||
}
|
||||
@Override
|
||||
public void observe() {
|
||||
System.out.println(String.format("%s is furious!", mammoth));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnterState() {
|
||||
System.out.println(String.format("%s gets angry!", mammoth));
|
||||
}
|
||||
@Override
|
||||
public void onEnterState() {
|
||||
System.out.println(String.format("%s gets angry!", mammoth));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,27 +2,25 @@ package com.iluwatar.state;
|
||||
|
||||
/**
|
||||
*
|
||||
* In State pattern the container object has an internal state object that
|
||||
* defines the current behavior. The state object can be changed to alter the
|
||||
* behavior.
|
||||
* In State pattern the container object has an internal state object that defines the current
|
||||
* behavior. The state object can be changed to alter the behavior.
|
||||
* <p>
|
||||
* This can be a cleaner way for an object to change its behavior at runtime
|
||||
* without resorting to large monolithic conditional statements and thus improves
|
||||
* maintainability.
|
||||
* This can be a cleaner way for an object to change its behavior at runtime without resorting to
|
||||
* large monolithic conditional statements and thus improves maintainability.
|
||||
* <p>
|
||||
* In this example the {@link Mammoth} changes its behavior as time passes by.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Mammoth mammoth = new Mammoth();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
Mammoth mammoth = new Mammoth();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
mammoth.timePasses();
|
||||
mammoth.observe();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,31 +7,31 @@ package com.iluwatar.state;
|
||||
*/
|
||||
public class Mammoth {
|
||||
|
||||
private State state;
|
||||
private State state;
|
||||
|
||||
public Mammoth() {
|
||||
state = new PeacefulState(this);
|
||||
}
|
||||
public Mammoth() {
|
||||
state = new PeacefulState(this);
|
||||
}
|
||||
|
||||
public void timePasses() {
|
||||
if (state.getClass().equals(PeacefulState.class)) {
|
||||
changeStateTo(new AngryState(this));
|
||||
} else {
|
||||
changeStateTo(new PeacefulState(this));
|
||||
}
|
||||
}
|
||||
public void timePasses() {
|
||||
if (state.getClass().equals(PeacefulState.class)) {
|
||||
changeStateTo(new AngryState(this));
|
||||
} else {
|
||||
changeStateTo(new PeacefulState(this));
|
||||
}
|
||||
}
|
||||
|
||||
private void changeStateTo(State newState) {
|
||||
this.state = newState;
|
||||
this.state.onEnterState();
|
||||
}
|
||||
private void changeStateTo(State newState) {
|
||||
this.state = newState;
|
||||
this.state.onEnterState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "The mammoth";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "The mammoth";
|
||||
}
|
||||
|
||||
public void observe() {
|
||||
this.state.observe();
|
||||
}
|
||||
public void observe() {
|
||||
this.state.observe();
|
||||
}
|
||||
}
|
||||
|
@ -7,20 +7,20 @@ package com.iluwatar.state;
|
||||
*/
|
||||
public class PeacefulState implements State {
|
||||
|
||||
private Mammoth mammoth;
|
||||
private Mammoth mammoth;
|
||||
|
||||
public PeacefulState(Mammoth mammoth) {
|
||||
this.mammoth = mammoth;
|
||||
}
|
||||
public PeacefulState(Mammoth mammoth) {
|
||||
this.mammoth = mammoth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void observe() {
|
||||
System.out.println(String.format("%s is calm and peaceful.", mammoth));
|
||||
}
|
||||
@Override
|
||||
public void observe() {
|
||||
System.out.println(String.format("%s is calm and peaceful.", mammoth));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnterState() {
|
||||
System.out.println(String.format("%s calms down.", mammoth));
|
||||
}
|
||||
@Override
|
||||
public void onEnterState() {
|
||||
System.out.println(String.format("%s calms down.", mammoth));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ package com.iluwatar.state;
|
||||
*/
|
||||
public interface State {
|
||||
|
||||
void onEnterState();
|
||||
void onEnterState();
|
||||
|
||||
void observe();
|
||||
void observe();
|
||||
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.state.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user