Merge pull request #281 from ankurkaushal/master

Reformat according to google style guide
This commit is contained in:
Ilkka Seppälä
2015-11-02 21:39:17 +02:00
438 changed files with 8257 additions and 8382 deletions

View File

@ -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));
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}

View File

@ -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));
}
}

View File

@ -7,8 +7,8 @@ package com.iluwatar.state;
*/
public interface State {
void onEnterState();
void onEnterState();
void observe();
void observe();
}

View File

@ -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);
}
}