简体   繁体   English

如何检查“修改”状态:一个额外的字段,或==运算符?

[英]How to check “modified” state: an extra field, or == operator?

I'm designing a new service that takes two strings, and may or may not change them and returns the (possibly) modified value. 我正在设计一个新的服务,该服务需要两个字符串,并且可以更改它们,也可以不更改并返回(可能)修改后的值。

For instance: 例如:

public class Phrases
{
    public string Phrase1 { get; set; }
    public string Phrase2 { get; set; }
}

public interface IModifier
{
    Phrases Go(Phrases phrases);
}

In addition, the client should know if the original search phrase changed or not. 此外,客户应知道原始搜索词是否已更改。

The question is: what should the returned type be? 问题是:返回的类型应该是什么?

If it's the same Phrases class, then there should be an overload for the == operator (and/or Equals method). 如果它是相同的Phrases类,则==运算符(和/或Equals方法)应该有一个重载。 So that the client's code would look like this: 这样客户端的代码将如下所示:

Phrases phrases = new Phrases { Phrase1 = "hello", Phrase2 = "world" };
Phrases modifierResult = someModifier.Go(phrases);
if (modifierResult.Equals(phrases))
    // do something
    ;

However, this leaves the client to keep the original state, too. 但是,这也使客户端也保持原始状态。

The alternative is to have an extra field in the modifier result's returned type: 另一种方法是在修饰符结果的返回类型中包含一个额外的字段:

public interface IModifier
{
    ModifiedPhrases Go(Phrases phrases);
}

public class ModifiedPhrases : Phrases
{
    public bool IsModified { get; set; }
}

And then the client would have the IsModified field, without the need to keep the original instance. 然后,客户端将具有IsModified字段,而无需保留原始实例。
However, this way we have another type in the system. 但是,这样,我们在系统中就有了另一种类型。 In addition, maybe it's not the service's responsibility to indicate for modification in the first place. 此外,也许一开始就指示修改不是服务的责任。 So maybe it's the client's responsibility to check for modification, and hence the IsModified might be unnecessary. 因此,检查修改是否是客户的责任,因此IsModified可能是不必要的。

What would you choose, and why? 您会选择什么,为什么?

You can add additional field StatusID May be in future you want to delete the record. 您可以添加其他字段StatusID,将来可能要删除该记录。 The StatusId will be int and can be set with the following values StatusId将为int,可以使用以下值进行设置

  1. 2- Delete 2-删除
  2. 8- Changed 8-已更改
  3. 0 - Unchanged 0-不变

With the statuses above you can do bit operational. 使用上面的状态,您可以进行一些操作。 For example you can change if record was deleted with StatusId & 2 例如,您可以更改是否使用StatusId&2删除了记录

Both of your suggestions sound good, and my choice would depend on the situation, mainly how many original states I would have to hold for how long, and how many equality-checks I would have to do. 您的两个建议听起来都不错,我的选择将取决于具体情况,主要是我必须保持多少个原始状态多长时间,以及我必须进行多少次平等检查。 From the design point of view, I do prefer using a method IsModified . 从设计的角度来看,我更喜欢使用IsModified方法。


Some details: 一些细节:

If you are not using isModified : Think about making Phrases immutable, eg in Java via 如果您没有使用isModified :考虑使Phrases不可变,例如在Java中通过

public class Phrases
{
    private String phrase1;
    private String phrase2;

    public Phrases(String p1, String p2) {
      phrase1 = p1;
      phrase2 = p2;
    }

    //getter and no setter for phrase1 and phrase2...

    //equals()...
}

Then you could alternatively make the constructor private and offer a factory method with caching functionality, and even guarantee that only one instance holding {"hello", "world"} exists. 然后,您也可以将构造函数设为私有,并提供具有缓存功能的工厂方法,甚至可以保证仅存在一个包含{“ hello”,“ world”}的实例。 That way you save memory and can do the equality check faster via checking object identity. 这样,您可以节省内存,并可以通过检查对象标识来更快地进行相等性检查。

If you are using isModified : Think about using an enum instead of boolean IsModified . 如果使用的是isModified :考虑使用枚举而不是boolean IsModified That way, you can extend the possible statuses the object can be in, as Gregory suggested, but in a much cleaner way. 这样,您可以按照Gregory的建议扩展对象可能处于的状态,但是使用的方式更简洁。 For details, see Josh Bloch's Effective Java item 30, "Use enums instead of int constants". 有关详细信息,请参见Josh Bloch的Effective Java项目30,“使用枚举而不是int常量”。

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

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