Sharing properties without inheritance

I use the Unity engine, where the base class for almost all scripts should be obtained from MonoBehaviour. I created my own wrapper class, which derives from MonoBehaviour called CMonoBehaviour, which contains some of my helper functions, which I use almost everywhere. But now I am faced with the problem that SOMETIME I do not want to retrieve from MonoBehavuour, but ThirtPartyLibraryWrapperClass (a class that also derives from MonoBehaviour but also performs some additional functionality - just like my wrapper). Here is an example of what I want to archive.

edit: I forgot to mention that ThirtPartyLibraryWrapperClass is packaged in a dll, which means that I cannot change it in any way.

 // This is what I have
public class CMonoBehavour : MonoBehaviour
{
    public int SomeHelperProperty1 { get; private set; }
    public int SomeHelperProperty2 { get; private set; }
    public int SomeHelperProperty3 { get; private set; }
    public int SomeHelperProperty4 { get; private set; }
}

public class ThirtPartyLibraryWrapperClass : MonoBehaviour
{
    public int SomeHelperProperty5 { get; private set; }
    public int SomeHelperProperty6 { get; private set; }
}

// My problem :

public class ExampleUseage1 : CMonoBehavour
{
    // here I have to copypaste content of ThirtPartyLibraryWrapperClass because I cust cannot derive from two classes
}

public class ExampleUseage2 : ThirtPartyLibraryWrapperClass
{
    // here I have to copypaste content of CMonoBehavour because I cust cannot derive from two classes
}
+4
3

, . , , .

, :

  • ( - ).
  • use interfaces + composition = > , .

/ :

    public class CMonoBehavour : MonoBehaviour
    {
        public int SomeHelperProperty1 { get; private set; }
        public int SomeHelperProperty2 { get; private set; }
        public int SomeHelperProperty3 { get; private set; }
        public int SomeHelperProperty4 { get; private set; }
    }

    public class ThirtPartyLibraryWrapperClass : MonoBehaviour
    {
        public int SomeHelperProperty5 { get; private set; }
        public int SomeHelperProperty6 { get; private set; }
    }

    //COMPOSITE SOLUTION

    public class CompositeMonoBehaviour : CMonoBehaviour
    {
        private ThirtPartyLibraryWrapperClass _thirdParty;

        public int SomeHelperProperty5 { get{ return _thirdParty.SomeHelperProperty5; } }
        public int SomeHelperProperty6 { get{ return _thirdParty.SomeHelperProperty6; } }
    }

    public class ExampleUseage1 : CompositeMonoBehaviour
    {
          //you have all your properties, great news!
    }

    public class ExampleUseage2 : CompositeMonoBehaviour
    {
        //you have all your properties, great news!
    }

, Unity , GameObject. :

  • ,
+3

, . - :

public class CMonoBehavour : MonoBehaviour
{
    public int SomeHelperProperty1 { get; private set; }
    public int SomeHelperProperty2 { get; private set; }
    public int SomeHelperProperty3 { get; private set; }
    public int SomeHelperProperty4 { get; private set; }
}

public class ThirtPartyLibraryWrapperClass : CMonoBehavour
{
    public int SomeHelperProperty5 { get; private set; }
    public int SomeHelperProperty6 { get; private set; }
}


//The first class have 4 properties
public class ExampleUseage1 : CMonoBehavour
{

}
    //The first class have 6 properties
public class ExampleUseage2 : ThirtPartyLibraryWrapperClass
{

}
+1

CMonoBehavour (uh, CMonoBehaviour, i "" ) .

public interface CMonoBehaviour
{
    int SomeHelperProperty1 { get; }
    int SomeHelperProperty2 { get; }
    int SomeHelperProperty3 { get; }
    int SomeHelperProperty4 { get; }
}

, set, . - .

:

public class Something : MonoBehaviour,CMonoBehaviour
{
    private int a;
    public int SomeHelperProperty1 {
        get {
            return a;
        }

        private set  {
            a = value;
        }
    }
    ...
}

, , , , : IMonoBehaviour , , IBehaviourExtensions IBehaviourAdditions IBaseBehaviour. "MonoBehaviour" , / .

-1

Source: https://habr.com/ru/post/1676859/


All Articles