면책 조항 - 이것은 학교 학기 프로젝트의 일부입니다.Java의 메멘토 패턴 - 내부 클래스의 serialization
개체 상태를 저장하려면 Memento 패턴을 사용해야합니다. 이 객체는 MVC의 모델입니다. 그래서 지금 (간체)되어있는 방법은 :
public class Model {
// ...
public static class Memento implements Serializable {
protected Stats stats;
public Memento(Stats stats) {
this.stats = stats;
}
}
public static class Stats implements Serializable {
protected int score;
protected int cannonPos;
protected int cannonAngle;
protected int cannonSpeed;
protected int totalShotsFired = 0;
protected int enemiesHit;
protected transient List<StatsObserver> observers = new ArrayList<StatsObserver>();
// + getters etc
}
}
나는 외부 클래스가없는 동안 직렬화 내부 클래스를 가진 것은 합리적인 이유가 자바에서 정말 불가능하다는 것을 읽었습니다. 그러나 내 경우에는 내부 클래스가있을 때 외부 클래스를 인스턴스화 할 필요가 없습니다. 내부는 전혀 외부가 필요하지 않습니다. 외부 클래스가 내부 구성원을 액세스 할 수 있도록이 구조로만 구성됩니다.
... 그뿐만 아니라 의미가 있습니다 :
이 내 과정의 유품 설명의 말씀입니다. 모델 만이 Memento 내부의 세부 정보에 액세스 할 수 있어야합니다. "Caretaker"객체 (디스크에서 /로 데이터 저장/검색을 처리하는 객체)는 객체 내부에서 보지 않아야합니다. Java에는 친구 클래스가 없으므로이 방법이 있어야합니다.
제안 된대로 구현하려면 직렬화를 사용할 수 없습니까?
편집 :
내가 메멘토 클래스는 정적뿐만 아니라 통계 클래스했다,하지만 난 여전히 오류를 얻고있다. 아직 Model.Stats
java.io.NotSerializableException: cz.melkamar.adp.shooter.model.Model
- field (class "cz.melkamar.adp.shooter.model.Model$Stats", name: "this$0", type: "class cz.melkamar.adp.shooter.model.Model")
- object (class "cz.melkamar.adp.shooter.model.Model$Stats", [email protected])
- field (class "cz.melkamar.adp.shooter.model.Model$Memento", name: "stats", type: "class cz.melkamar.adp.shooter.model.Model$Stats")
- root object (class "cz.melkamar.adp.shooter.model.Model$Memento", [email protected])
클래스'Model' 또는 내부 클래스'Stats'에는 직렬화되지 않는 객체에 대한 참조를 보유하는 멤버 또는 (익명) 멤버가 있습니다. – Jesper