C#C
C#3y ago
Gopher

Inherit from Abstract class: base.Do() vs field.Do

If im using the command Pattern, is there some rule, that says, that i should use base.Do instead of creating a field?

Example:
public class Increment : CounterCommand
    {

        private Counter counter;

        public Increment(Counter counter) : base(counter)
        {
            this.counter = counter;
        }

        public override void Execute() => counter.Increment();
        public override void Undo() => counter.Decrement();
        public override void Redo() => counter.Increment();
    }

or:
public class Increment : CounterCommand
    {
        public Increment(Counter counter) : base(counter)
        {
            
        }

        public override void Execute() => base.counter.Increment();
        public override void Undo() => base.counter.Decrement();
        public override void Redo() => base.counter.Increment();
    }

Why is the better approach better?
Was this page helpful?