0

I have a CustomObject class which has the abilities like being Clickable, KeyPressable and Movable. I created interfaces: IClickable, IKeyPressable and IMovable and make different CustomObject classes to implement them. For example;

public class ClickableObject: IClickable {...}
public class MovableObject: IMovable {...}
public class ClickableAndMovableObject: IClickable , IMovable {...}

But I'm thinking now that I can do it with one class and less lines like below;

public class CustomObject 
{
    public bool isClickable;
    public bool isMovable;
    public bool isKeyPressable;

    // OnClick, OnMove and OnKeyPress events and other functions.
}

and use this bool values if they are clickable or movable etc.

I am not sure which is the correct way to do.

4

If the interfaces are empty and you find yourself querying:

if (someObject is IClickable)
{
    ...
}

then those interfaces are probably not very useful and can be replaced by boolean values.

If on the other hand the interfaces contain members which are actively used, for example:

clickableObject = someObject as IClickable;
if (clickableObject != null)
{
    clickableObject.Click(this.coordinates);
}

then it makes perfect sense to use interfaces. If you are afraid of code duplication, make an abstract class inherited by any object which uses one of your interfaces. In this abstract class, create properties such as:

protected bool IsClickable
{
    get
    {
        return this is IClickable;
    }
}


protected bool AsClickable
{
    get
    {
        return this as IClickable;
    }
}

or go fancy with:

protected bool RunIfClickable(Action<IClickable> action)
{
    var clickable = this.AsClickable;
    if (clickable != null)
    {
        action(clickable);
    }
}

which makes the earlier example as easy as:

someObject.RunIfClickable(c => c.Click(this.coordinates));
  • I use them exactly the same as the second example. But my question is that I can use "if(obj.isClickable)" instead of converting operation. – Cem Jan 14 '15 at 6:43
  • @Cem: Your concern being the mess with as and an if block all over your code, I guess? I edited my question to show how code duplication can be avoided. – Arseni Mourzenko Jan 14 '15 at 7:07
  • But still I should implement Click for both ClickableAndMovableObject and ClickableObject (nearly c/p the code). So there are (2^3)-1 = 7 possibilities I should implement in worst scenario. – Cem Jan 14 '15 at 7:21
  • @Cem: I don't understand your comment. The number of possibilities is the same whenever you use interfaces or booleans. – Arseni Mourzenko Jan 14 '15 at 7:25
  • There is only one class if I don't use interfaces so I should only implement one Click function. But with interfaces I should implement Click for all classes which have IClickable interface. Am I wrong? – Cem Jan 14 '15 at 7:31

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.