简体   繁体   English

面向状态的OO编程

[英]OO programming about state

This is my professor's assignment for oo programming. 这是我教授的oo编程任务。 But I found that the marital status can be an variable in the Person class, how can I achieve this with two classes? 但是我发现婚姻状况在Person类中可以是一个变量,如何通过两个类来实现?

Assume people can have marital status: single, married, widow, divorced. 假设人们可以拥有婚姻状况:单身,已婚,寡妇,离婚。 Create a state OODP (ie, Java, JavaScript, C++, Python, or Ruby) that deal with people's marital status. 创建一个状态OODP(即Java,JavaScript,C ++,Python或Ruby)来处理人们的婚姻状况。 You will have at least two classes: Person, and Marital State. 您将至少有两节课:人格和婚姻状态。 Make sure the following rules are followed: single->married married-> divorced | 确保遵循以下规则:单身->已婚->离婚| widow divorced -> married widow -> married create a Client class to test your program. 寡妇离婚->已婚寡妇->已婚创建一个Client类来测试您的程序。 Make sure you test valid and invalid change of marital status. 确保测试婚姻状况的有效和无效变化。
The assignment page 作业页面


These are my codes: 这些是我的代码:

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

Here: 这里:

String maritalstatus;

That is probably not what your professor wants. 那可能不是您的教授想要的。 The point of that marital status is: there is only a fixed set of values. 该婚姻状况的重点是:只有一组固定的值。

The proper way to express that is to use an enum in java, like: 表达的正确方法是在Java中使用枚举 ,例如:

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

You could then further enhance this by creating a state machine , that implements the rules, such as: when you are SINGLE, and doMarry() , your new status should be MARRIED afterwards. 然后,您可以通过创建执行规则的状态机来进一步增强此功能,例如:当您为SINGLE时,以及doMarry() ,之后应将您的新状态doMarry()

Before copy and past.. 复制和过去之前

This solution is maybe not what your professor want. 该解决方案可能不是您的教授想要的。 There are multiple other solutions that could be used in this scenario. 在这种情况下,可以使用多种其他解决方案。

In the following solution I assume that you can only use 2 classes! 在以下解决方案中, 我假设您只能使用2个类!

Possible Solution 可能的解决方案

Your Person should have a field of type MaritalState . 您的Person应具有MaritalState类型的MaritalState This MaritalState tracks the state of a Person so it don't have to do it by its own. MaritalState跟踪一个Person的状态,因此不必自己执行。

The following is just quick and dirty and is to show you the idea behind an aggregation. 以下内容既快速又肮脏 ,目的是向您展示聚合背后的想法。

Person

The Person now delegates all the logic that depends on the MaritalState to MatrialState 现在,“ 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);
  }
}

MaritalState MaritalState

The logic which was in Person is now in Martiral state. 这在逻辑Person现在处于Martiral状态。 I extend some functionality like isSingle() to make the code more readable. 我扩展了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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM