面向状态的OO编程

时间:2018-11-26 08:04:18

标签: java oop design-patterns object-oriented-analysis

这是我教授的oo编程任务。但是我发现婚姻状况在Person类中可以是一个变量,如何通过两个类来实现?

假设人们可以拥有婚姻状况:单身,已婚,丧偶,离婚。创建一个状态OODP(即Java,JavaScript,C ++,Python或Ruby)来处理人们的婚姻状况。您将至少有两节课:人格和婚姻状态。确保遵循以下规则:单身->已婚->离婚|寡妇离婚->已婚寡妇->已婚创建一个Client类来测试您的程序。确保测试婚姻状况的有效和无效。
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);
    }
}

2 个答案:

答案 0 :(得分:1)

这里:

String maritalstatus;

这可能不是您的教授想要的。婚姻状况的关键是:只有一组固定的值。

表达的正确方法是在Java中使用enum,例如:

public enum MaritalStatus { SINGLE, MARRIED, ... } 

然后,您可以通过创建实现规则的状态机来进一步增强此功能,例如:当您为SINGLE时,而doMarry()则应在之后更改您的新状态。

答案 1 :(得分:0)

复制和粘贴之前。.

此解决方案可能不是您的教授想要的。在这种情况下,可以使用多种其他解决方案。

在以下解决方案中,我假设您只能使用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;
  }
}