0

I have 3 different objects, lets call them: Box, Square, Triangle. Now, I want to make an update function that requires an object to be passed. As far as I know, the function would look like this

void Update(Box box, Square square, Triangle triangle)
{
    //do something 
}

That would require me to pass all three object even if sometimes I want to update just one of them. The other way I know would be by initializing objects previously in constructor and pass them into a private variable that is inside the class. Then I would update all of the objects, whether I want to update all of them or not.

void Update()
{
    //do something for Box 
    //do the same for Square
    //do the same for Triangle
}

This would require a lot of code, sometimes unnecessary. Now, is there any way that I could pass whichever object do I want and do the same code. For example

void Update(Object randomObject)
{
    //do something for the randomObject whether its Square, Box or Triangle 
}
2
  • Why don't you have an Update method in each class? So every instance can update itself? Or derive the three classes from an abstract base class that forces every derived class to implement an Update method? You approach seems not very object oriented but you need to give more context. What is this class that can update a Box a Square or a Triangle? Commented Dec 7, 2014 at 13:32
  • Its a class that detects collision between those objects and player. Now, I have a function inside a class that has the same way of detecting collision between all those objects. Problem is that I have to write the same code 3 times for each object, because those objects are not the same type and I cannot just put it all into one code and request one object to be passed. I hope I explained it, it is really hard. Anyway, what I'm looking for is a universal object that can be passed as an argument and I can refer Square, Triangle or Box, whatever I need. Commented Dec 7, 2014 at 13:41

3 Answers 3

1

You can define a common interface for your objects using a base class or an interface. For your example, you could have a Shape base class:

public abstract class Shape 
{ 
    // Put common methods here, possibly abstract:
    public abstract double Area();
}

Then define your concrete shapes as inheritors of Shape:

public class Box : Shape 
{
    public double Area() { return _width * _height; }
}

// etc.

Then you can have your Update method take a Shape argument. The parameter can now be any of your classes inheriting Shape and you can use any of the methods defined on Shape

void Update(Shape shape)
{
}

Abstract in this context means that the implementation is deferred to the concrete implementations, so you would need to provide the implementation in the concrete classes - exemplified by the Area implementation in the Box class - but of course, you can also define non-abstract methods in the base class.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, use one of the primary features of OO programming - Polymorphism. Your three classes should inherit from the same base class. In that class implement method update. Polymorphism

Comments

0

I don't know your scenario details but you may do something like this :

void Update(Box box = null, Square square = null, Triangle triangle = null)
{
    if(box !=null)
    {
       //Do box update
    }
    if(square !=null)
    {
       //Do square update
    }
    if(triangle  !=null)
    {
       //Do triangle update
    }
}

and you could call this method just for one or two objects

Update(square: mysquare);

Anyway if you could manipulate your Model design to make all three classes inherit from one base class it would be better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.