关于状态的OO编程
OO programming about state
这是我教授的 oo 编程作业。但是我发现Personclass里面的婚姻状态可以是一个变量,我怎么用两个class实现呢?
假设人们可以有婚姻状况:单身、已婚、丧偶、离婚。创建处理人们婚姻状况的状态 OODP(即 Java、JavaScript、C++、Python 或 Ruby)。您将至少有两个 classes:Person 和 Marital State。确保遵循以下规则:单身->已婚已婚->离婚|寡妇离婚 -> 已婚寡妇 -> 已婚 创建一个客户端 class 来测试你的程序。请务必测试有效和无效的婚姻状况变更。
The assignment page
这些是我的代码:
public class AssignmentOOP {
public static void main(String[] args) {
Person p1 = new Person("p1");
Person p2 = new Person("p2");
Person p3 = new Person("p3");
p1.PrintMaritalStatus();
p2.PrintMaritalStatus();
p3.PrintMaritalStatus();
p1.GetMarried(p2);
p1.GetMarried(p3);
p2.Died();
p1.GetMarried(p3);
}
}
class Person {
String maritalstatus;
boolean mateIsAlive;
Person mate;
String name;
Person(String name1) {
maritalstatus = "single";
mate = null;
name = name1;
}
void GetMarried(Person mate) {
if(this.maritalstatus.equals("married")|| mate.maritalstatus.equals("married"))
{
System.out.println("Marital status error! At least one of you are married");
return;
} else {
this.maritalstatus = "married";
this.mate = mate;
mate.maritalstatus = "married";
mate.mate = this;
System.out.println("Congratulations!!! " + this.name + " and " + mate.name + " are married!");
}
}
void GetDivorced(Person mate) {
if(this.maritalstatus.equals("married") && this.mate == mate) {
maritalstatus = "divorced";
System.out.println(this.name+" and "+mate.name+" are getting divorced.");
}else if(this.maritalstatus.equals("single")) {
System.out.println("You are not married and you cannot get divorced before getting married");
}else if(maritalstatus.equals("widow")) {
System.out.println("Your marital status is widow, you cannot get divorced.");
}
}
void Died() {
this.maritalstatus = "dead";
this.mate.maritalstatus = "widow";
System.out.println("Sorry for your loss, " + this.mate.name + " marital status is widow.");
}
void PrintMaritalStatus() {
System.out.println(this.name + " marital status is " + this.maritalstatus);
}
}
这里:
String maritalstatus;
这可能不是你的教授想要的。那个婚姻状况的要点是:只有一套固定的价值观。
正确的表达方式是在 java 中使用 enum,例如:
public enum MaritalStatus { SINGLE, MARRIED, ... }
然后您可以通过创建一个 状态机 来进一步增强此功能,该状态机会执行规则,例如:当您是单身时,并且 doMarry()
,您的新状态之后应该结婚了。
在复制和过去之前..
这个解决方案可能不是您的教授想要的。在这种情况下可以使用多种其他解决方案。
在下面的解决方案中我假设你只能使用2 类!
可能的解决方案
您的 Person
应该有一个 MaritalState
类型的字段。 MaritalState
跟踪 Person
的状态,因此它不必自己完成。
以下只是快速而粗略的,旨在向您展示聚合背后的想法。
人
Person
现在将所有依赖于 MaritalState
的逻辑委托给 MatrialState
public class Person {
MaritalState maritalState;
Person mate;
String name;
Person(String name1) {
maritalState = new MaritalState();
mate = null;
name = name1;
}
void GetMarried(Person mate) {
maritalState.marry(this, mate);
}
void GetDivorced(Person mate) {
maritalState.divorce(this, mate);
}
void Died() {
maritalState.die(this);
}
void PrintMaritalStatus() {
System.out.println(this.name + " maritalState status is " + this.maritalState.get());
}
public boolean isMarried() {
return maritalState.isMarried(maritalState);
}
public boolean isSingle() {
return maritalState.isSingle(maritalState);
}
public boolean isWidow() {
return maritalState.isWidow(maritalState);
}
}
婚姻状况
处于 Person
的逻辑现在处于 Martiral
状态。我扩展了一些功能,例如 isSingle()
以使代码更具可读性。
public class MaritalState {
private static final String MARRIED = "married";
private static final String SINGLE = "single";
private static final String WIDOW = "widow";
private String state;
private MaritalState(String state) {
this.state = state;
}
public MaritalState() {
this.state = "single";
}
void marry(Person person, Person mate) {
if (person.isMarried() || mate.isMarried()) {
System.out.println("MaritalState status error! At least one of you are married");
} else {
person.maritalState = new MaritalState(MARRIED);
person.mate = mate;
mate.maritalState = new MaritalState(MARRIED);
mate.mate = person;
System.out.println("Congratulations!!! " + person.name + " and " + mate.name + " are married!");
}
}
void divorce(Person person, Person mate) {
if (person.isMarried() && person.mate == mate) {
person.maritalState = new MaritalState("divorced");
System.out.println(person.name + " and " + mate.name + " are getting divorced.");
} else if (person.isSingle()) {
System.out.println("You are not married and you cannot get divorced before getting married");
} else if (person.isWidow()) {
System.out.println("Your maritalState status is widow, you cannot get divorced.");
}
}
void die(Person person) {
person.maritalState = new MaritalState("dead");
person.mate.maritalState = new MaritalState("widow");
System.out.println("Sorry for your loss, " + person.mate.name + " maritalState status is widow.");
}
public boolean isMarried(MaritalState maritalState) {
return maritalState.state.equals(MARRIED);
}
public boolean isSingle(MaritalState maritalState) {
return maritalState.state.equals(SINGLE);
}
public boolean isWidow(MaritalState maritalState) {
return maritalState.state.equals(WIDOW);
}
public String get() {
return state;
}
}
这是我教授的 oo 编程作业。但是我发现Personclass里面的婚姻状态可以是一个变量,我怎么用两个class实现呢?
假设人们可以有婚姻状况:单身、已婚、丧偶、离婚。创建处理人们婚姻状况的状态 OODP(即 Java、JavaScript、C++、Python 或 Ruby)。您将至少有两个 classes:Person 和 Marital State。确保遵循以下规则:单身->已婚已婚->离婚|寡妇离婚 -> 已婚寡妇 -> 已婚 创建一个客户端 class 来测试你的程序。请务必测试有效和无效的婚姻状况变更。
The assignment page
这些是我的代码:
public class AssignmentOOP {
public static void main(String[] args) {
Person p1 = new Person("p1");
Person p2 = new Person("p2");
Person p3 = new Person("p3");
p1.PrintMaritalStatus();
p2.PrintMaritalStatus();
p3.PrintMaritalStatus();
p1.GetMarried(p2);
p1.GetMarried(p3);
p2.Died();
p1.GetMarried(p3);
}
}
class Person {
String maritalstatus;
boolean mateIsAlive;
Person mate;
String name;
Person(String name1) {
maritalstatus = "single";
mate = null;
name = name1;
}
void GetMarried(Person mate) {
if(this.maritalstatus.equals("married")|| mate.maritalstatus.equals("married"))
{
System.out.println("Marital status error! At least one of you are married");
return;
} else {
this.maritalstatus = "married";
this.mate = mate;
mate.maritalstatus = "married";
mate.mate = this;
System.out.println("Congratulations!!! " + this.name + " and " + mate.name + " are married!");
}
}
void GetDivorced(Person mate) {
if(this.maritalstatus.equals("married") && this.mate == mate) {
maritalstatus = "divorced";
System.out.println(this.name+" and "+mate.name+" are getting divorced.");
}else if(this.maritalstatus.equals("single")) {
System.out.println("You are not married and you cannot get divorced before getting married");
}else if(maritalstatus.equals("widow")) {
System.out.println("Your marital status is widow, you cannot get divorced.");
}
}
void Died() {
this.maritalstatus = "dead";
this.mate.maritalstatus = "widow";
System.out.println("Sorry for your loss, " + this.mate.name + " marital status is widow.");
}
void PrintMaritalStatus() {
System.out.println(this.name + " marital status is " + this.maritalstatus);
}
}
这里:
String maritalstatus;
这可能不是你的教授想要的。那个婚姻状况的要点是:只有一套固定的价值观。
正确的表达方式是在 java 中使用 enum,例如:
public enum MaritalStatus { SINGLE, MARRIED, ... }
然后您可以通过创建一个 状态机 来进一步增强此功能,该状态机会执行规则,例如:当您是单身时,并且 doMarry()
,您的新状态之后应该结婚了。
在复制和过去之前..
这个解决方案可能不是您的教授想要的。在这种情况下可以使用多种其他解决方案。
在下面的解决方案中我假设你只能使用2 类!
可能的解决方案
您的 Person
应该有一个 MaritalState
类型的字段。 MaritalState
跟踪 Person
的状态,因此它不必自己完成。
以下只是快速而粗略的,旨在向您展示聚合背后的想法。
人
Person
现在将所有依赖于 MaritalState
的逻辑委托给 MatrialState
public class Person {
MaritalState maritalState;
Person mate;
String name;
Person(String name1) {
maritalState = new MaritalState();
mate = null;
name = name1;
}
void GetMarried(Person mate) {
maritalState.marry(this, mate);
}
void GetDivorced(Person mate) {
maritalState.divorce(this, mate);
}
void Died() {
maritalState.die(this);
}
void PrintMaritalStatus() {
System.out.println(this.name + " maritalState status is " + this.maritalState.get());
}
public boolean isMarried() {
return maritalState.isMarried(maritalState);
}
public boolean isSingle() {
return maritalState.isSingle(maritalState);
}
public boolean isWidow() {
return maritalState.isWidow(maritalState);
}
}
婚姻状况
处于 Person
的逻辑现在处于 Martiral
状态。我扩展了一些功能,例如 isSingle()
以使代码更具可读性。
public class MaritalState {
private static final String MARRIED = "married";
private static final String SINGLE = "single";
private static final String WIDOW = "widow";
private String state;
private MaritalState(String state) {
this.state = state;
}
public MaritalState() {
this.state = "single";
}
void marry(Person person, Person mate) {
if (person.isMarried() || mate.isMarried()) {
System.out.println("MaritalState status error! At least one of you are married");
} else {
person.maritalState = new MaritalState(MARRIED);
person.mate = mate;
mate.maritalState = new MaritalState(MARRIED);
mate.mate = person;
System.out.println("Congratulations!!! " + person.name + " and " + mate.name + " are married!");
}
}
void divorce(Person person, Person mate) {
if (person.isMarried() && person.mate == mate) {
person.maritalState = new MaritalState("divorced");
System.out.println(person.name + " and " + mate.name + " are getting divorced.");
} else if (person.isSingle()) {
System.out.println("You are not married and you cannot get divorced before getting married");
} else if (person.isWidow()) {
System.out.println("Your maritalState status is widow, you cannot get divorced.");
}
}
void die(Person person) {
person.maritalState = new MaritalState("dead");
person.mate.maritalState = new MaritalState("widow");
System.out.println("Sorry for your loss, " + person.mate.name + " maritalState status is widow.");
}
public boolean isMarried(MaritalState maritalState) {
return maritalState.state.equals(MARRIED);
}
public boolean isSingle(MaritalState maritalState) {
return maritalState.state.equals(SINGLE);
}
public boolean isWidow(MaritalState maritalState) {
return maritalState.state.equals(WIDOW);
}
public String get() {
return state;
}
}