另一方面,如果两个方法设计为逻辑彼此相互独立,那么它们将支持两个场景。在这个优胜的例子中,postLogonMessage是一个局域变量,由getPostLogonMessage()方法自己创建:

public boolean isActiveForPurchasing() {

 return this.accountStatus != null && this.accountStatus.equals("A");

}

public String getPostLogonMessage() {

 if("A".equals(this.accountStatus)){

    return "Your purchasing account is active.";

 } else if("E".equals(this.accountStatus)) {

    return "Your purchasing account has " +

           "expired due to a lack of activity.";

 } else {

    return "Your purchasing account cannot be " +

           "found, please call customer service "+

           "for assistance.";

 }

}

  使得这两个方法彼此独立的一个额外的好处是这些方法容易被理解。例如,isActiveForPurchasing()更加具有可读性,因为它仅仅只回答“is active for purchasing”的问题,而不是相反的它还要设置“post logon message”。另外一个额外的好处是每一个方法都能被独立的测试,这也使得测试容易被理解:

public class CustomerAccountTest extends TestCase{

 public void testAccountIsActiveForPurchasing(){

    String username = "robertmiller";

    String password = "java.net";

 

    class CustomerAccountMock extends CustomerAccount{

      ...

      public void loadAccountStatus() {

        this.accountStatus = "A";

      }

    }

    ICustomerAccount ca = new CustomerAccountMock(username, password);

    try {

      ca.loadAccountStatus();

    } catch (CustomerAccountsSystemOutageException e) {

      fail(""+e);

    }

    assertTrue(ca.isActiveForPurchasing());

  }