0

I'm looking to implement a certain behavior but I'm not sure how to implement it.

Given a base class :

public class Base
{
    void Start() { }
    void Update() { }
}

And these two classes which inherit it.

public class Behavior1 : Base
{
    private int member;
    void Start() { member = 0;  }
    void Update() { member++; }
}

public class Behavior2 : Base
{
    private string name;
    void Start() { name = "some string";  }
    void Update() { if(name) { Console.WriteLine(name) } }
}

And then a final class which I wish to inherit the logic of the two sub classes.

public class Child : Base // ? Behavior1, Behavior2
{
    void Start() {  } // logic and members implemented but don't need to be referenced
    void Update() {   }
}

How would I go about having the Child class implement the two Behavior classes? I don't think you can inherit more than one class at a time so I can't do that. Is there another construct which can accomplish this?

infamoustrey
  • 730
  • 8
  • 16
  • 2
    [Composition?](https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – Sweeper May 08 '22 at 16:37
  • @Sweeper I considered that, and will be willing to do it if nothing else makes sense, but was trying to avoid cluttering the `Child` class with calling the requisite behavior methods. – infamoustrey May 08 '22 at 16:38
  • What do you mean "cluttering the Child" - does it need the behavior or does it not? If it does, it needs to somehow invoke that behavior. That's not clutter, is it? – Fildor May 09 '22 at 00:03
  • @Fildor By cluttering I mean unnecessary invocation when simply using a directive will suffice. For instance, when inheriting: why reimplement the parent constructor in a child class when calling `: base(params)` will do? I'm simply curious to know if there is a better way to encapsulate the behavior above. I acknowledge that the method described in @Viktor is possibly the only way to accomplish this, but I'm curios to know if there is a better way. – infamoustrey May 09 '22 at 02:20
  • Ah, now I see what you mean by that. – Fildor May 09 '22 at 11:03

1 Answers1

0

Wihtout enter to valorate the inheritance, that probably need some think as you can read in the comments, you can do something like this if you want use both behaviors ni a class that doesn't inherith them:

    public class Child : Base
    {
        private readonly Behavior1 _behavior1;
        private readonly Behavior2 _behavior2;
        public Child()
        {
            this._behavior1 = new Behavior1();
            this._behavior2 = new Behavior2();
        }

        public override void Start()
        {
            this._behavior1.Start();
        }
        public override void Update()
        {
            this._behavior2.Update();
        }
    }

You can also inherith from Behavior1 and only add Behavior2 as a field:

    public class Child : Behavior1
    {
        private readonly Behavior2 _behavior2;
        public Child()
        {
            this._behavior2 = new Behavior2();
        }
        public override void Update()
        {
            this._behavior2.Update();
        }
    }

But, as I said, is probably that you find a better solution thinking about your models and their composition/inheritance.

Victor
  • 2,313
  • 2
  • 5
  • 13
  • I totally agree that this is probably the way to do it, I was just wondering if there was a way to do it without cluttering the Child class with the methods. – infamoustrey May 08 '22 at 17:30
  • If you are mixing behaviors from different objects, it's usually because inheritance isn't the solution. If it's something punctual, you can always create a static method and use it in the two classes that use that behavior, but if it's something generalized, inheritance isn't the solution. You can use dependency injection and the interfaces that your object requires (IStart, IUpdate...), you solve them according to the needs that you have in each moment and your object will have the behavior that you associate to it, without having to force an inheritance that does not make sense. – Victor May 08 '22 at 19:02