Formatted all files to the same standard
This commit is contained in:
@ -3,34 +3,34 @@ package com.iluwatar;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Memento pattern is for storing and restoring object state. The object (Star)
|
||||
* gives out a "memento" (StarMemento) that contains the state of the object.
|
||||
* Later on the memento can be set back to the object restoring the state.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Stack<StarMemento> states = new Stack<>();
|
||||
public static void main(String[] args) {
|
||||
Stack<StarMemento> states = new Stack<>();
|
||||
|
||||
Star star = new Star(StarType.SUN, 10000000, 500000);
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
while (states.size() > 0) {
|
||||
star.setMemento(states.pop());
|
||||
System.out.println(star);
|
||||
}
|
||||
}
|
||||
Star star = new Star(StarType.SUN, 10000000, 500000);
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
states.add(star.getMemento());
|
||||
star.timePasses();
|
||||
System.out.println(star);
|
||||
while (states.size() > 0) {
|
||||
star.setMemento(states.pop());
|
||||
System.out.println(star);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,20 +3,20 @@ package com.iluwatar;
|
||||
/**
|
||||
*
|
||||
* Star uses "mementos" to store and restore state.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class Star {
|
||||
|
||||
|
||||
private StarType type;
|
||||
private int ageYears;
|
||||
private int massTons;
|
||||
|
||||
|
||||
public Star(StarType startType, int startAge, int startMass) {
|
||||
this.type = startType;
|
||||
this.ageYears = startAge;
|
||||
this.massTons = startMass;
|
||||
}
|
||||
|
||||
|
||||
public void timePasses() {
|
||||
ageYears *= 2;
|
||||
massTons *= 8;
|
||||
@ -41,7 +41,7 @@ public class Star {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
StarMemento getMemento() {
|
||||
|
||||
StarMementoInternal state = new StarMementoInternal();
|
||||
@ -49,20 +49,21 @@ public class Star {
|
||||
state.setMassTons(massTons);
|
||||
state.setType(type);
|
||||
return state;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void setMemento(StarMemento memento) {
|
||||
|
||||
|
||||
StarMementoInternal state = (StarMementoInternal) memento;
|
||||
this.type = state.getType();
|
||||
this.ageYears = state.getAgeYears();
|
||||
this.massTons = state.getMassTons();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s age: %d years mass: %d tons", type.toString(), ageYears, massTons);
|
||||
return String.format("%s age: %d years mass: %d tons", type.toString(),
|
||||
ageYears, massTons);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
||||
/**
|
||||
*
|
||||
* External interface to memento.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public interface StarMemento {
|
||||
|
||||
|
@ -3,29 +3,34 @@ package com.iluwatar;
|
||||
/**
|
||||
*
|
||||
* Internal interface to memento.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class StarMementoInternal implements StarMemento {
|
||||
|
||||
private StarType type;
|
||||
private int ageYears;
|
||||
private int massTons;
|
||||
|
||||
|
||||
public StarType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(StarType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getAgeYears() {
|
||||
return ageYears;
|
||||
}
|
||||
|
||||
public void setAgeYears(int ageYears) {
|
||||
this.ageYears = ageYears;
|
||||
}
|
||||
|
||||
public int getMassTons() {
|
||||
return massTons;
|
||||
}
|
||||
|
||||
public void setMassTons(int massTons) {
|
||||
this.massTons = massTons;
|
||||
}
|
||||
|
@ -2,11 +2,7 @@ package com.iluwatar;
|
||||
|
||||
public enum StarType {
|
||||
|
||||
SUN,
|
||||
RED_GIANT,
|
||||
WHITE_DWARF,
|
||||
SUPERNOVA,
|
||||
DEAD;
|
||||
SUN, RED_GIANT, WHITE_DWARF, SUPERNOVA, DEAD;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@ -32,5 +28,5 @@ public enum StarType {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user