JEPAAB
JEPAAB

Reputation: 166

Inheritance of a Common Object

I have the following scenario where I retrieve a set of objects of type BaseObject. I split these object according to types Object1 and Object2.

Now I have two classes (Plugin1 and Plugin2) that process these list according to the particular type i.e. type Object1 or type Object2.

As you can see in the code I needed to declare the different lists in their respective classes.

Is there a way that is deceleration is set in the base class? I still need to be able to process the lists according to their type in their respective classes i.e. Plugin1 and Plugin2.

public class BaseObject {}
public class Object1 : BaseObject {}
public class Object2 : BaseObject {}

public interface IPlugin
{
    void Process();
}

public class BasePlugin : IPlugin
{
    protected MyManager _manager;        

    public BasePlugin(MyManager manager)
    {
        this._manager = manager;            
    }

    public virtual void Process() { }
}

public class Plugin1 : BasePlugin
{
    private List<Object1> _list;

    public Plugin1(MyManager manager, List<Object1> list)
        : base(manager)
    {
        _list = list;
    }

    public override void Process() 
    {
        base.Process();
    }
}

public class Plugin2 : BasePlugin
{
    private List<Object2> _list;

    public Plugin2(MyManager manager, List<Object2> list)
        : base(manager)
    {
        _list = list;
    }

    public override void Process() 
    {
        base.Process();
    }
}

Upvotes: 1

Views: 70

Answers (2)

SWeko
SWeko

Reputation: 30902

You should be able to make the IPlugin interface generic, i.e.

public interface IPlugin<T> where T: BaseObject 
{
    void Process();
}

public class BasePlugin<T> : IPlugin<T>
{
    protected MyManager _manager;        
    protected List<T> _list;

    public BasePlugin(MyManager manager, List<T> list)
    {
        this._manager = manager;    
       _list = list;        
    }

    public virtual void Process() {... }
}

Now, if the code for Plugin1 and Plugin2 is the same, and only differs in type, they just become:

var plugin1 = new BasePlugin<Object1>();
var plugin2 = new BasePlugin<Object2>();

i.e. you don't need the classes at all, you can just specify the type at declaration.

Upvotes: 2

JEPAAB
JEPAAB

Reputation: 166

Generics seemed to have done the job.

https://msdn.microsoft.com/en-us/library/512aeb7t.aspx

public abstract class BaseObject {}
public class Object1 : BaseObject {}
public class Object2 : BaseObject {}

public interface IPlugin
{
    void Process();
}

public class BasePlugin<T> : IPlugin where T : BaseObject
{
    protected MyManager _manager;        

    protected List<T> _list;

    public BasePlugin(MyManager manager, List<T> list)
    {
        this._manager = manager;            
        this._list = list;
    }

    public virtual void Process() { }
}

public class Plugin1 : BasePlugin<Object1>
{    
    public Plugin1(MyManager manager, List<Object1> list)
        : base(manager, list) { }

    public override void Process() 
    {
        base.Process();
    }
}

public class Plugin2 : BasePlugin<Object2>
{
    public Plugin2(MyManager manager, List<Object2> list)
        : base(manager, list) { }

    public override void Process() 
    {
        base.Process();
    }
}

Upvotes: 0

Related Questions