11

For example I have the following classes:

1.

class MyClass1
{
    public MyClass1 Method()
    {
        ...
        return new MyClass1();
    }
}

class MyClass2
{
    public MyClass2 Method()
    {
        ...
        return new MyClass2();
    }
}

The methods have the same bodies that's why I want to extract the code and re-use.

2.

abstract class MyClass
{

    protected void Method()
    {
        ...
    }
}

class MyClass1 : MyClass
{

    public MyClass1 Method()
    {
        base.Method();
        return new MyClass1();
    }
}

class MyClass2 : MyClass
{
    public MyClass2 Method()
    {
        base.Method();
        return new MyClass2();
    }
}

However since there are a lot of such methods it will be better to move the methods into the base class MyClass at all:

3.

abstract class MyClass<T>: where T : MyClass<T>
{
    protected abstract T Create();

    public T Method()
    {
        ...
        return Create();
    }
}

class MyClass1 : MyClass<MyClass1>
{
    protected override MyClass1 Create() => new MyClass1();
}

class MyClass2 : MyClass<MyClass2>
{
    protected override MyClass2 Create() => new MyClass2();
}

It works fine but the contract looks too weird. Of course, I can extract something like IMyInterface and return it instead of the class. However I have to preserve the original contract because it contains specific methods as well.

Upd: So, the weird thing is in bold - class MyClass1: MyClass<MyClass1>

22

3 Answers 3

4
+25

This is the usual so-called self-type problem (you have Method() should return the same type as the object on which it was called). Your solution #3 looks a lot like F-bounded quantification. However, this is C#, not Java, so we can do a bit better using an extension class.

You can make sure those methods only get called on subclasses of MyClass by adding a where T : MyClass bound on T.

// Put all your shared methods in generic classes in here.
public static class MyMethods
{
    public static T Method<T>(this T x) where T : MyClass
    {
        ...
    }
}

Your classes don't change much, except they won't need to mention Method (or the other shared methods) at all.

public abstract class MyClass
{
    ...
}

public class MyClass1 : MyClass
{
    ...
}

public class MyClass2 : MyClass
{
    ...
}
4
  • Is this new in c#: public static T Method<T extends MyClass>(this T x)? This is the first time if seen the use of extends keyword...
    – IronGeek
    Feb 25, 2019 at 2:35
  • @IronGeek Nah, just me forgetting the concrete syntax of C#. Moved into a where clause. This time, I also checked everything compiles. Thanks for keeping me honest :)
    – Alec
    Feb 25, 2019 at 3:04
  • It allows public access only. Private/protected of MyClass are not available.
    – Serg046
    Feb 25, 2019 at 11:16
  • @Serg046 Quite right. protected is the real problem here: these aren't real methods, so you will never be able to override them. If you intend to have all subclasses of MyClass in your project, internal just might work instead of private.
    – Alec
    Feb 25, 2019 at 14:43
3

Yes it looks a little bit weird to have a method which only create.

Because you have a 2 classes MyClass1 and MyClass2 which have their specific different methods and only base method is common (which you put in base class) I think you can use Abstract factory pattern.

public class ClassFactory: IClassFactory
{
    public MyClass Create()
    {
         if (some condition)
              return new MyClass1;
         return new MyClass2;
    }
}

class MyClass
{
    protected string CommonLogic()
    {
         //common logic 
         return string;
    }

}

class MyClass1 : MyClass
{
    public object SpecificMethod()
    {
        CommonLogic();
        .....
    }

}

class MyClass2 : MyClass
{
    public object SpecificMethod2()
    {
        CommonLogic();
        .....
    }
}

In this case you won't have duplicated code and you will have some class which one will have responsibility about creating you classes and will know when and which class return. + You will easy use it IoC here.

I hope my answer will help you.

7
  • 2
    How can it help? What's the difference between my #2 and your suggestion? Anyway, thank you for your time and attempt.
    – Serg046
    Feb 17, 2019 at 20:29
  • Logic in #2 and my answer more less the same. Problem in your case only in responsibilities. You have 2 classes which are created by their self. in my case it is some another class which will decide how to create and it means it will be more flexible. Send all dependencies on top level is good practise. Now if you need to use my factory you need to add constructor dependency and after that create what you want. If you need to change logic how to create class, you will only create another implementation of factory and change setup of IoC container. Feb 17, 2019 at 20:38
  • Or you can update how will you use this common method and we will find another solution or will keep yours. Feb 17, 2019 at 20:38
  • I need MyClass1/MyClass2 contract. They have their specific methods. I cannot describe them by a single contract. Of course, I can extract something like IMyInterface and return it instead of the class. However I have to preserve the original contract because it contains specific methods as well.
    – Serg046
    Feb 17, 2019 at 20:40
  • 3
    Casting? No, thanks. My options #2, #3 and your original suggestion are better.
    – Serg046
    Feb 17, 2019 at 21:00
0

There are some rare situations where a self-referencing type constraint may be needed, but I am not convinced that this is one of them.

It seems that you want to use the factory (Create) pattern and also have those factories return different concrete types. But at the same time you are saying that these concrete types all have something in common, specified by the base class.

The conventional approach would be to define the common features in an interface (IMyInterface as you suggested) and return that from the Create method. This would capture the polymorphic aspect of the concrete classes. Then the question is how to capture the other methods that are implemented in the concrete types. For this one can simply define additional interfaces that capture the various clusters of functionality implemented by more than one of the concrete classes.

To the extent that there are any dribs and drabs of functionality left over after you've done that, I would say handle them by casting would be the easiest -- at that point the functionality would be unique to just one of the concrete classes. If you want to fly with your eyes closed you could use the 'dynamic' type instead of casting.

Also, normally the Create method is not defined in the object instances, in other words normally objects are not their own factories. Typically they are either static or in a separate factory classes. In the current situation a bit of reflection helps deal with the fact that you have multiple derived types. There are various ways to do this besides what I show below.

So … perhaps something like this:

public interface ICommonFunctionality
{
    void SomethingThatEveryoneCanDo();
    // ... other common functionality
}

public interface IAdditionalFunctionality1
{
    void SomethingThatAFewCanDo();
    // ... other special functionality
}
public interface IAdditionalFunctionality2
{
    void SomethingThatOthersCanDo();
    // ... other special functionality
}

public class MyClass : ICommonFunctionality
{
    static public ICommonFunctionality Create(Type derivedType)
    {
        if (!typeof(ICommonFunctionality).IsAssignableFrom(derivedType)) { throw new ArgumentException(); }
        return derivedType.CreateInstance() as ICommonFunctionality;
    }

    virtual public void SomethingThatEveryoneCanDo() { /* ... */  }
}

public class MyClass1 : MyClass, IAdditionalFunctionality1
{
    public void SomethingThatAFewCanDo() { /* ... */ }
}

public class MyClass2 : MyClass, IAdditionalFunctionality1, IAdditionalFunctionality2
{
    public void SomethingThatAFewCanDo() { /* ... */ }
    public void SomethingThatOthersCanDo() { /* ... */ }
}

public class MyClass3 : MyClass, IAdditionalFunctionality2
{
    public void SomethingThatOthersCanDo() { /* ... */ }
}

public static class TypeHelpers
{
    public static object CreateInstance(this Type type, bool required = true)
    {
        System.Reflection.ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
        if (required && ctor == null) { throw new InvalidOperationException("Missing required constructor."); }
        return ctor?.Invoke(null);
    }
}

P.S. I have made the base class method virtual, this is pretty much optional depending on your situation.

1
  • This answer is very thick with terminology and thin on actual code. How exactly would you implement this for OP's MyClass, MyClass1, MyClass2, and Method? I don't see how this is any more than a partial description of the OP's 3rd attempt.
    – Alec
    Feb 28, 2019 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.