开发者

How to check "modified" state: an extra field, or == operator?

开发者 https://www.devze.com 2023-04-05 14:50 出处:网络
I\'m designing a new service that takes two strings, and may or may not change them and returns the (possibly) modified value.

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 int开发者_开发问答erface 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). 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.

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.

What would you choose, and why?


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.


Some details:

If you are not using isModified: Think about making Phrases immutable, e.g. in Java via

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. 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. That way, you can extend the possible statuses the object can be in, as Gregory suggested, but in a much cleaner way. For details, see Josh Bloch's Effective Java item 30, "Use enums instead of int constants".


You can add additional field StatusID May be in future you want to delete the record. The StatusId will be int and can be set with the following values

  1. 2- Delete
  2. 8- Changed
  3. 0 - Unchanged

With the statuses above you can do bit operational. For example you can change if record was deleted with StatusId & 2

0

精彩评论

暂无评论...
验证码 换一张
取 消