Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich design option is more suitable for auto-correction on construction?
    text
    copied!<p>Trying to decipher an appropriate OO design to implement. The basic scenario is that you have a PstnNumber which is essentially a 10 digit phone number that always starts with 0 (e.g. 0195550000). A rule has been introduced to allow auto-correcting of a number if the leading 0 is missing (e.g. 195550000). </p> <p><strong>START EDIT</strong></p> <p>I realised the original question may have been misunderstood (thankyou kindly to those whom have answered already), so I have edited to try and better explain the scenario.</p> <p><strong>END EDIT</strong></p> <p>I started playing with some preliminary concepts and then thought I would ask if there was a more appropriate way to go or do one of these suffice (on some level)?</p> <p>Concept 1</p> <pre><code>public class PstnNumber { public virtual string Number { get; set; } public PstnNumber() { } public PstnNumber(string number) { this.Number = number; } } public class AutoFormattedPstnNumber : PstnNumber { public override string Number { get { return base.Number; } set { base.Number = value.PadLeft(10, '0'); } } public AutoFormattedPstnNumber() : base() { } public AutoFormattedPstnNumber(string number) { this.Number = number; } } </code></pre> <p>Concept 2 (removed)</p> <p>Concept 3</p> <pre><code>public class PstnNumber { public bool AutoCorrect { get; set; } private string number; public virtual string Number { get { return (this.AutoCorrect) ? this.number.PadLeft(10, '0') : this.number; } set { this.number = value; } } public PstnNumber() : this(false) { } public PstnNumber(bool autoCorrect) { this.AutoCorrect = autoCorrect; } public PstnNumber(string number) : this(false) { this.Number = number; } public PstnNumber(string number, bool autoCorrect) : this(autoCorrect) { this.Number = number; } } </code></pre> <p>I think Concept 1 may violate the Liskov Substitution rule because the subclass changes the behaviour of the Number property (happy to learn if I've misunderstood that).</p> <p>Any alternative suggestions would be received happily.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload