readonly class design when a non-readonly class is already in place

前端 未结 5 2418
借酒劲吻你
借酒劲吻你 2021-02-19 16:55

I have a class that upon construction, loads it\'s info from a database. The info is all modifiable, and then the developer can call Save() on it to make it Save that informati

5条回答
  •  耶瑟儿~
    2021-02-19 17:42

    The Liskov Substitution Principle says that you shouldn't make your read-only class inherit from your read-write class, because consuming classes would have to be aware that they can't call the Save method on it without getting an exception.

    Making the writable class extend the readable class would make more sense to me, as long as there is nothing on the readable class that indicates its object can never be persisted. For example, I wouldn't call the base class a ReadOnly[Whatever], because if you have a method that takes a ReadOnlyPerson as an argument, that method would be justified in assuming that it would be impossible for anything they do to that object to have any impact on the database, which is not necessarily true if the actual instance is a WriteablePerson.

    Update

    I was originally assuming that in your read-only class you only wanted to prevent people calling the Save method. Based on what I'm seeing in your answer-response to your question (which should actually be an update on your question, by the way), here's a pattern you might want to follow:

    public abstract class ReadablePerson
    {
    
        public ReadablePerson(string name)
        {
            Name = name;
        }
    
        public string Name { get; protected set; }
    
    }
    
    public sealed class ReadOnlyPerson : ReadablePerson
    {
        public ReadOnlyPerson(string name) : base(name)
        {
        }
    }
    
    public sealed class ModifiablePerson : ReadablePerson
    {
        public ModifiablePerson(string name) : base(name)
        {
        }
        public new string Name { 
            get {return base.Name;}
            set {base.Name = value; }
        }
    }
    

    This ensures that a truly ReadOnlyPerson cannot simply be cast as a ModifiablePerson and modified. If you're willing to trust that developers won't try to down-cast arguments in this way, though, I prefer the interface-based approach in Steve and Olivier's answers.

    Another option would be to make your ReadOnlyPerson just be a wrapper class for a Person object. This would necessitate more boilerplate code, but it comes in handy when you can't change the base class.

    One last point, since you enjoyed learning about the Liskov Substitution Principle: By having the Person class be responsible for loading itself out of the database, you are breaking the Single-Responsibility Principle. Ideally, your Person class would have properties to represent the data that comprises a "Person," and there would be a different class (maybe a PersonRepository) that's responsible for producing a Person from the database or saving a Person to the database.

    Update 2

    Responding to your comments:

    • While you can technically answer your own question, StackOverflow is largely about getting answers from other people. That's why it won't let you accept your own answer until a certain grace period has passed. You are encouraged to refine your question and respond to comments and answers until someone has come up with an adequate solution to your initial question.
    • I made the ReadablePerson class abstract because it seemed like you'd only ever want to create a person that is read-only or one that is writeable. Even though both of the child classes could be considered to be a ReadablePerson, what would be the point of creating a new ReadablePerson() when you could just as easily create a new ReadOnlyPerson()? Making the class abstract requires the user to choose one of the two child classes when instantiating them.
    • A PersonRepository would sort of be like a factory, but the word "repository" indicates that you're actually pulling the person's information from some data source, rather than creating the person out of thin air.
    • In my mind, the Person class would just be a POCO, with no logic in it: just properties. The repository would be responsible for building the Person object. Rather than saying:

      // This is what I think you had in mind originally
      var p = new Person(personId);
      

      ... and allowing the Person object to go to the database to populate its various properties, you would say:

      // This is a better separation of concerns
      var p = _personRepository.GetById(personId);
      

      The PersonRepository would then get the appropriate information out of the database and construct the Person with that data.

      If you wanted to call a method that has no reason to change the person, you could protect that person from changes by converting it to a Readonly wrapper (following the pattern that the .NET libraries follow with the ReadonlyCollection class). On the other hand, methods that require a writeable object could be given the Person directly:

      var person = _personRepository.GetById(personId);
      // Prevent GetVoteCount from changing any of the person's information
      int currentVoteCount = GetVoteCount(person.AsReadOnly()); 
      // This is allowed to modify the person. If it does, save the changes.
      if(UpdatePersonDataFromLdap(person))
      {
           _personRepository.Save(person);
      }
      
    • The benefit of using interfaces is that you're not forcing a specific class hierarchy. This will give you better flexibility in the future. For example, let's say that for the moment you write your methods like this:

      GetVoteCount(ReadablePerson p);
      UpdatePersonDataFromLdap(ReadWritePerson p);
      

      ... but then in two years you decide to change to the wrapper implementation. Suddenly ReadOnlyPerson is no longer a ReadablePerson, because it's a wrapper class instead of an extension of a base class. Do you change ReadablePerson to ReadOnlyPerson in all your method signatures?

      Or say you decide to simplify things and just consolidate all your classes into a single Person class: now you have to change all your methods to just take Person objects. On the other hand, if you had programmed to interfaces:

      GetVoteCount(IReadablePerson p);
      UpdatePersonDataFromLdap(IReadWritePerson p);
      

      ... then these methods don't care what your object hierarchy looks like, as long as the objects you give them implement the interfaces they ask for. You can change your implementation hierarchy at any time without having to change these methods at all.

提交回复
热议问题