GoldBishop
GoldBishop

Reputation: 2861

Workaround limitation of Multiple Inheritance Need

I have 3 classes, each of the classes can be inherited individually for certain inheritied abilities.

But 2 of the classes can be inherited into the 3rd to create a "super" class that includes the abilities of the other 2 and their default logic implementation.

What is the best approach for this to keep coding logic centralized?

As you will see below, Class2 is a distilled form of what i have built in Class3. I want to implement the validation logic for the properties in one location, instead of developing an interface and having to implement the logic over and over again for every implement.

There is a situation where Class2 can be used by itself as well as being coupled with Class1. Each of the classes have their own unique use but Class3 combines 2 & 1 for a ultimate implementation combining both classes functionality.

Classes:
public mustinherit class Class1(of T as {New, Component})
  private _prop as T

  public sub New()
    ...
  end sub

  protected friend readonly property Prop1 as T
    Get
      ..validation..
      return me._prop
    end get
  end property
end class

public mustinherit class Class2
  private _prop as short

  public sub new(val as short)
    me._prop = val
  end sub

  protected friend readonly property Prop2 as short
    get
      return me._prop
    end get
  end property
end class
Current Class3 implementation:
public mustinherit class Class3(of T as {New, Component})
  Inherits Class1(of T)

  'Distilled the logic design below into its own MustInherit class cause this design
  '  by itself is useful without Clas1 implementation.
  private _prop as Short  <--- Same as Class2._prop

  public sub New(val as short)
    me._prop = val
  end sub

  protected friend readonly property Prop2 as short
    get
      return me._prop
    end get
  end property
end class

Per @Servy:

Class1
public mustinherit class Data(of T as {New, Component})
...
end class
Class2
public mustinherit class Brand
...
end class
Class3
public mustinherit class BrandData(of T as {New, Component})
  inherits Data(Of T)
...
end class

Upvotes: 1

Views: 171

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

I want to implement the validation logic for the properties in one location, instead of developing an interface and having to implement the logic over and over again for every implement.

No need to implement interface over and over again. You must implement you validation logic at once then inject it to your classes which needs validation.

interface IValidationLogic
{}

class Class1
{
    protected readonly IValidationLogic _validationLogic;
    public Class1(IValidationLogicvalidationLogic)
    {
        _validationLogic = validationLogic;
    }
}

class Class2
{
    protected readonly IValidationLogic _validationLogic;
    public Class2(IValidationLogicvalidationLogic)
    {
        _validationLogic = validationLogic;
    }
}

class Class3 : Class2
{
    public Class2(IValidationLogicvalidationLogic)
         : base(validationLogic) 
    {}
}


class MyValidationLogic : IValidationLogic
{}


var obj = new Class3(new MyValidationLogic())

Upvotes: 1

Related Questions